***Newbie Needs Help With Functions Program ***
Author Message
***Newbie Needs Help With Functions Program ***

Hello,

I just started this C class and need a little help coding this
program.  If anyone can show me some examples of how to write a simple
program to put these 3 functions in one program.

1. long intPower(short base short exponent)
Accepts two short integers and returns a long. The long return

represents the result of raising the base integer to the
exponent integer power.

2. double average(int i, int j, int k, int l)
average should return the real average of 4 integers sent to
the function .

3. void blankLines(short n)
the function should be used to produce n number of blank lines
in the program output.

4.  A function theat is sent 5 integers and returns the highest of the

highest of the five.

Any help anyone can give me would be greatly appreciated.  I am so
confused right now.
--

Mon, 05 Mar 2001 03:00:00 GMT
***Newbie Needs Help With Functions Program ***
Quote:
> 1. long intPower(short base, short exponent)

{// very slow version without errorhandling
long result=1;
if (base == 0) {
if (exponent < 0)
return ERRORVALUE;
else
return 0;    //    intPower(0,0) := 0 (1 also possible)
}

if (exponent ==0 {
return 1;
}

if (exponent < 0)
if (base>1)     // base > 1  follows  0 < 1/base < 1
return 0;
else
return 1;

while (exponent--) {
result *=base;  // overflow possible!
}
return result;

Quote:
}

faster version with <math.h>

{
return (long)(pow(base,short)); // check matherr

Quote:
}
> 2. double average(int i, int j, int k, int l)
>        average should return the real average of 4 integers sent to
>        the function .

{
return ( 1.0 * ( i + j + k + l ) )/4.0;

Quote:
}

> 3. void blankLines(short n)
>        the function should be used to produce n number of blank lines
>        in the program output.

{
if (n<=0){
return
}
while (n--) {
printf("\n");
}

Quote:
}

> 4.  A function theat is sent 5 integers and returns the highest of the

Takes an array on count integer (standart count=5), and returns the
highest one.

int Max(int *in, int count=5) {
int r = *in;
while (--count) {
in++;
if (r < *in) {
r = *in;
}
}
return r;

Quote:
}

Regards

Gert

--
Max-Planck-Institute of Cognitive NeuroSience      http://www.cns.mpg.de
Inselstrasse 22-26, 04103 Leipzig, Germany

Mon, 05 Mar 2001 03:00:00 GMT
***Newbie Needs Help With Functions Program ***

Quote:
>Hello,

>   I just started this C class and need a little help coding this
>program.  If anyone can show me some examples of how to write a simple
>program to put these 3 functions in one program.

People round here get annoyed if you ask us to do your homework for you
without at least trying. In future please post some sample code and state
what the problem with it is.

Quote:
>1. long intPower(short base short exponent)
>       Accepts two short integers and returns a long. The long return

look at this function

#include <math.h>

double pow(double base, double exp)

How to do the type conversions is left as an exercise to the reader.

Quote:
>returns base raised to the expth power.
>       represents the result of raising the base integer to the
>       exponent integer power.

>2. double average(int i, int j, int k, int l)
>       average should return the real average of 4 integers sent to
>       the function .

Quote:
>3. void blankLines(short n)
>       the function should be used to produce n number of blank lines
>       in the program output.

for (a=0;a<n;a++)printf("\n");

Quote:
>4.  A function theat is sent 5 integers and returns the highest of the

>       highest of the five.

if(a>b){/*stuff*/}

will probably come into it.

try writing the program, then if you get stuck post us the code and the
problem, and what you've done to try and fix it.

HTH,

Matthew

--
Matthew Vernon, Selwyn College Computer Support
Steward of the Cambridge Tolkien Society
"Elen sila lumenn' omentielvo"
http://www.cam.ac.uk/CambUniv/Societies/tolkien/  http://pick.sel.cam.ac.uk/

Mon, 05 Mar 2001 03:00:00 GMT
***Newbie Needs Help With Functions Program ***

This is a Windows program:

I have declared this function:

BOOL FAR Pascal DialogProc(HWND, UINT, WPARAM, LPARAM);

then:

long FAR PASCAL WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
static FARPROC lpProc;
switch(message)
{
case WM_CREATE:
this line!!!!!!       lpProc=MakeProcInstance(DialogProc, hAppli);

What the hell is wrong with it?
I'm getting ERROR: cannot convert int(__stdcall*)(hwnd, message, wParam,
lParam) to int(__stdcall*)() in function WndProc.

Haven't been able to correct it.

FARPROC is suppose to be a pointer to function?
MakeInstanceProc(FARPROC, hInstance);

BTW, DialogProc returns TRUE or FALSE. Typing FALSE works but the program
screws up. Typing TRUE gives the exact same error.
WHY?

Mon, 05 Mar 2001 03:00:00 GMT
***Newbie Needs Help With Functions Program ***

Quote:
>BOOL FAR PASCAL DialogProc(HWND, UINT, WPARAM, LPARAM);
[...]
>this line!!!!!!       lpProc=MakeProcInstance(DialogProc, hAppli);

>What the hell is wrong with it?
>I'm getting ERROR: cannot convert int(__stdcall*)(hwnd, message,
wParam,
>lParam) to int(__stdcall*)() in function WndProc.

>Haven't been able to correct it.

DialogProc and FARPROC are not one and the same type because they have
different parameters.  FARPROC is a generic type accepted by
MakeProcInstance(), whereas DialogProc accepts four parameters.

You must cast DialogProc to FARPROC explicitly in order to pass it to
MakeProcInstance().

lpProc=MakeProcInstance((FARPROC)DialogProc, hAppli);

My word of caution when casting function pointers to generic types is
to be certain that they are prototyped exactly as the caller expects
them to be.  To not do so is an invitation to any operating system to
have its way with you.

--
Quinn Tyler Jackson

url: http://www.qtj.net/~quinn/
ftp: qtj.net

Wed, 07 Mar 2001 03:00:00 GMT
***Newbie Needs Help With Functions Program ***

Quote:
>   I just started this C class and need a little help coding this
>program.  If anyone can show me some examples of how to write a
simple
>program to put these 3 functions in one program.

>1. long intPower(short base short exponent)
>       Accepts two short integers and returns a long. The long return

>       represents the result of raising the base integer to the
>       exponent integer power.

[... more homework questions followed ...]

Might I be so bold as to suggest that you do the exercises yourself?

You were given the exercises in order that you develop your analytical
skills, to test your ability to apply your knowledge, and to stretch
your mind.  You were not given those exercises to hand in our answers

How would you feel, if one day, just before going under on the
operating table, you were to find out that the surgeon had all of his
first year anatomy exams done for him by members of the
sci.medical.heartsurgery newsgroup?  You would flee in terror and
think the surgeon an awful loafer.

Software engineering is not something we professionals take lightly.
At times, it is a mind bending profession, and we take pride in our
ability to strive for some level of craft.  If you cannot manage to do
the above questions on your own, then why are you in a C class?
Perhaps you ought to take up something less strenuous or more to your
make-up.

post the results (or better yet, just those sections that really nag
at you and you don't know why they're nagging at you, and even after
reading the ten books you should have read, they won't stop nagging at
you) in one of the C flavor newsgroups, and politely ask for a code
review -- and you may find that people are willing to help you once
you have truly tried.

--
Quinn Tyler Jackson

url: http://www.qtj.net/~quinn/
ftp: qtj.net

Wed, 07 Mar 2001 03:00:00 GMT
***Newbie Needs Help With Functions Program ***
Wow! I can't believe you posted this!! You've got some big brass ones,
fella'.
books and figure out what to do. Don't be a lazy palooka.

/*******Example Starts****************/

#include<stdio.h>

double DivideTwoNumbers(int x, int y);

int main(void)
{
double quotient = DivideTwoNumbers( 43,13 );
printf("Quotient: %f \n", quotient);
return 0;

Quote:
}

double DivideTwoNumbers(int x, int y)
{
double q = (double(x)/double(y));
return q;
Quote:
}

/**** Example Ends **********/

Quote:
>1. long intPower(short base short exponent)
>       Accepts two short integers and returns a long. The long return

>       represents the result of raising the base integer to the
>       exponent integer power.

>2. double average(int i, int j, int k, int l)
>       average should return the real average of 4 integers sent to
>       the function .

--
Jim Murphy   cjmurphy<at>telusplanet.net

--

Fri, 09 Mar 2001 03:00:00 GMT

 Page 1 of 1 [ 9 post ]

Relevant Pages