Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int rows =2;
const int columns =12;
string month[] = {"January", "Feburary", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"};
void getData();
double averageHigh(int matrix[rows][columns]);
double averageLow(int matrix[rows][columns]);
int row;
int col;
int matrix[rows][columns];
int counter = 0;
int maxIndex = 0;
int main()
{
double highaverage;
double lowaverage;
for(row = 0; row<rows; row++)
for(col =0; col < columns; col++)
matrix[row][col] = 0;
getData();
highaverage = averageHigh(matrix);
cout<<"The average of the high temperatures is "<<highaverage<<endl;
lowaverage = averageLow(matrix);
cout<<"The average of the low temperatures is "<<lowaverage<<endl;
system("pause");
return 0;
}
void getData()
{
cout<<"Please Enter the Highest Temperature for each month: "<<endl;
row = 0;
for(col = 0; col<columns; col++)
{
cout << month[col] << ": ";
cin>>matrix[row][col];
}
cout<<"Please Enter the Lowest Temperature for each month: "<<endl;
row = 1;
for(col = 0; col<columns; col++)
{
cout << month[col] << ": ";
cin>>matrix[row][col];
}
}
double averageHigh(int matrix[rows][columns])
{
double average=0;
int sum = 0;
row = 0;
for(col =0; col<columns; col++)
sum = sum + matrix[row][col];
average = sum / 12;
return average;
}
double averageLow(int matrix[rows][columns])
{
double average=0;
int sum =0;
row =1;
for(col =0; col<columns; col++)
sum = sum + matrix[row][col];
average = sum / 12;
return average;
}
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
int indexLowTemp(int matrix[rows][columns])
{
int LowTemp = matrix[0][0];
int rc = 0; //Return Code - single digit index to matrix
int row;
int col;
for( row=0; row < rows; row++ )
{
for( col=0; col < cols; col++ )
{
if( matrix[row][col] < LowTemp )
{
LowTemp = matrix[row][col];
rc = row * rows + col;
}
}
}
return rc;
}
/* To get the value of the lowest temp */
int* pmatrix = &matrix; //Pointer to matrix[0][0];
int LowTemp = *pmatrix[ indexLowTemp( matrix ) ];
/* Bonus, decode the row/col from the return of indexLowTemp */
int index = indexLowTemp( matrix );
int row = index / rows;
int col = index % rows;
struct rowcol
{
int row;
int col;
};
struct rowcol indexLowTemp(int matrix[rows][columns])
{
int LowTemp = matrix[0][0];
struct rc;
rc.row = 0;
rc.col = 0;
int row;
int col;
for( row=0; row < rows; row++ )
{
for( col=0; col < cols; col++ )
{
if( matrix[row][col] < LowTemp )
{
LowTemp = matrix[row][col];
rc.row = row;
rc.col = col;
}
}
}
return rc;
}
/* To get the value of the lowest temp */
struct rowcol rc = indexLowTemp( matrix );
int LowTemp = matrix[ rc.row ][ rc.col ];
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.