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

## Recommended Answers

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 …``

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.

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

## All 9 Replies

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';``````

Thanks for your help! Its all working now

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

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!

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

mmm... k...
So if I have this:

``````int i, max, min,;
int num;

num = 10;
num=3;
num=75;
num=0;
num=1;
num=56;
num=100;
num=12;
num= -19;
num=88;

min=max=num;

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

Now, since `num=10`, im telling it:

`````` if (num < 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??

>but here's the thing num[3, 4 and 8] are smaller than 10...
Yes, num, num, and num 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, you'll replace the value from 3 to 0. And when you get to num (skipping over num 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.

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

Be a part of the DaniWeb community

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