Min/Max value and average array help!!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 4
Reputation: Lenny19 is an unknown quantity at this point 
Solved Threads: 0
Lenny19 Lenny19 is offline Offline
Newbie Poster

Min/Max value and average array help!!

 
0
  #1
Mar 8th, 2009
Hi,
I'm got to input 12 values and find the maximum, minimum and average value, i've
done the maximum, but can't do the min or average, i've spent a couple of hours trawling the web and looking over the
code but nothing seems to work
heres the part of the code i'm having trouble with-

  1. int maxtermtemp;
  2. int mintermtemp;
  3. int sum=0;
  4. int average=0;
  5. cout<<"\nThis is Terminal\n";
  6. for(int count=0;count<12;count++) //Input Terminal Temperatures
  7. {
  8. cout<<"Enter temp for "<<month[count]<<"=";
  9. cin>> temp[count];
  10. }
  11.  
  12. for(count=0;count<12;count++) //Maximum Terminal Temperature
  13. {
  14. if (maxtermtemp<temp[count])
  15. maxtermtemp=temp[count];
  16. }
  17. cout<<"\nThe maximum temperature for the terminal is "<< maxtermtemp <<" degrees.\n\n";
  18.  
  19. for(count=0;count<12;count++) //Minimum Terminal Temperature
  20. {
  21. if(mintermtemp>temp[count])
  22. mintermtemp=temp[count];
  23. }
  24. cout<<"\nThe minimum temperature for the terminal is "<<mintermtemp<<" degrees.\n\n";
  25.  
  26. for(count=0;count<12;count++) //Average Terminal Temperature
  27. {
  28. sum= sum + temp[count];
  29. average=sum/12;
  30. cout<<"Average: "<<average;
  31. }



The maximum value is correct, but
the minimum value is about negative 9 million,
and the average comes out several times, with ascending values,

any help would be really appreciated!!!

Thanks

Lenny
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Min/Max value and average array help!!

 
1
  #2
Mar 8th, 2009
Both your min and max variables are uninitialized. Set them both to the first item in the array. As for the average problem, you're printing the average inside the loop rather than after it completes. The loop should only be summing the items:
  1. for ( int i = 0; i < 12; i++ ) {
  2. sum += temp[i];
  3. }
  4.  
  5. average = sum / 12;
  6. cout<<"Average: "<< sum <<'\n';
Last edited by Narue; Mar 8th, 2009 at 11:29 am.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 4
Reputation: Lenny19 is an unknown quantity at this point 
Solved Threads: 0
Lenny19 Lenny19 is offline Offline
Newbie Poster

Re: Min/Max value and average array help!!

 
0
  #3
Mar 8th, 2009
Thanks for your help! Its all working now
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: gurdeep kaur is an unknown quantity at this point 
Solved Threads: 0
gurdeep kaur gurdeep kaur is offline Offline
Newbie Poster

Re: Min/Max value and average array help!!

 
0
  #4
Jul 28th, 2009
1. Write a function called MaxMinAvg in C that takes an integer array as argument and finds the maximum, minimum and the average value of the array elements.

The function signature in C is as follows:

void MaxMinAvg(int arr[], int * max, int * min, int * avg);

Write a main function to test your code with different input valuesto demonstrate that your function is robust.

plz help me quickly ..................
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Min/Max value and average array help!!

 
1
  #5
Jul 28th, 2009
gurdeep kaur, how does your post help Lenny19 with his problem? Or do you have a new question which needs to be asked in it's own thread?

Before starting your thread, read some of the posts that explain how to post your question properly, which includes The Rules.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: momike205 is an unknown quantity at this point 
Solved Threads: 0
momike205 momike205 is offline Offline
Newbie Poster
 
0
  #6
30 Days Ago
Narue you told Larry19 to "Set them both (min and max) to the first item in the array."

I don't understand the theory behind this step?? Why r u setting them both to the first item in the array?

What if it finds 2 values smaller, does it choose the smallest one automatically?????

Thanks in advance for your help!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch
 
2
  #7
30 Days Ago
>I don't understand the theory behind this step??
>Why r u setting them both to the first item in the array?

Why not? The algorithm starts at the beginning and moves to the end. The min and max variables represent the smallest and largest values encountered so far. If the only item we've encountered is the first one, doesn't it make sense that that item is both the smallest and the largest?
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 4
Reputation: momike205 is an unknown quantity at this point 
Solved Threads: 0
momike205 momike205 is offline Offline
Newbie Poster
 
0
  #8
30 Days Ago
mmm... k...
So if I have this:

int i, max, min,;
int num[10];

num[0] = 10;
num[1]=3;
num[2]=75;
num[3]=0;
num[4]=1;
num[5]=56;
num[6]=100;
num[7]=12;
num[8]= -19;
num[9]=88;

min=max=num[0];

for (i=1; i<10; i++) {
if (num[i] < min) min = num[i];
if (num[i] > max) max = num[i];

Now, since num[0]=10, im telling it:
if (num[1] < 10) min = 3;
but here's the thing num[3, 4 and 8] are smaller than 10... So it automatically chooses the smallest value and disregards the previous one??
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch
 
2
  #9
30 Days Ago
>but here's the thing num[3, 4 and 8] are smaller than 10...
Yes, num[3], num[4], and num[8] are indeed smaller than 10. But that's quite irrelevant at that point because you've changed the value in min from 10 to 3. When you get to num[3], you'll replace the value from 3 to 0. And when you get to num[8] (skipping over num[4] because 0 is less than 1), you'll replace the value from 0 to -19. At the end of the loop, min will have a value of -19, which is correct.
Last edited by Narue; 30 Days Ago at 4:50 pm.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 1795 | Replies: 8
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC