# Thread: second version of old problem, help?

1. ## second version of old problem, help?

I decided not to use an array and go with a so called easier way. here is the question again
"write a program to read in 3 doubles then call 2 functions get_sum and get_big. The functions get passed the 3 doubles and return, respectively, the sum and the biggest, and print them out."

Here is my code, please take a look at it and tell me what is wrong, what i need to change etc. I know it is sloppy, i'm getting there though.

#include <stdio.h>
double get_sum(double, double , double, double);
void double get_big(double, double, double, double);
main()
{
double x, y, z, biggest;
printf ("please enter x y and z"\n)
scanf ("%d %d %d", &x, &y, &z);
get_sum(x, y, z, s);
printf("the sum is %f", s);
double get_big(x, y, z);
}
double get_sum(double a, double b, double sum)
{
double sum;
sum=a + b +c
return sum;
}
void double get_big(double c, double d, double e)
{
if (c > d & c > e)
printf ("%f is the biggest\n",c);
if (d > c & d> e)
printf ("%f is the biggest\n",d);
else
printf("%d is the biggest\n",e);
return;
}

2. >>scanf ("%d %d %d", &x, &y, &z);
To get doubles use %g or %f. %d is an int.

>>void double get_big(double c, double d, double e)
do you mean void get_big(...) or double get_big(...)

>>get_sum(x, y, z, s);
>>double get_sum(double, double , double, double);
>>double get_sum(double a, double b, double sum) {
You prototype get_sum as taking 4 doubles, and you pass it 4 doubles, but you have the function as taking 3 doubles. You pass s to get_sum, but the value will not change when it returns. You need to pass the address and use a pointer if you want to do it that way. If you just want to return the value then do it like this:

Code:
#include <stdio.h>
double get_sum(double, double, double);
void get_big(double, double, double);
main()
{
double x, y, z,s;
printf ("please enter x y and z"\n)
scanf ("%g %g %g", &x, &y, &z);
s = get_sum(x, y, z);
printf("the sum is %g", s);
get_big(x, y, z);
}
double get_sum(double a, double b, double c)
{
double sum;
return a + b +c
}
void get_big(double c, double d, double e)
{
if (c > d && c > e)
printf ("%g is the biggest\n",c);
if (d > c && d> e)
printf ("%g is the biggest\n",d);
else
printf("%g is the biggest\n",e);
return;
}

>>if (c > d & c > e)
Do you mean
Code:
if(c>d && c>e)
- Sean

3. I believe this is what you were trying to do:
Code:
#include <stdio.h>

static double get_sum(double, double, double);
static double get_big(double, double, double);

int main ( void )
{
double x, y, z;
printf ("please enter x y and z\n");
if ( scanf ("%lf %lf %lf", &x, &y, &z) == 3 ) {
printf("The sum is %f\n", get_sum(x, y, z));
printf("The largest value is %f\n", get_big(x, y, z));
}
return 0;
}

double get_sum(double a, double b, double c)
{
return (a + b + c);
}

double get_big(double c, double d, double e)
{
double biggest = c;
if ( biggest < d ) biggest = d;
if ( biggest < e ) biggest = e;
return biggest;
}

Still debating on whether or not to go into a rant about floating-point arithmetic.
[/edit]

-Prelude

4. why are you making the function protoypes static? Is there any particular reason or just looks better?

*Never did understand why people put static on non needed functions*

5. >why are you making the function protoypes static? Is there any particular reason or just looks better?
Functions in C have a default global visibility, which has been conclusively and repeatedly shown to be a mistake. So to keep my programs neay and tidy, I give my functions the most limited visibility possible and declare them as static if they will not be accessed outside of that source file.

-Prelude