Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses a two-dimensional array to store the highest and lowes

ID: 3813910 • Letter: W

Question

Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:

a. Function getData: This function reads and stores data in the two - dimensional array.

b. Function averageHigh: This function calculates and returns the average high temperature for the year.

c. Function averageLow: This function calculates and returns the average low temperature for the year.

d. Function indexHighTemp: This function returns the index of the highest high temperature in the array.

e. Function indexLowTemp: This function returns the index of the lowest low temperature in the array.

(These functions must all have the appropriate parameters.)

I believe something is wrong with my output for this question but im not sure what is the correct output

This is what i got ::::::::::::::::::::::::::::::::::::::::::::::::::

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

void getData(int array [12][2]);
double averageHigh (int array [12][2]);
double averageLow (int array [12][2]);
int indexHighTemp (int array [12][2]);
int indexLowTemp (int array [12][2]);

int main()
{
int array [12][2];

int high;
int low;


getData(array);
averageHigh(array);

cout << setw(20) << left << "Average High Temp:" << fixed << setprecision(2) << averageHigh (array)<<endl;

averageLow (array);

cout << setw(20) << left << "Average Low Temp:"<< fixed<< setprecision(2) << averageLow (array)<<endl;

high = indexHighTemp (array);
low = indexLowTemp (array);

cout << setw(20) << left << "Highest Temp:";
cout << fixed <<setprecision(2) << high << endl;
cout << setw(20) << left << "Lowest Temp:";
cout << fixed << setprecision(2) << low << endl;

int z;
cout << "pause" << endl;
cin >> z;


}

void getData (int array[12][2])

{

int i;

int j;

for(i=0; i<12; i++)

{

for(j=0; j<2; j++)

{

}

}

}

double averageHigh (int array [12][2])

{

double avg;

double sum;

int x;

int y;

avg = 0;

sum = 0;

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

{

{

sum = sum + array[x][0];

}

}

avg = sum / x;

return avg;

}

double averageLow (int array[12][2])

{

double avg2;

double sum2;

int x;

int y;

avg2=0;

sum2=0;

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

{


{

sum2 = sum2 + array[x][1];

}

}

avg2 = sum2 /x;

return avg2;

}

int indexHighTemp (int array[12][2])
{

int highest;
int x;
int y;

highest = array[0][0];

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

{

for (y=0; y<2; y++)

{

if (highest < array[x][y])

{

highest =array[x][y];


}
}

}

return highest;

}

int indexLowTemp (int array[12][2])

{

int lowest;

int x;

int y;


lowest = array[0][0];

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

{

for (y=0; y<2; y++)

{

if (lowest > array[x][y])

{

lowest = array[x][y];


}

}

}


return lowest;

}

OUTPUT:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Average High Temp: -791443813.50

Average low Temp: 7.08

Highest Temp: 1229148993

Lowest Temp: -2145872742

Explanation / Answer

Below are the issues with your program.

1. You are not initializing the array of templrature with your values. In getData function you are neither reading a file, not taking input from the user and not even hardcoding the values. So array is getting initialized with garbage values(arbitrary values). and so is your output weird. I tried running your code by changing getData as below: -

void getData (int array[12][2])
{
int i;
int j;
for(i=0; i<12; i++)
{
for(j=0; j<2; j++)
{
   if(j == 0) {
       array[i][0] = 10;
   }else {
       array[i][1] = 5;
   }
}
}
}

i.e. I am inserting for every month high as 10 and low as 5, I am getting correct output as below: -

Average High Temp: 10.00
Average Low Temp: 5.00
Highest Temp: 10
Lowest Temp: 5
pause

So you need to give inputs to your array.

2. The way you are getting high and low temperatures is correct but not as per the question. The program will work correctly but this will be not as per the question. indexHighTemp and indexLowTemp must return the array index of high and low temperatures not the temperatures itself and you need to use those indexes to print the values in main() function. If this is not the issue than its ok...

So the main part to make your code work is to give some values to your temperature array.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote