Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 28th, 2005, 5:33 AM   #1
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
Question I am new to C and I need some help in these two C questions

Hi, I am writting these codes on the practice paper and I am not sure if I am correct:

1.) Write a C function stats that receives three integers and returns the average of these three integers as a double value and the sum as an integer through the parameter sum. You only need to write the function. The function prototype is given below:

double stats(int x, int y, int z, int *sum);

Here is my code...

double stats(int x, int y, int z, int *sum)
{

double average = (double) (x + y + z) /3;
sum = x + y + z;
*sum = sum;

printf("Average is: 6.2f. \nSum is: %d.", average, *sum);
return (0);
}

2.) Write a C function min_max that receives three integers and return the minimum and maximum values. The function prototype is given below:

void min_max(int x, int y, int z, int *max, int *min);

Here is my code...

void min_max(int x, int y, int z, int *max, int *min)
{

if((x>y) && (y>z))
          max = x; min = z; *max=max; *min=min;
elseif((x>y) && (y<z))
          max = x; min = y; *max=max; *min=min;
elseif((y>x) && (x>z))
          max = y; min = z; *max=max; *min=min;
elseif((y>x) && (x<z))
          max = y; min = x; *max=max; *min=min;
elseif((z>x) && (x>y))
          max = z; min = y; *max=max; *min=min;
elseif((z>x) && (x<y))
          max = z; min = x; *max=max; *min=min;
printf("Maximum is %d.\nMinimum is %d.", *max, *min);
return (0);
}
jch02140 is offline   Reply With Quote
Old Jul 28th, 2005, 6:20 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
The first one is pretty much there you just need 1 ammendment i think

double stats(int x, int y, int z, int *sum)
{

double average = (double) (x + y + z) /3;
sum = x + y + z;
*sum = sum;

printf("Average is: 6.2f. \nSum is: %d.", average, *sum);
return (average); //so it returns the average va;lue and sum points to the sum
}

And the second one i changed a lot of it :/

void min_max(int x, int y, int z, int *max, int *min)
{

if((x>y) && (y>z)){
          *max = x; *min = z;
}else if((x>y) && (y<z)){
          *max = x; *min = y; 
}else if((y>x) && (x>z)){
          *max = y; *min = z; 
}else if((y>x) && (x<z)){
          *max = y; *min = x; 
}else if((z>x) && (x>y)){
          *max = z; *min = y; 
}else if((z>x) && (x<y)){
          *max = z; *min = x; 
}
printf("Maximum is %d.\nMinimum is %d.", *max, *min);

}

If you need to no why the second one was changed just ask
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jul 28th, 2005, 7:40 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
In the first one, you don't declare a local variable named sum (which would conflict with the integer pointer, "sum", anyway), so the code
sum = x + y + z;
*sum = sum;
will attempt to overwrite the pointer value with x + y + z (the compiler should throw an error with this line), then use the value of the sum as a pointer, which, if you actually got this far, would likely cause a seg fault. You're also not returning the value as instructed.

Try this:
double stats (int x, int y, int z, int *sum)
{
   int localSum = x + y + z;
   double average = (double) localSum / 3.0;
   *sum = localSum;

   printf ("Average is: 6.2f. \nSum is: %d.", average, *sum);
   return average;
}
As for the second, I would write it so that the process is clear to the reader:

Get the largest of two values: largest = x > y? x : y;
Get the largest of that and the third value: largest = largest > z? largest : z;

If you haven't encountered the ternary operator yet, just replace with the appropriate conditional construct you're familiar with.

Larry Wall, the author of Perl, says, "tmtowtdir". That's, "There's more than one way to do it right." In deciding among correct solutions, clarity makes a good criterion.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 28th, 2005, 7:53 AM   #4
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
I see. so a star in front of the variables is pass by pointers...
It is for deference purpose right?
jch02140 is offline   Reply With Quote
Old Jul 28th, 2005, 8:07 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
That is correct. If you're unfamiliar with pointers, you might read the material at the link in my signature, Pointer Basics.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 28th, 2005, 10:12 AM   #6
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
Hi, just trying to see if I get it this time...

void min_max(int x, int y, int z, int *max, int *min)
{
int larger = x > y? x : y;
*max = larger > z? larger : z;

int smaller = x < y? x :y;
*min = smaller < z? smaller : z;
return;
}

I realised that this version produced some redundant check on the variables twice...

Also, can I do it this way?...

void min_max(int x, int y, int z, int *max, int *min) {
  if((*max = (x > y ? x : y)) < z) *max = z;
  if((*min = (x < y ? x : y)) > z) *min = z;
}
jch02140 is offline   Reply With Quote
Old Jul 28th, 2005, 10:33 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Actually, you can do it this way:

*max = z > (*max = x > y? x : y)? z : *max;

My comment regarding clarity versus terseness (which often doesn't result in reduced code) will come back to you the first time you go back to a program you wrote a year or so ago, in order to add to it. You won't know what the hell you did and will be tempted to call its author a dumbazz. Been there.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 28th, 2005, 11:34 AM   #8
pr0gm3r
Hobbyist Programmer
 
Join Date: Dec 2004
Location: CA
Posts: 102
Rep Power: 4 pr0gm3r is on a distinguished road
Send a message via MSN to pr0gm3r
Talking

Quote:
Originally Posted by DaWei
Actually, you can do it this way:

*max = z > (*max = x > y? x : y)? z : *max;

My comment regarding clarity versus terseness (which often doesn't result in reduced code) will come back to you the first time you go back to a program you wrote a year or so ago, in order to add to it. You won't know what the hell you did and will be tempted to call its author a dumbazz. Been there.
so am I, I took C++ class last spring 2004 and all the project that I did, I couldn't even remember what is the code for. I need to go over my program line by line...

I think in my opinion, we should use conditions statement such as if/else or switch, because it is much clear to read our program this way. Isn't it right?

^__^
__________________
--
pr0gm3r
pr0gm3r is offline   Reply With Quote
Old Jul 28th, 2005, 11:49 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
If one is familiar with the ternary operator, it is VERY clear. Nesting/chained expression evaluation makes it very less so. Additionally, if one wants to do that, one needs to review the precedence of operations of the language and what things are evaluated left-to-right or right-to-left. Obfuscation can set in very quickly. On the other hand, if one uses a lot of individual if-else-else if constructs, the operation can be splattered over so much territory that it quickly becomes unclear that one is dealing with three values in a particularly relative way.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC