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.