My problem is to make a program that stores a High temperature and a Low temperature within a two dimensional array and then displays the Average. I can get the numbers to be stored(I think, but the average doesn't work at all it seems to display the last number entered in the 7th temperature. Could someone help me out and show me what I did wrong?

``````#include <iostream>
#include <cmath>

using namespace std;

//******function prototypes********
void getTempsAvg(int xTemps);

int main ()
{
//display array
int temps = {0};

getTempsAvg(temps);

system("pause");
return 0;

}

//*****Functions*****
void getTempsAvg(int xTemps)
{
int temperatures = 0;

const int DAYS = 7;
const int TEMPS = 2;

for (int days = 0; days < 7; days ++)

{
cout << "Enter High Temp for day "
<<  days + 1 << ": ";
cin >> xTemps[DAYS][TEMPS];

cout << "Enter Low Temp for day "
<<  days + 1 << ": ";
cin >> xTemps[DAYS][TEMPS];
} //end for

xTemps[DAYS][TEMPS] / 7;
cout << "Average temperatures " << xTemps[DAYS][TEMPS] << endl;
}``````

You should definitely separate the user input from the computations. An added benefit of doing this is that you can hard code some values for testing purposes. Also, your function should return something and then you should output the answer from main.

crap! Sorry, I did something and the entry truncated.

...more loop action...
for ( int i = 0; i < DAYS; i++)
for ( j = 0; j < TEMPS; j++)

This way you can enter 2 temps for each day.

-For the average, you can pass …

## All 7 Replies

You should definitely separate the user input from the computations. An added benefit of doing this is that you can hard code some values for testing purposes. Also, your function should return something and then you should output the answer from main.

Okay so I made it a value returning function. I tried to make it an average, but i am not sure how to actually make the numbers in the two-dimensional array only add the high numbers to each other and the same for the low numbers. (Revised code)

``````#include <iostream>
#include <cmath>

using namespace std;

//******function prototypes********
int getTempsAvg(int xTemps);

int main ()
{
//declare variables
int temperatures = 0;

//declare array
int temps = {0};

getTempsAvg(temps);

temperatures = getTempsAvg(temps);

system("pause");
return 0;

}

//*****Functions*****
int getTempsAvg(int xTemps)
{
int temperatures = 0;

const int DAYS = 7;
const int TEMPS = 2;

for (int days = 0; days < 7; days ++)

{
cout << "Enter High Temp for day "
<<  days + 1 << ": ";
cin >> xTemps[DAYS][TEMPS];

cout << "Enter Low Temp for day "
<<  days + 1 << ": ";
cin >> xTemps[DAYS][TEMPS];
//end for
}
temperatures = xTemps[DAYS][TEMPS];
return temperatures;
}``````

Say I was to look for each number in the array so for high I would do ,,  ect all the way to 7 and for low temperature I would use , ,  ect. is this correct? and If so How in the world would I do that?

For the hard coding purposes are you meaning to separate when I add my high and low temperatures?

``````const int DAYS = 7;
const int TEMPS = 2;

for (int days = 0; days < 7; days ++)
{
cout << "Enter High Temp for day "
<<  days + 1 << ": ";
cin >> xTemps[DAYS][TEMPS];    // This always loads xTemps --
// which does not exist.  Max is
// xTemps

cout << "Enter Low Temp for day "
<<  days + 1 << ": ";
cin >> xTemps[DAYS][TEMPS];    // Overwrites the above.
//end for
}``````

Hi Vindal!

To clarify: the user enters the high and low temps for each day. The average you're looking for is of the 2 daily temp values? (or the average high & average low for the week?)

As prev mentioned, it's better to seperate the input from the computations. in main(), cin>> the temps, then pass them to the function. A couple of other things:

-Declare your array in main, along with the const int(s).

-In your function you have "cin >> xTemps[DAYS][TEMPS];" Given the prev declared const int(s), DAYS = 7, and TEMPS = 2. forever (it's that whole const thing :) )

-Since you're entering data into a 2d array, you'll need more loop action going on.

crap! Sorry, I did something and the entry truncated.

...more loop action...
for ( int i = 0; i < DAYS; i++)
for ( j = 0; j < TEMPS; j++)

This way you can enter 2 temps for each day.

-For the average, you can pass the array into the function, use loops to step through it, adding  +  and / by 2.

You'll need to either store or cout<< the daily average (unless it's weekly averages you're going for.

I hope this helps you! I'm new at posting, but I've written a few of these, so I know it works. Hopefully I was clear in my suggestions. If not, just yell!

commented: teehee +6

Hi there,

If I were you, I'd split the program by making two functions, one that reads in the temperatures and another that gets the average. (I'm assuming that you want to average the high temps. across the week and the same for the low temps, if you want to find the average of the high and low each day then you'll have to swap things about a bit, but the principle is the same). I think that you've tried to do this in your example, but you have used the same function ( `getTempsAvg(int *)` ) twice. So you want a main program that looks something like:

``````int main(){
int temperatures;

/* Read in the temperatures with a function */
getTemps(temperatures);

/* Average the temperatures with another function */
double aveHighTemperature, aveLowTemperature;
aveTemps(temperatures, &aveHighTemperatures, &aveLowTemperatures);

/* Output the averages */
std::cout << "Ave. high = " << aveHighTemperature
<< "Ave. low = " << aveLowTemperature << std::endl;

return 0;
}``````

OK, so then you need functions called `getTemps(int **)` that does things with `std::cin` to get the values, like:

``````void getTemps(int **temperatures){
for(unsigned i = 0; i < 6; i++){
std::cin >> temperatures[i];
std::cin >> temperatures[i];
}
}``````

You also need `aveTemps(int **, double &, double &)` :

``````void aveTemps(int **temperatures, double &high, double &low){
high = 0;
low = 0;
for(unsigned i = 0; i < 7; i++){
low +=  temperatures[i];
high += temperatures[i];
}
}``````

So, these two functions should read in the data and average it. I haven't compiled it, so it might need a bit of tweaking to get rid of typos and other small errors and such.

Also, notice that I have swapped the array dimensions (using `` instead of `` , this means that you can write a more general function that just averages the values in an array and then just pass it `temperatures` and `temperatures` , which I actually like more since it has more potential re-usability. (in general, you'd write a function called `double average(int *, unsigned)` , where the second argument is the size of the array, or at least the number of elements that you want to average over)

Hope that helps.

Thank you everyone that posted on this thread:
Walt, thank you for showing me where I was messing up with my code and giving a visual example.
Christina, Thank you for your explanations of different things I could have done my program with two for statements in one function and that way did not completely set in it and I have seen it in my book and did try it, but it made me enter too many numbers so I did not use this.
Ravenous, Thank you for your visual example along with the worded examples; you did most of the code that I wrote, but if I was asked to do something like this again I would be able to do it easily because that is how I learn so thanks a lot!

``````#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

//******function prototypes********
void getTemps(int xTemps);
void avgTemps(int xTempsX, double &high, double &low);

int main ()
{

//declare variables
int temperatures = 0;
const int DAYS = 7;
const int TEMPS = 2;

//declare array
int temps[TEMPS][DAYS] = {0};

//asks for high and low temperatures
getTemps(temps);
//sets the two averages
double avgHigh, avgLow;
//calculates the average of the two arrays
avgTemps(temps, avgHigh, avgLow);

system("pause");
return 0;

}

//*****Functions*****
void getTemps(int xTemps)
{
for(int i = 0; i < 7; i++)
{
cout << "Enter High Temperature for the day: ";
cin >> xTemps[i];
cout << "Enter Low Temperature for the day: ";
cin >> xTemps[i];
}
}

void avgTemps(int xTempsX, double &high, double &low)
{
high = 0;
low = 0;

for(int j = 0; j < 7; j++)
{
high += xTempsX[j];

low += xTempsX[j];

}
high = high / 7;
low = low / 7;

cout << fixed << setprecision(0);

cout << "High Average is: " << high << endl;
cout << "Low Average is: " << low << endl;

}``````

There is my code it works perfectly and anyone can use this to learn from it.

Be a part of the DaniWeb community

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