Ok, thanks that part works perfect.
Thank you for the suggestion to change my if statement to a while statement.
Those are the constraints because thats what the instructions say they need to be on my assignment.
Now I've expanded my code a little bit and I'm having a similar problem as the one before except I'm trying to pass a double instead of a integer.
I want to pass the value of the average onto another function that will decide how many values are above and below the value.
I want to pass the value with the function "double avg_function" but how is the function know to look at the array for the values it needs to calculate the average?
Code:
#include <stdio.h>
#include <stdlib.h>
int num_fahr_temps (void);
double avg_function (void);
void input_fahr_temps (double array [], int numValues);
void output_temps (double array [], int numValues);
void avg_high_low (double array [], int numValues);
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 (void)
{
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);
}