0
Problem with finding middle value
Hello, I'm starter in C, and I'm trying to find max and middle value of temperatures in n days of one month. My code: ``` #include <stdio.h> #include <stdlib.h> int main() { int i, n, a[35], max; float mid; printf("n= "); scanf("%d", &n); for(i=0;i<n;i++) { printf("a[%d]= ",i); scanf("%d",&a[i]); } max=a[0]; for(i=0;i<n;i++) if (a[i]>max) max=a[i]; printf("Max: %d\n", max); // for(i=0;i<n;i++) mid=a[i]/n; printf("Middle: %1.1f\n", mid); } ``` Problem is that result of middle value is "max temperature/n days". Middle value should be sum of all temperatures/n days. Example: For 5 days, temperatures are 27, 25, 18, 19, 30. Output should be: ``` Max: 30 Middle: 23.8 ``` But my output is: ``` Max: 30 Middle: 6.0 ```
3 Respuestas
+ 2
What are u doing?
mid = a[i] /n
in a loop! Would ever be the value of last day divided by n.
U must calc the sum of All days in this loop
sum = sum + a[i]
And after the loop divided by n
mid = sum/n
Try this and report if it works.
+ 2
Thomas Your solution works, thank you for answer, my mistake.
0
Btw, it's bad to declare 35 for the size of a when you're accessing 0 to n-1. What if your n is >35?