943,748 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6552
  • C++ RSS
Mar 8th, 2009
0

Min/Max value and average array help!!

Expand Post »
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-

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lenny19 is offline Offline
4 posts
since Mar 2009
Mar 8th, 2009
1

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

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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 8th, 2009
0

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

Thanks for your help! Its all working now
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lenny19 is offline Offline
4 posts
since Mar 2009
Jul 28th, 2009
0

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

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 ..................
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gurdeep kaur is offline Offline
2 posts
since Jul 2009
Jul 28th, 2009
1

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

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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Nov 23rd, 2009
0
Re: Min/Max value and average array help!!
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
momike205 is offline Offline
4 posts
since Nov 2009
Nov 23rd, 2009
2
Re: Min/Max value and average array help!!
>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?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 23rd, 2009
0
Re: Min/Max value and average array help!!
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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
momike205 is offline Offline
4 posts
since Nov 2009
Nov 23rd, 2009
2
Re: Min/Max value and average array help!!
>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; Nov 23rd, 2009 at 4:50 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 16th, 2010
0

Help

Hello
please ;
help me to write a program for finding maximum number in array ?
send for me source code in C++.thanks a lot.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saeed67 is offline Offline
1 posts
since Oct 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problems about Cimg
Next Thread in C++ Forum Timeline: pointer problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC