Oh thanks i forgot to add that part into the code.
I'm still having trouble though passing the value of the average.
This is a discussion on Passing Values From Functions within the C Programming forums, part of the General Programming Boards category; Oh thanks i forgot to add that part into the code. I'm still having trouble though passing the value of ...
Oh thanks i forgot to add that part into the code.
I'm still having trouble though passing the value of the average.
post code and error msg.
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); }
double avg_function (double array[], int numValues)
Missing the semi-colon in the proto-type.
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.
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?
Maybe you should try and write the function and post it. Where are the values supposed to come from?But how does the "double avg_function (void)" know to use the values inputted from the array?
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
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.Maybe you should try and write the function and post it. Where are the values supposed to come from?
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".