int matrix[][]; // the matrix to find the max in
int max = matrix[0][0];
int r,c;
for(r = 0; r < 3; ++r) {
for(c = 0; c < 3; ++c) {
if(matrix[r][c] > max) {
max = matrix[r][c];
}
}
}
// max is now the maximum number in matrix
Chat with our AI personalities
You initially assume that the first number you want to compare is the maximum. Call this "maximum", for example. Then you iterate through all the other numbers you want to compare, and if you find a number that is larger than "maximum", copy this number to "maximum". To iterate through all the elements of a matrix, you have to use two "for" loops with indices, which are traditionally called "i" and "j". To restrict to an upper triangle for example, you need to calculate the corresponding range for the indices.
Each number in a matrix is called an element.
Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.
Yes, do write. That's what you always have to do when you have got a homework-program.
For example, if you have [ -4 1 0 3] as your matrix, it would be negative 4. Whatever negative number is in your matrix is your answer.