Thread: coding help? (functions)

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    10

    coding help? (functions)

    i wrote up a program using functions to calculate a sum and product. I looked it over and it looks right to me, but im not getting the desired values...

    Code:
    #include <stdio.h>
    #define MAX_ELEMS 100
    
    void SumAndProduct (double[], int, double, double);
    
    int main()
    {
         double arr[MAX_ELEMS];
         int count = 0;
         double val, sum, product;
         printf("Enter array values (or 0.0 to stop): ");
         do
         {
                scanf("%lf", &val);
                if(val!=0.0)
                {
                       arr[count]=val;
                       count++;
                } 
          } while( val != 0.0 && count < MAX_ELEMS);
          SumAndProduct(arr, count , sum, product);
          printf("The sum is %f and the product is %f. \n", sum, product);
          return 0;
    }
    
    void SumAndProduct( double a[], int len, double sum, double prod)
    {
           int i;
           prod = 1.0;
           sum = 0.0;
           for(i=0; i< len;i++)
           {
                  sum = sum + a[i];
                  prod = prod * a[i];
           }
    }
    not really sure where i went wrong, thanks for the help.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I was writing up a reply but I ended up losing it.

    Your program logic is correct, but the technical details regarding function parameters are what is causing the incorrect output.

    When you pass a variable to a function, only a copy of the variable is passed. You are really just transferring the value of the variable, not the variable itself. This means if you pass an int to a function with a value of 2, your function will receive a new variable with the value of 2. You can change the variable inside the function, but the variable in the calling function will not be changed.

    To remedy this, you have to use pointers. Pass the address of the variable that you want to change to the function in question. Define the function to receive a pointer instead of a regular variable. Alter the value that the pointer points to.

    So like this:

    Code:
    #include <stdio.h>
    #define MAX_ELEMS 100
    
    void SumAndProduct (double[], int, double *, double *);
    
    int main()
    {
         double arr[MAX_ELEMS];
         int count = 0;
         double val, sum, product;
         printf("Enter array values (or 0.0 to stop): ");
         do
         {
                scanf("%lf", &val);
                if(val!=0.0)
                {
                       arr[count]=val;
                       count++;
                } 
          } while( val != 0.0 && count < MAX_ELEMS);
          SumAndProduct(arr, count , &sum, &product);
          printf("The sum is %f and the product is %f. \n", sum, product);
          return 0;
    }
    
    void SumAndProduct( double a[], int len, double *sum, double *prod)
    {
           int i;
           *prod = 1.0;
           *sum = 0.0;
           for(i=0; i< len;i++)
           {
                  *sum = *sum + a[i];
                  *prod = *prod * a[i];
           }
    }

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    10
    hey thanks alot! learn something new everyday haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM