I need some help with this array!

I finally got this array set up but I need help modifying it to display the location (row & column) that the max number is in. Can anyone assist me with this?

Here is what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

const int ColMax = 5;
double MatrixMax(const double[][ColMax],int,int);
int main ()
{
	int ColRow[2];
	const int RowSize = 4, ColSize = 5 ;
	double Values[RowSize][ColSize] = {{16,22,99,4,18},
									   {-258,4,101,5,98},
									   {105,6,15,2,45},
									   {33,88,72,16,3}};
	double Maximum = MatrixMax(Values,RowSize,ColSize);
	cout << "Contents of Matrix\n";
	for (int Row = 0; Row < RowSize; Row++)
	{	for (int Col = 0; Col < ColSize; Col++)
		cout << setw(6) << Values[Row][Col];
	cout << endl; }
cout << "Max Value in Matrix = " << Maximum << endl;

return 0;}

double MatrixMax(const double A[][ColMax], int R, int C)
{	double Max = A[0][0];
	for (int Row = 0; Row < R; Row++)
		for (int Col = 0; Col < C; Col++)
			if (A[Row][Col]>Max) Max = A[Row][Col];
	return Max;}


I am trying to revise the function, MatrixMax, so when I run the program it will identify a row number and column number in which the max is in, even if I were to change the values within the array.
Last edited on
Since you can't return two values from a function, you have two choices.
1) Return a struct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Result
{  int maxrow;
    int maxcol;
};

Result MatrixMax(const double A[][ColMax], int R, int C)
{   Result rslt;
     rslt.maxrow = 0;
     rslt.maxcol = 0;
...
    if (A[Row][Col]>Max) 
    {   rslt.maxrow = Row;
         rslt.maxcol = Col; 
    }
    return rslt;
}


2) You can pass the row and column back as reference arguments
 
double MatrixMax(const double A[][ColMax], int R, int C, int & maxrow, int & maxcol)



Your data resides in array Values[] , but you say you are passing array 'A'
to your function MatrixMax () as well as 'C' and 'R' both the latter of which don`t seem to have been declared. Maybe I can`t see the wood for the trees or just haven`t had enough coffee. To find the max value in array Values [] you would need to iterate through array Values[] with at least one loop
Topic archived. No new replies allowed.