954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Min/Max value and average array help!!

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-

int maxtermtemp;
	int mintermtemp;
	int sum=0;
	int average=0;
	cout<<"\nThis is Terminal\n";
	for(int count=0;count<12;count++)								//Input Terminal Temperatures
	{
		cout<<"Enter temp for "<<month[count]<<"=";
		cin>> temp[count];
	}

	for(count=0;count<12;count++)									//Maximum Terminal Temperature
	{
		if (maxtermtemp<temp[count])
			maxtermtemp=temp[count];
	}
	cout<<"\nThe maximum temperature for the terminal is "<< maxtermtemp <<" degrees.\n\n";

	for(count=0;count<12;count++)									//Minimum Terminal Temperature
	{
		if(mintermtemp>temp[count])
			mintermtemp=temp[count];
	}
	cout<<"\nThe minimum temperature for the terminal is "<<mintermtemp<<" degrees.\n\n";

	for(count=0;count<12;count++)									//Average Terminal Temperature
	{
		sum= sum + temp[count];
		average=sum/12;
		cout<<"Average: "<<average;
	}


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

Lenny19
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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:

for ( int i = 0; i < 12; i++ ) {
  sum += temp[i];
}

average = sum / 12;
cout<<"Average: "<< sum <<'\n';
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks for your help! Its all working now

Lenny19
Newbie Poster
4 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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 ..................

gurdeep kaur
Newbie Poster
2 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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!

momike205
Newbie Poster
4 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

>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?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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??

momike205
Newbie Poster
4 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Hello
please ;
help me to write a program for finding maximum number in array ?
send for me source code in C++.thanks a lot.

saeed67
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You