-
Array sorting help
Write a program that asks the user the daily temperature at noon over the course of one month. Store the values in an array. At the end of the program, display the highest and lowest temperature, as well as the average temperature over the course of the month.
I tried finding the highest temperature using if/else but for some reason high is always the sum of the arrays. Any help will be greatly appreciated thanks alot in advance!
ps. replaced 30 with 3
Code:
#include <iostream>
using namespace std;
int main ()
{
int days[3],sum=0;
double avg;
double high;
cout<<"Enter noon temperatures for 3 days\n";
for (int a=0;a<3;a++)
{
cin>>days[a];
sum=sum+days[a];
if (days[a+1]>days[a])
high=days[a+1];
else
high=days[a];
}
avg=sum/3;
cout<<"The average temperature is"<<avg<<endl;
cout<<"The highest temperature is"<<high<<endl;
return 0;
}
-
For one thing, you mistakenly compare days[a + 1] > days[a]. Now, you only ever input into days[a] so days[a + 1] is always garbage. Even if you fix that though the code will still be wrong because in order to find the maximum, you have to make a guess for the high, and compare the value of high to the rest of the elements, assigning higher elements to high as you go.