| | |
Min/Max value and average array help!!
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 4
Reputation:
Solved Threads: 0
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-
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
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)
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
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)
for ( int i = 0; i < 12; i++ ) { sum += temp[i]; } average = sum / 12; cout<<"Average: "<< sum <<'\n';
Last edited by Narue; Mar 8th, 2009 at 11:29 am.
New members chased away this month: 5
•
•
Join Date: Jul 2009
Posts: 2
Reputation:
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 ..................
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2009
Posts: 4
Reputation:
Solved Threads: 0
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!
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!
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?
>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
•
•
Join Date: Nov 2009
Posts: 4
Reputation:
Solved Threads: 0
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??
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??
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.
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
![]() |
Similar Threads
- Import scores.txt into array and make calculations (C++)
- Please Help!! (C++)
- Finding the percentage of unique numbers in an array. (C++)
- numpy + MA, problem with min and max on masked arrays (Python)
- Array (C++)
- Array (C++)
- Building Arrays (Java)
- Homework Help. (C++)
- home work help funcations and arry issues (C)
Other Threads in the C++ Forum
- Previous Thread: Sorting a vector
- Next Thread: Class help!
Views: 1795 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






