hi, i need help with how to find the max number and min number from an array. there are 31 numbers, i know that i am suppose to use a for loop to do this and this is what i got so far
array is inputed by user

#include <iostream.h>
int main ()
{
   int temp[31];
   for(int i=0;i<31;i++)
   {
       cout<<"ENTER THE TEMPERATURE FOR THE MONTH OF DECEMBER"<<endl;
       cin>>temp[i];
    }
   for(i=0;i<31;i++)
   {
      if(temp[i]>temp[i+1])

this is where im stuck, i don't know what to do next. i need to find the max and min so now i just need to find a way to swap the order of the array so later on i can just cout the first and last element of the array to show the max and min.

Recommended Answers

All 3 Replies

What I would do is this instead of the "read" loop:

int tmax, tmin;

tmax=temp[0];
tmin=temp[0];

for(int i=0; i<31; i++){
    if(temp[i]>tmax){
        tmax=temp[i];
    }
    if(temp[i]<tmin){
        tmin=temp[i];
    }
}
cout<<"The maximum temperature is "<<tmax<<".\n";
cout<<"The minimum temperature is "<<tmin<<".\n";

Do you see where I'm going with this?

Writing an array-sorting algorithm has been the bane of intro-C++ courses since the language was invented, I think. Being a hobbyist, I try to avoid having to sort. If you just need the max and min, what I posted should work fine. If not, ask someone more experienced.

thanks, i understand

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.