English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

int min_matrix(int a[][NCOLS], int nrows){
---int i, j, min = 0;

------for(i = 0; i < nrows; i++)
---------for(j = 0; j < NCOLS; j++)
------------if(a[i][j] < min)
---------------min = a[i][j];
------return min;
}


The code finds the lowest possible value and returns it. However there is a bug in the code, find and fix the bug.

2007-04-16 17:10:56 · 4 answers · asked by Adam B 1 in Computers & Internet Programming & Design

4 answers

You must not initialize min to zero. What if the two dimentional array's min value is 1? In that case your function would return 0 as the lowest value in the array and that's inccorect. Initialize the first value in the array to min as follows:

min = a[0][0];

2007-04-16 19:33:08 · answer #1 · answered by Silver_Sword 3 · 1 0

I spotted error. min should not be initialized to 0.
suppose the numbers is greater than 0 then it will show the result as 0 but zero is not in array a.
So it will become an bug.

I have enclosed the corrected program. if you have any problem call me at m_gopi_m@yahoo.co.in

int min_matrix(int a[][],int nrows,int ncols)
{
int i, j;
min=a[0][0];

for(i = 0; i < nrows; i++)
{
for(j = 0; j < ncols; j++)
{
if(a[i][j] < min)
min = a[i][j];
}
}
return min;
}

2007-04-17 11:17:54 · answer #2 · answered by Gopinath M 3 · 0 0

I know C++ but its sort of similar. From the looks of it u should have "int NCOLS" after "int nrows" at the top and not inside the [ ]. :

int min_matrix(int a[][], int nrows, int NCOLS){


I dunno what the function prototype looks like so i dunno if there is an array and 2 integers passed to it. Other then that not really sure what else is wrong.

2007-04-17 00:58:11 · answer #3 · answered by Pierre 2 · 0 0

int min_matrix(int a[][NCOLS], int nrows){
---int i, j, min = a[0][0];

------for(i = 0; i < nrows; i++)
----- {
---------for(j = 0; j < NCOLS; j++)
---------if(a[i][j] < min) min = a[i][j];
------}

------return min;
}

2007-04-17 02:39:37 · answer #4 · answered by iyiogrenci 6 · 0 0

fedest.com, questions and answers