Thread: second version of old problem, help?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    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. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    >>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
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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;
    }
    [edit]
    Still debating on whether or not to go into a rant about floating-point arithmetic.
    [/edit]

    -Prelude
    My best code is written with the delete key.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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*
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. How to set File Version of VC++ 6 dll
    By mercury529 in forum Windows Programming
    Replies: 3
    Last Post: 12-08-2006, 02:49 PM
  3. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM