max and min

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2006
Posts: 22
Reputation: mr.cool is an unknown quantity at this point 
Solved Threads: 1
mr.cool mr.cool is offline Offline
Newbie Poster

max and min

 
0
  #1
Jan 7th, 2008
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
  1. #include <iostream.h>
  2. int main ()
  3. {
  4. int temp[31];
  5. for(int i=0;i<31;i++)
  6. {
  7. cout<<"ENTER THE TEMPERATURE FOR THE MONTH OF DECEMBER"<<endl;
  8. cin>>temp[i];
  9. }
  10. for(i=0;i<31;i++)
  11. {
  12. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 143
Reputation: venomlash is an unknown quantity at this point 
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

Re: max and min

 
0
  #2
Jan 7th, 2008
What I would do is this instead of the "read" loop:

  1. int tmax, tmin;
  2.  
  3. tmax=temp[0];
  4. tmin=temp[0];
  5.  
  6. for(int i=0; i<31; i++){
  7. if(temp[i]>tmax){
  8. tmax=temp[i];
  9. }
  10. if(temp[i]<tmin){
  11. tmin=temp[i];
  12. }
  13. }
  14. cout<<"The maximum temperature is "<<tmax<<".\n";
  15. cout<<"The minimum temperature is "<<tmin<<".\n";
Do you see where I'm going with this?
Last edited by venomlash; Jan 7th, 2008 at 9:41 pm. Reason: Clarify
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 143
Reputation: venomlash is an unknown quantity at this point 
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

Re: max and min

 
0
  #3
Jan 7th, 2008
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.
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: mr.cool is an unknown quantity at this point 
Solved Threads: 1
mr.cool mr.cool is offline Offline
Newbie Poster

Re: max and min

 
0
  #4
Jan 7th, 2008
thanks, i understand
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC