r/programminghelp • • Nov 29 '20

C Need some help with my C program.

Hi, i need some help in my C program. My task is to create a program that will create a N*N matrix and read its biggest and smallest elements in every row along with their location and then print out the matrix along with the smallest and biggest element in every row.

So far i've got most of it working except for those issues:

  • location/coordinates of printed results are wrong

  • maximum elements are read correctly but the minimum results are wrong. it always prints out first element of every row along with its wrong coordinates

  • the requirement of the task states that i have to use at least 1 more function different from main. I do not understand how to do this, essentially i need to split the program into at least 2 functions.

If someone could please take a look at my code and fix it up a bit i would be really thankful.

#include<stdio.h>

int main(){
    int m;                                                          //user interface
    printf("Input a number defining the row/column size of the matrix (N*N): ");
    scanf("%d",&m);
    printf("Please input " "[%d]*[%d]" " matrix elements.\n\n",m,m);

    int matrix[m][m], col, row;
    for(col=0;col<m;col++){                                        // filing the matrix
        for(row=0;row<m;row++){
            printf("Define [%d][%d] element: ",col,row);
            scanf("%d", &matrix[col][row]);}
            printf("\n");
                    }
                                                                    // printing out the matrix
    printf("Entered matrix:\n");

    for(col=0;col<m;col++){
        for(row=0;row<m;row++){
            printf("%d\t",matrix[col][row]);}
        printf("\n");
                    }
printf("\nLargest elements in every row of the matrix:");
    for(col = 0;col < m;col++){
        int max = matrix[col][0];
    for(row = 0;row < m;row++){                                     // determining the largest row element along with its coordinates
        if(max < matrix[col][row]) max = matrix[col][row];
                        }
        printf("\nElement [%d], is the largest in row [%d] at column[%d]",max,col,row);
                        }

printf("\n\nSmallest elements in every row of the matrix:");
    for(col = 0;col < m;col++){
        int min = matrix[col][0];
    for(row = 0;row > m;row++){                                     // determining the smallest row element along with its coordinates
        if(min < matrix[col][row]) min = matrix[col][row];
                        }
        printf("\nElement [%d], is the smallest in row [%d] at column[%d]",min,col,row);
                        }
return 0;}
4 Upvotes

0 comments sorted by