Solved programming problem

Find Max value and its index from 2D array in C.

Source code

#include <stdio.h>

int main()

{

    int i,j,n,row=0,column=0;

    int array[100][100];

    printf("Please Enter row or column Size: ");

    scanf("%d",&n);

    printf("Please Enter a %d X %d matrix: ",n,n);

    for(i=0; i<n; i++)

    {

        for(j=0; j<n; j++)

        {

            scanf("%d",&array[i][j]);

        }

    }

    int max=array[0][0];

    for(i=0; i<n; i++)

    {

        for(j=0; j<n; j++)

        {

            if(array[i][j]>max) ///checking part

            {

                max=array[i][j]; ///save value

                row=i;            ///save row

                column=j;        ///save column

            }

        }

    }

    printf("The Max value in the array is: %d",max);

    printf("And its Index is: %d %d",row,column);

    return 0;

}

Output

Please Enter row or column Size:3

Please Enter a 3 x 3 matrix:

1 2 5

3 9 0

1 5 8

The Max value in the array is:9

And its Index is: 1 1

 

Join the discussion

Add your comment