Im trying to complete an assignment and am stuck on two parts.

With the code listed below, I need to add two additional functions named indexHighTemp and indexLowTemp.

indexLowTemp: This function needs to return the index of the highest high temperature in the array.
indexHighTemp: This function needs to return the index of the lowest low temperature in the array.

I'd like to add these functions to the code listed below, with minimal intrusions.
Thanks for your input!

#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;
}

struct example
And careful about getting your row/col backwards. The latest post you were using [row] as the 2nd dimension, but all of your code so far is using [row] for the 1st dimension.

I assume your problem is that the array is two dimensional, but a function can only return one thing.

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).

I think the first option would work fine, seeing how High temps are assigned to matrix[0][rows] and Low temps are stored in matrix[1][rows]. Could I simply utilize two for loops and send the results back to main? If so, think you could lend me a hand code wise?

I was thinking something like below but I cant seem to get it to work right. I think my logic is a little fubar also.

for (counter = 0; counter < 12; counter++)
{
if (matrix[0][colums] < max)
max = matrix[0][colums]
}

Hopefully the 'struct' idea is self explanatory, but here's an example of treating the data as a single dimenstional array.
The concept is to allow access to the 2D array via a 1D array, then define a function that can return the offset of the desired item in the 1D array.

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;

And careful about getting your row/col backwards. The latest post you were using [row] as the 2nd dimension, but all of your code so far is using [row] for the 1st dimension.

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 ];

Thank you very much for your assistance!
I was really close by the time that I noticed this post. Your assistance helped confirm that I was on the right track. I appricaite the timely reply.

0

Featured Post

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

What is C++ STL?:
STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector.
…

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects.
A brief on problem:
Lets take example problem for simplicity:
- I have a G…

The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…