![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
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);
} |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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 |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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; 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;
}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 |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
I see. so a star in front of the variables is pass by pointers...
It is for deference purpose right? |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
|
Quote:
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 |
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|