Finding the maximum value in a matrix (C programming) -


i'm learning code in c. i've wrote following code find maximum value inside matrix, reason program return highest value in first row (87), except desired 99. can't find flaw in code. happy help!

#include <stdio.h>  int maxmin(int a[][4], int row, int col) {   int i, j, max;   max = a[0][0];    (i = 0; < row; i++) {     (j = 0; j < col; j++) {       if (a[i][j] > max)         max = a[i][j];     }     return max;   } }  void main() {   int a[3][4] = {     {  3, 87, 11, 23 },     { 99, 78, 19, 44 },     { 59, 60, 13, 14 }   };    int num;   num = maxmin(a, 3, 4);   printf("%d\n", num); } 

not easy spot bugs down & j typos. return max; inside row's loop.

move end of function , you'll examine every row of matrix. compiled , ran fix.

the way discover such errors, either put in print statements, :

printf( "a[%d][%d]=%d ", i, j, a[i][j]); 

or step through program debugger, setting break point on code you're interested in.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -