Hey Everyone,

I have some code here that I am trying to make. I made a two dimensional array that stores a high temperature and a low temperature then displays the average, but I am having a hard time getting it to display the Highest and lowest amount in the array. I have tried a bubble sort and it doesn't work properly so I was trying this:

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

using namespace std;

//******function prototypes********
void getTemps(int xTemps[2][7]);
void avgTemps(int xTempsX[2][7], double &high, double &low);
void showHighest(int xXTempsX[2][7], int &highestTemp, int &xHighestTemp);
void showLowest(int xXXTempsX[2][7], int &lowestTemp, int &xLowestTemp);

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);
	//shows the lowest Temperature
	int lowestTemp, xLowestTemp;
	showLowest(temps, lowestTemp, xLowestTemp);
	//shows the highest Temperature
	int highestTemp, xHighestTemp;
	showHighest(temps, highestTemp, xHighestTemp);
	
	

	system("pause");
	return 0;
	
}

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

void avgTemps(int xTempsX[2][7], double &high, double &low)
{
	high = 0;
	low = 0;

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

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

	cout << fixed << setprecision(0);

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

}

void showLowest(int xXXTempsX[2][7], int &lowestTemp, int &xLowestTemp)
{
	lowestTemp = 0;
	xLowestTemp = 0;

	while (lowestTemp <= 7)
	{
		if (xXXTempsX[1][lowestTemp] >= xLowestTemp)
		{
			xLowestTemp = xXXTempsX[1][lowestTemp];
		}

		lowestTemp++;
	}

	cout << "The Lowest Temp out of the seven days was: " << xLowestTemp << endl;
}

void showHighest(int xXTempsX[2][7], int &highestTemp, int &xHighestTemp)
{
	highestTemp = 0;
	xHighestTemp = 0;

	while (highestTemp <= 7)
	{
		if (xXTempsX[1][highestTemp] >= xHighestTemp)
		{
			xHighestTemp = xXTempsX[0][highestTemp];
		}

		highestTemp++;
	}

	cout << "The Highest Temp out of the seven days was: " << xHighestTemp << endl;
}

It displays random numbers within the two arrays, but not the numbers that I desire I am not sure where I went wrong with my information I match the array from my avgTemps function so I would assume that is how I would see the highest and lowest values.

Edited 5 Years Ago by Vindal: n/a

Setting you variables to zero on lines 85 and 103 isn't what you should do. You should initialize them to the first value in the array and then check from there. On line 89 you are checking if the lowest temp is less than the array value and if it is make the array value the lowest temp. You should be doing it the other way around. There are other errors but you should be able to start from here.

void showLowest(int xXXTempsX[2][7], int &lowestTemp, int &xLowestTemp)
{


	while (lowestTemp <= 7)
	{
		if (&xLowestTemp >= &xXXTempsX[1][lowestTemp] )
		{
			&xLowestTemp = &xXXTempsX[1][lowestTemp];
		}

		lowestTemp++;
	}

	cout << "The Lowest Temp out of the seven days was: " << xLowestTemp << endl;
}

void showHighest(int xXTempsX[2][7], int &highestTemp, int &xHighestTemp)
{


	while (highestTemp <= 7)
	{
		if (&xHighestTemp >= &xXTempsX[0][highestTemp])
		{
			xHighestTemp = xXTempsX[0][highestTemp];
		}

		highestTemp++;
	}

	cout << "The Highest Temp out of the seven days was: " << xHighestTemp << endl;
}

I changed how they see each other and didn't initalize the numbers to 0 and it gives me a break error in my compiler (Visual studio 2010)

You don't need the '&'s on lines 7,9,24. You are trying to compare addresses of those values. Once you've passed something in by reference (i.e., using & in the function definition) you just access it like any other variable.

Edited 5 Years Ago by jonsca: n/a

I keep getting location errors c4700 errors and a bunch of initializing errors
Here is the full code again I really do not know how to fix this.

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

using namespace std;

//******function prototypes********
void getTemps(int xTemps[2][7]);
void avgTemps(int xTempsX[2][7], double &high, double &low);
void showLowest(int xXXTempsX[2][7]);
void showHighest(int xXTempsX[2][7]);


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);
	//shows the lowest Temperature

	showLowest(temps);
	//shows the highest Temperature

	showHighest(temps);
	
	

	system("pause");
	return 0;
	
}

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

void avgTemps(int xTempsX[2][7], double &high, double &low)
{
	high = 0;
	low = 0;

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

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

	cout << fixed << setprecision(0);

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

}

void showLowest(int xXXTempsX[2][7])
{
	int lowestTemp, xLowestTemp;
	
	if (xLowestTemp >= xXXTempsX[1][lowestTemp] && lowestTemp <= 7)
	{
		
		{
			xLowestTemp = xXXTempsX[1][lowestTemp];
		}

		xLowestTemp++;
	}

	cout << "The Lowest Temp out of the seven days was: " << xLowestTemp << endl;
}

void showHighest(int xXTempsX[2][7])
{
	int highestTemp, xHighestTemp;
	
	if (xHighestTemp >= xXTempsX[0][highestTemp] && highestTemp <= 7)
	{
		
		{
			xHighestTemp = xXTempsX[0][highestTemp];
		}

		xHighestTemp++;
	}

	cout << "The Highest Temp out of the seven days was: " << xHighestTemp << endl;
}

I think you know this already, but the error is in your highest and lowest temp functions.

Your variables are uninitialized and you're trying to get the highest or lowest with just one if statement.

Reread NathanOliver's post:

You should initialize them to the first value in the array and then check from there.

You were right in using a loop before (though you don't need a while, a for will suffice). Imagine you have room in your hand for the lowest temperature, as you're walking along what happens if you find a lower one? Do you need the old one anymore?

I keep getting location errors c4700 errors and a bunch of initializing errors
Here is the full code again I really do not know how to fix this.

Neither do we, since we don't know what initialization errors there are, nor where. And c4700 doesn't mean anything to us non-compiler humans. It helps to give us ALL the information you can so we can understand the problem.

Edited 5 Years Ago by WaltP: n/a

This article has been dead for over six months. Start a new discussion instead.