MAX ARRAY

Write a function called matrixMax that returns the largest element in a matrix of doubles , with numRows and numCols. A function definition skeleton is provided. Note that the problem does not call for you to output to the console, so do not output . Also, it does not call for you to input from the console, so do not input.

how do i set max?????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  double matrixMax(double M[MAXROWS][MAXCOLS], int numRows, int numCols)
{
	
	
	 for(int r=0; r<numRows; r++)
     {
		for(int c=0; c < numCols ; c++ )
		{
	
		    M[MAXROWS][MAXCOLS] = M[numRows][numCols];
			
		}



	  }


}
Which part of that code was provided?


In order to find the largest value you have to look at each element and maintain a copy of the largest value that you have found so far. Initialize that copy from the first element.
double matrixMax(double M[MAXROWS][MAXCOLS], int numRows, int numCols)
{

....

}


That was given above.

My new code with the advice you gave but still wrong :(


double matrixMax(double M[MAXROWS][MAXCOLS], int numRows, int numCols)
{

double max = M[0][0];


for(double r=0; r<numRows; r++)
{
for(double c=0; c < numCols ; c++ )
{


if(M[r][c] > max)
{
max = M[r][c];
}



}

}



}
Should the function return something?
i tried returning --- return max;

does there need to be some type of return for the function or not always??
You can't return entire arrays from the function. However, you can return a pointer to the array
i tried returning --- return max;

That is the right thing to do. 'max' is a double and the return value of 'matrixMax' is declared to be a double.

You say "still wrong" and "tried". That does not tell us what are the exact errors that you do encounter.

Please, keep using the code tags.

You have changed the types of 'r' and 'c' in your second version. Why?
Write a function called matrixMax that returns the largest element in a matrix of doubles , with numRows and numCols

your function is limited by this double M[MAXROWS][MAXCOLS] Your function can only process arrays of that size, rendering the other parameters meaningless.

You also modify the array, which wasn't the intended operation for the function.


perhaps this is closer to the question..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double matrixMax(double** matrix, int numRows, int numCols)
{
    double max = 0.0;

    for(int r=0; r<numRows; r++)
    {
        for(int c=0; c < numCols ; c++ )
       {
	    if (matrix[numRows][numCols] > max)
            {
                max = matrix[numRows][numCols];
            }
        }
    }
    return max;
}


call it like this...
1
2
double myMatrix[6][6] = {};
double answer = matrixMax(reinterpret_cast<double**>(myMatrix),6,6);
Last edited on
Jaybob66 wrote:
call it like this...
1
2
double myMatrix[6][6] = {};
double answer = matrixMax(reinterpret_cast<double**>(myMatrix),6,6);

That will not work. An array is not a pointer.
your function is limited by this double M[MAXROWS][MAXCOLS] Your function can only process arrays of that size, rendering the other parameters meaningless.

That is not entirely true. Yes, the M has a static size that is determined during compilation, but nothing forces it to be filled to the brim. This is legal use:
1
2
3
4
5
6
7
8
9
10
const size_t MAXROWS = 99;
const size_t MAXCOLS = 66;

int main() {
  double X[MAXROWS][MAXCOLS];
  X[0][0] = 7.0;
  X[0][1] = 42.0;
  double answer = matrixMax( X, 1, 2 );
  return 0;
}


Besides, the function was already declared, so the task is to implement the body only.
peter87 wrote:
That will not work. An array is not a pointer.


yes it is.

Upon further reading, it isn't. their interchangability led me to think that they are.
Last edited on
thanks for everyone's help!

The final answer that is correct:


double matrixMax(double M[MAXROWS][MAXCOLS], int numRows, int numCols)
{

double max = M[0][0];


for(int r=0; r<numRows; r++)
{
for(int c=0; c < numCols ; c++ )
{
if (M[r][c] > max)
{
max = M[r][c];
}
}
}


return max;
}
Topic archived. No new replies allowed.