Problem with finding middle value | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 ```

13th Oct 2020, 7:28 PM
Zoran Dori
Zoran Dori - avatar
3 Answers
+ 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.
13th Oct 2020, 7:46 PM
Coding Cat
Coding Cat - avatar
+ 2
Thomas Your solution works, thank you for answer, my mistake.
13th Oct 2020, 8:09 PM
Zoran Dori
Zoran Dori - avatar
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?
17th Oct 2020, 3:27 AM
LastSecond959
LastSecond959 - avatar