MAX ARRAY

Mar 13, 2014 at 11:09pm
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];
			
		}



	  }


}
Mar 13, 2014 at 11:52pm
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.
Mar 14, 2014 at 12:09am
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];
}



}

}



}
Mar 14, 2014 at 12:11am
Should the function return something?
Mar 14, 2014 at 2:25am
i tried returning --- return max;

does there need to be some type of return for the function or not always??
Mar 14, 2014 at 3:12am
You can't return entire arrays from the function. However, you can return a pointer to the array
Mar 14, 2014 at 8:11am
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?
Mar 14, 2014 at 12:36pm
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 Mar 14, 2014 at 12:40pm
Mar 14, 2014 at 1:05pm
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.
Mar 14, 2014 at 1:34pm
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.
Mar 14, 2014 at 2:06pm
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 Mar 14, 2014 at 2:17pm
Mar 14, 2014 at 4:05pm
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.