Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.
#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;
}
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 ];
You have two options. Define a struct with two integers and return a structure, or return a single integer that is equal to the location of the desired item as if it were a one dimensional array.
In other words, for an array of matrix[rows][cols], that is equal to an array of matrix2[rows*cols], and the item at matrix[x][y] would be at matrix2[(x-1)*size of an element + col], (basically, it's possible to specify an element in a multi-dimensional array with a single value).