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