Normaly you would create an array to hold the values which you wan't the user to put in ie:
Code:
#include <stdio.h>
#define SIZE 10 /* you should use 10 temperatures */
#define FREEZING 32.0 /* temperature for freezing point */
/* will ask user for 10 temperatures, returns number of reads above FREEZING */
int read_temperatures(double* arr, int size);
/* temperatures are usualy as doubles, thus a average will be double aswell
* it will only average temperatures above FREEZING */
double calculate_average (double* arr, int size);
/* simple test function to display the temperatures read */
int print_temperatures(double* arr, int size);
int main()
{
double temperatures[SIZE], average_temp=0.0;
int count_positive=0;
if(!(count_positive = read_temperatures(temperatures, SIZE)))
printf("Error no temperatures above freezing point (%.2f) added\n",
FREEZING);
else
if(!(average_temp = calculate_average(temperatures, SIZE)))
printf("Error calculating average temperature\n");
/* testing bed */
if(!print_temperatures(temperatures, SIZE))
printf("Error printing temperatures\n");
if(count_positive)
{
printf("Positive temps: %d\n", count_positive);
printf("Average temp: %.2f\n", average_temp);
}
return 0;
}
int read_temperatures(double* arr, int size)
{
double value=0;
int j, res=0;
for(j=0; j<size; ++j)
{
printf("Temperature %d: ", j+1);
fflush(stdout);
if(0 <= scanf("%lf", &value))
{
if(value > FREEZING)
++res;
arr[j]=value;
}
else
printf("Error reading input temperature %.2f\n", value);
}
return res;
}
double calculate_average(double* arr, int size)
{
int j;
double res=0.0;
for(j=0; j<size; ++j)
if(arr[j] > FREEZING)
res+=arr[j];
return (res/size);
}
int print_temperatures(double* arr, int size)
{
int j;
printf("[ ");
for(j=0; j<size; ++j)
printf("%.2f%c", arr[j], j<(size-1)?',':' ');
printf("]\n");
return j;
}
Now the readin function can be changed to fit your needs, I just made this to fit what ever I thought usefull.. I'm not trying to do your homework, but for one thing I would like to keep the submitted temperatures below freezingpoint.
Btw, it could be altered, so it would save the
below sub-temperature submitted at the end of the array, then a search through acceptable temperatures would be [0-count_positive] and a search through sub-temperatures would be [count_positive-SIZE] but in order to change the functions for calculation of average, it would need to have a starting point aswell.
Anywho, I'm not here to do your homework, so that would be somethign you could play with if you feel like it.. I'm guessing the next assignment will probably try to challenge you with something like that.
Oh, and in your example, the
Code:
for(i=1;i<=10;i++)
iTemp[i ]=0; {
if (i<32)
continue;
}
should have been
Code:
for(i=1;i<=10;i++)
{
iTemp[i ]=0;
if (i<32)
continue;
}
in order to activate your
continue statement, but anyway it won't be effective since you're comparing the i value to your 32, i will never exceed 10, and iTemp will consist of 0 values, and never get filled with any input from teh user, oh and take a look at my code for fitting to your needs, because your example won't work anyway.
//edit, I noticed the increment counter I frequently use fsck'ed the display up, it found that
[ i] should start italic display, even within the code tags.