Home > Enterprise >  How can i find smallest number in main diagonal of multidimensional array?
How can i find smallest number in main diagonal of multidimensional array?

Time:12-24

I need to write a program that loads a matrix of integers of dimensions 3x3 and finds the smallest element on the first diagonal. I made this code but it gives me only the smallest integer in whole matrix:

#include <stdio.h>

int main() {
    int mat[10][10];
    int i,j,smallest;
    printf(" ");
    for(i = 0; i < 10; i  )
        for (j = 0; j < 10; j  )
        scanf("%d", &mat[i][j]);
        smallest = mat[0][0];
    for ( i = 0; i < 10; i  ){ 
        for(j = 0; j < 10;  j  ){
            if(mat[i][j] < smallest)
            smallest = mat[i][j];
        }
    }
    printf("%d",smallest);
    return 0;
}

Can you give me any solutions to this problem ?

CodePudding user response:

The answer is very very easy and you surely find it even on google as well.

But here is a tip to find answers for such question:

Draw this matrix on a paper. (if not 10x10, try 2x2, then 4x4 then 5x5). then write down the indexes of only main diagonal elements.

for example in the case of 4x4 matrix, those would be:

[0][0], [1][1], [2][2], [3][3]

you see the pattern above?

The patter would follow for 10x10 too.

Now since you need to find the smallest element just on the main diagonal, you only need to loop over those elements on main diagonal only. (use the pattern above to design your loop for 10x10 and give it another shot.)

CodePudding user response:

  1. Use functions
  2. Use the correct types for indexes (size_t)
  3. When you test your code try to avoid user input. It will save you a looooots of time during the debugging.
int findSmallestDiagonal(size_t size, int (*array)[size])
{
    int result = array[0][0];
    for(size_t index = 1; index < size; index  )
        if(array[index][index] < result) result = array[index][index];
    return result;
}

void fillOrPrint(size_t size, int (*array)[size], int print, int max)
{
    for(size_t r = 0; r < size; r  )
    {
        for(size_t c = 0; c < size; c  )
        {
            if(print) printf("[m] ", array[r][c]);
            else array[r][c] = rand() % max;
        }
        if(print) printf("\n");
    }

}

#define SIZE 16

void main()
{
    int array[SIZE][SIZE];
    int smallest;

    srand(time(NULL));

    fillOrPrint(SIZE, array, 0, 1000); //fill array
    fillOrPrint(SIZE, array, 1, 0);    //print array

    smallest = findSmallestDiagonal(SIZE, array);

    printf("The smalles diagonal is: %d\n", smallest);
}

https://godbolt.org/z/75fzj8Tsq

CodePudding user response:

The following code will help for it,

    #include <stdio.h>

int main() {
    int mat[3][3];
    int i,j,smallest;
    printf(" ");
    for(i = 0; i < 3; i  )
        for (j = 0; j <3; j  )
        scanf("%d", &mat[i][j]);
        smallest = mat[0][0];
    for ( i = 0; i < 3; i  ){ 
        for(j = 0; j < 3;  j  ){
            if(i==j)     //logic for main diagnol
                if(smallest>mat[i][j])
                    smallest = mat[i][j];
        }
    }
    printf("%d",smallest);
    return 0;
}
  • Related