Array sorting help

This is a discussion on Array sorting help within the C++ Programming forums, part of the General Programming Boards category; Write a program that asks the user the daily temperature at noon over the course of one month. Store the ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    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;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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.
    Last edited by whiteflags; 04-16-2012 at 04:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array sorting Help
    By Daichi Sato in forum C Programming
    Replies: 4
    Last Post: 02-18-2012, 03:34 PM
  2. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. Array Sorting ???
    By Takis_ in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2008, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21