Array Min and its Location

I am trying to write a program that loops through a 2x3 array and locates the smallest value (I figured that part out). Then I need it to display that value (figured this out too) and its row and column position. My code finds the smallest value but not the right location. No matter where the smallest value is, it says its in 2, 3 when it should be 0, 0. Any suggestions?
//#include <stdio.h>
#define ROWS 2
#define COLS 3

void findMin(int [ROWS][COLS]);

int main()
{
	int val[ROWS][COLS] = 	{2,64,13,88,31,82};
	
	findMin(val);
}
void findMin(int val[ROWS][COLS])
{
	int i, j, min, minrow, mincol;
	for(i = 0; i < ROWS; i++)
		for (j = 0; j < COLS; j++)
	if (val[i][j] < min)
	
	min = val[i][j];
	minrow = i;
	mincol = j;
	
	printf("\nThe minimum array value is %d\n", min);
	printf("The array position of the minimum value is %d %d\n", minrow, mincol);
	
	return;
}

Comments

  • You need { and } around the loops because they are multi-line statements. You should get into the habit of using braces for loops like that whether they are needed or not so that there not be such problems if you want to add a statement inside the loop.
    for(j = 0; j < COLS; j++)
    {
        if (val[i][j] < min)
        {
            // blabla
        }
    }
    
  • Thanks. Late night work, forgot the basics.
Sign In or Register to comment.