What am I doing wrong?

Im working on a assignment and it asks to find the lowest, highest and average temperatures...and it gives a list ten temperatures that it wants me to run to see if it works properly....Ive been able to get the correct low and high temperature but im having difficulties getting the average of the temperatures.. Can someone please help ive tried many things and im pretty much out of ideas... Thx


int main()

{
const int MAXTEMPS = 10;

float temps[MAXTEMPS];

int highest = -9999;

int lowest = 9999;

int x, avg;





cout << "Enter temperatures until MAXTEMPS is reached: " << endl;
cin >> temps[MAXTEMPS];





for(x = 1; x <= 9; x++)

{



cin >> temps[MAXTEMPS];



if(temps[MAXTEMPS] > highest){

highest = temps[MAXTEMPS];
}


if(temps[MAXTEMPS] < lowest){

lowest = temps[MAXTEMPS];
}

}
avg = 0;
for(x=0; x<10; x++)

avg += temps[MAXTEMPS];
avg /= 10;



cout << "Average: " << avg << endl;
cout << "Highest: " << highest << endl;
cout << "Lowest: " << lowest << endl;

system("pause");
return 0;

}
Make avg a double and initialize it to 0.0.

Then when you divide, divide by 10.0.

Division assumes that there's a possibility of a remainder. If the variable is an int, it won't retain the remainder (decimal).

I hope that helps.
It still doesent seem to work... For average i keep getting whatever number is the lowest number.
try:
1
2
3
4
5
6
7
avg = 0.0;
for(x=0; x<10; x++)
{
avg += temps[MAXTEMPS];
}

avg /= 10.0;


let me know what happens when you do that.
Im still getting the lowest number for average....These are the numbers im entering..
75, 65, 41, 54, 66, 71, 78, 81, 55, 39............and im getting 39 on avg each time
OH! change all the MAXTEMPS you have in the arrays to x... that should really help... lol
lol the reason why i have MAXTEMPS is because our instructor wants us to use MAXTEMPS in the arrays
Once you declare the array, you don't use MAXTEMPS in your arrays.

Because once its declared, temps[10] doesn't exist.
This is what I have so far but im having one more problem... When I enter either the highest number or lowest number first it skips it and gives me the following lowest or greatest number...For example if i enter these numbers in this order 1, 23, 34, 56, 83, 5, 45, 56, 32... It will skip 1 as my lowest number and say 5 is the lowest number.... Can someone tell me what im doing wrong..thx



#include <iostream>

using namespace std;



int main()

{

const int MAXTEMPS = 10;

float temps[MAXTEMPS];

int highest = -9999;

int lowest = 9999;

int x;

double avg;







cout << "Enter temperatures until MAXTEMPS is reached: " << endl;

cin >> temps[x];



for(x = 1; x <= 9; x++)

{


cin >> temps[x];


if(temps[x] > highest){

highest = temps[x];

}


if(temps[x] < lowest){

lowest = temps[x];

}

}

avg = 0.0;

for(x=0; x<10; x++)



avg += temps[x];



avg /= 10.0;



cout << "Average: " << avg << " temperature" << endl;

cout << "Highest: " << highest << " temperature" << endl;

cout << "Lowest: " << lowest << " temperature" << endl;

system("pause");

return 0;

}
take out the cin right ofter the first cout.
if you take out the cin after the first cout you also have to remember to set your first for loop to start x at 0 other wise you will only be using the last 9 indexes of the array. The reason that the cin being outside the for can cause you problems is it relies on an assumption that x will initialize to zero. and while most often this is true and in a small program like this where x is not used anywhere else it might not cause a problem.

however when you start creating more complex programs where variables are recycled for different uses throughout the execution of the program x might have picked up another value, in which case you would be telling the computer to store a temp at a random location within the array or possibly out of rang of the array

Also i've noticed in your first for statement that you are using an less than or equal to comparator. While there is nothing wrong with this just keep in mind that when working with arrays your parameters on a for loop can be x=0; x<(the number of slots in the array) since the array starts at zero you will not actually be addressing anything outside of the array.
Last edited on
I took it out but now it messed up the average outcome again lol... its gives a very long number for average now lol
Nevermind it worked...You guys are great...thx a bunch
Not a problem good sir
Topic archived. No new replies allowed.