Thread: Passing Values From Functions

  1. #16
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Oh thanks i forgot to add that part into the code.

    I'm still having trouble though passing the value of the average.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    post code and error msg.

  3. #18
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Error:'avg_function' undefined; assuming extern returning int

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int num_fahr_temps (void);
    double avg_function (double array[], int numValues)
    void input_fahr_temps (double array [], int numValues);
    void output_temps (double array [], int numValues);
    void avg_high_low (double array [], int numValues);
    void abo_bel_equ (double array [], int numValues, double avg);
    
    int main (void)
    
    {
        double array [25], avg;
        int numValues;
     
        numValues = num_fahr_temps ();
        avg = avg_function ();
     
        input_fahr_temps (array, numValues); 
        output_temps (array, numValues);
        avg_high_low (array, numValues);
        abo_bel_equ (array, numValues, avg);
     
        return 0;    
    }
     
    int num_fahr_temps (void)
    {
        int numValues;
     
        printf ("\n        CIS 231 - Assignment 3 - Brian Avrit \n\n");
     
        printf ("How many fahrenheit tempetures would you like to enter (1-25) : ");
        scanf ("%i", &numValues);
     
        while ((numValues < 1 ) || (numValues > 25))
        {
            printf ("\nValue is out of range, please re-enter another value : ");
            scanf ("%i", &numValues);
        }
     
        return numValues;
    }
     
    void input_fahr_temps (double array[], int numValues)
    {
        int i;
     
        for (i = 0; i < numValues; i++)
        {
            printf ("\nEnter value for fahrenheit %i: ", i);
            scanf ("%lf", &array[i]);
            while ((array[i] < -175.0) || (array[i] > 175.0)) 
            {
                printf ("\nValue is out of range,  please re-enter: ");
                scanf ("%lf", &array[i]);
             }
        }    
    }
     
    void output_temps (double array[], int numValues)
    {
        int i;
     
        printf ("Fahr\n");
     
        for (i = 0; i <numValues; i++)
            printf ("%.1lf\n", array [i]);
    }
     
    void avg_high_low (double array [], int numValues)
    {
        int i;
        double avg, high, low, sum = 0;
         
        high = array[0];
        low = array[0];
     
        for (i = 0; i < numValues; i++)
            sum = sum + array [i];
            avg = sum / numValues;
     
        for (i = 0; i < numValues; i++)
            if (array[i] < low) 
                low = array[i]; 
        for (i = 0; i < numValues; i++)
            if (array[i] > high)
                high = array[i];
     
        printf ("\nAverage: %.1lf\n", avg);
        printf ("\nHigh: %.1lf\n", high);
        printf ("\nLow: %.1lf\n", low);
    }
     
    double avg_function (double array[], int numValues){
    
        int i;
        double avg;
     
        for (i = 0; i < numValues; i++)
            sum = sum + array [i];
            avg = sum / numValues;
     
        return avg;
    }
     
    void abo_bel_equ (double array[], int numValues, double avg)
    {
        int i, abo = 0, bel = 0, equ = 0;
     
        for (i = 0; i < numValues; i++)
            if (array[i] > avg) 
                abo++;
                printf ("\nAbove Average: %i \n", abo); 
                 
        for (i = 0; i < numValues; i++)
            if (array[i] < avg) 
                bel++;
                printf ("Below to Average: %i \n",bel); 
     
        for (i = 0; i < numValues; i++)
            if (array[i] == avg) 
                equ++;
                printf ("Equal to Average: %i \n", equ); 
    }

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    double avg_function (double array[], int numValues)


    Missing the semi-colon in the proto-type.

  5. #20
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    This is where I get confused.

    To return the value of the numValues I had to do the function "int num_temps (void)". I'm assuming if I wanted to return the value of the average I would do the function "double avg_function (void)" not a "double avg_function (double array[], int numValues)" function. But how does the "double avg_function (void)" know to use the values inputted from the array?

    Sorry if that's confusing, but thats the best way I could describe it.

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by skmightymouse View Post
    To return the value of the numValues I had to do the function "int num_temps (void)". I'm assuming if I wanted to return the value of the average I would do the function "double avg_function (void)" not a "double avg_function (double array[], int numValues)" function.
    Why do you think that? If the function is supposed to compute the average of some values, it has to get them from somewhere, right?

    But how does the "double avg_function (void)" know to use the values inputted from the array?
    Maybe you should try and write the function and post it. Where are the values supposed to come from?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #22
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Maybe you should try and write the function and post it. Where are the values supposed to come from?
    Sorry I'm a little confused on what function I should try. Do you mean the "double avg_function (void)" function? Because if I do that then the function doesn't know the values of the array.

    If I do (void) avg_function (double array [], int numValues); I get an error from this line of code "avg = avg_function ()" in the main function. The error states that "there are too few arguements in function call".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  3. Beginner question: passing values between functions
    By merike in forum C Programming
    Replies: 5
    Last Post: 11-18-2006, 08:39 PM
  4. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM
  5. Passing Values to Functions
    By shad0w in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 08:28 PM