Hello guys -- I have problem. There is specific function within my code that I'm having a hard time reconfiguring. In the "display_temps" function, I want to print a "+" beside the temp if it is higher than the average of the temperatures. I know that it's because of my comparison of the array/pointer with the int variable avg but I don't know what step to take in order to get it to do what I want. can anyone help.
**my code compiles, but only with incorrect results**
Code:#include <stdio.h> #include <stdlib.h> //Function Prototype void printHeading (); void input_temps( int temp_array[], int n ); void display_temps( int temps[], int num, int no_of_temps ); void calculate_avg( int temps[], int no_of_temps ); int main() { int* temps, count; int no_of_temps, no_of_bytes; printHeading (); printf("How many days did you collect temperatures in a month: "); scanf("%d", &no_of_temps); no_of_bytes = no_of_temps*sizeof(int); temps = (int*)malloc(no_of_bytes); input_temps(temps, no_of_temps); calculate_avg(temps, no_of_temps); display_temps(temps, no_of_temps, no_of_temps); } //******************************** // Temp. input function //******************************** void input_temps(int temp_array[], int n) { int i; printf("\n\nEnter the %d temperatures:\n\n", n); for (i=1; i<=n; i++) scanf("%d", &temp_array[i]); } //*************************************** // Display temp. function //*************************************** void display_temps( int temps[], int num, int no_of_temps ) { int i, total; int avg; for ( i = 1 ; i <= num ; i++ ) { //printf("\n\n\t%d \t%d", i , temps[i]); total += temps[i]; avg = ( total / no_of_temps ); if ( avg < temps[i] ) printf("\n\n\t%d \t%d +", i, temps[i]); else { printf("\n\n\t%d \t%d ", i, temps[i]); } } } //*************************************** // Calculate avg. temp //*************************************** void calculate_avg( int *temps, int no_of_temps) { int i, total; float avg; total = 0; for( i = 1; i <= no_of_temps; i++) { total += temps[i]; } avg = ( total / no_of_temps); printf("\nThe average temperature is %.2f" , avg); }



LinkBack URL
About LinkBacks



