185k views
5 votes
write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. prompt the user for 12 months of highest and lowest. write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. these methods must be your original code. your program should output all the values in the array and then output the average high and the average low.

User Zionyx
by
4.4k points

1 Answer

2 votes

Answer:

Explanation:

#include <iostream>

using namespace std;

const int months = 12;

void getData( double [][ 2 ], int );

double averageHigh ( double [] [ 2 ],int );

double averageLow ( double [] [ 2 ], int );

int indexofHighTemp ( double [] [ 2 ], int );

int indexofLowTemp ( double [] [ 2], int );

//main body of the function

int main()

{

double temperature [ months ] [ 2 ];

// takes data from user

getData( temperature, months );

//Prints result

cout << "The average high temperature for the year is "<<endl;

<< averageHigh ( temperature, MONTHS);

cout << "The average low temperature for the year is "<<endl;

<< averageLow ( temperature, MONTHS);

// Print the result

cout <<"Index of highest temperature for the year is "<<endl;

<< indexofHighTemp (temperature, MONTHS );

cout << "Index of lowest temperature for the year is "<<endl;

<< indexofLowTemp ( temperature, MONTHS );

return 0;

}

// store data in array

void getData ( double t [] [ 2 ], int y)

{

for ( int i = 0; i < y; i ++ )

{

// show & read input from the user

cout << " Enter the highest temperature for month:"<<endl;

<< ( i + 1 ) << " : ";

cin >> t [ i ] [ 0 ];

cout << " Enter the lowest temperature for month: "<<endl;

<< ( i + 1 ) << " : ";

cin >> t [ i ] [ 1 ];

}

}

// Returns the average high temperature of the year

double averageHigh ( double t [] [ 2 ], int m)

{

//assign a variable

double sum = 0;

//the sum of the highest temperatures

for ( int i = 0 ; i < y; i ++)

sum + = t [ i ] [ 0 ];

// average of the Highest temperatures

return ( sum/y);

}

//Returns the average Low Temperature of the year

double averageLow( double t [] [ 2 ], int y)

{

double sum =0;

//Compute the sum of the lowest temperatures

for ( int i = 0; i < y; i ++)

sum + = t [ i ] [ 1 ];

//Return the average of the Lowest temperatures

return ( sum / y);

}

//Returns the index of highest of high temperatures

int indexofHighTemp (double t [] [ 2 ], int y )

{

//Declare the variables

int index = 0;

double highest = t [ 0 ] [ 0 ];

//Search for the highest high temperature

for ( int i= 1; i < y ; i ++)

if ( t [ i ] [ 0 ] > highest )

{

//Modify the highest and index

highest = t [ i ] [ 0 ];

index = i;

}

//Return the index of highest high temperature

return index;

//End of the Function indexofHighTemp

}

//Returns the index of lowest of low temperatures

int indexofLowTemp ( double t [] [ 2 ], int y)

{

//Declare the variable

int index = 0;

double lowest = t [ 0 ] [ 1 ];

for (int i = 1; i < y; i ++)

if( t [ i ] [ 1 ] < lowest )

{

lowest = t [ i ] [ 1 ];

index = i; }

return index;

//End of function indexofLowTemp

}

User Source Matters
by
3.9k points