Changing a matrix by multiplying indexes of min element I need to make the following C program: Given A (N, N) matrix (2D array). The program needs to find the min element in each column. Then it needs to multiply the row in which the element is, with the difference of its indexes (a[i] * i - j). Then transfer the new row on another matrix. Print the new matrix.

Here is my code:

#include&lt;stdio.h&gt;

void InputArray(int a[][50],int M, int *N);
void NewArray(int a[][50], int b [][50], int N);
void PrintA(int a[][50], int N);
void PrintB(int b[][50], int N);

void main(){
    int a[50][50], b[50][50], N;
    InputArray(a, 50, &amp;N);
    system("cls");
    NewArray(a, b, N);
    PrintA(a, N);
    PrintB(b, N);
}
void InputArray(int a[][50], int M, int *N){
    do{
        printf("Enter rows and columns: ");
        scanf("%d", N);
    } while (*N &lt; 1 || *N &gt; M);

    for (int i = 0; i &lt; *N; i++){
        for (int j = 0; j &lt; *N; j++){
            printf("Enter i[%d][%d] element ", i, j);
            scanf("%d", &amp;a[i][j]);
        }
    }
}

void NewArray(int a[][50], int b[][50], int N){
    int i, j, min, d, k, rowb = 0, rowa = 0;
    for (i = 0; i &lt; N; i++){
        min = a[0][i];   
        d = 0 - i;      
        for (j = 0; j &lt; N; j++){ 
            if (a[j][i] &lt; min){
                min = a[j][i];
                d = j - i;
                rowa = j;
            }
        }

        for (k = 0; k &lt; N; k++){
            b[rowb][k] = a[rowa][k] * d;
        }
        rowb++;
    }
}
void PrintA(int a[][50], int N){
    printf("You've entered: \n");
    for (int i = 0; i &lt; N; i++){  
        for (int j = 0; j &lt; N; j++){
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}
void PrintB(int b[][50], int N){
    printf("New matrix is :\n");
    for (int i = 0; i &lt; N; i++){
        for (int j = 0; j &lt; N; j++){
            printf("%d  ", b[i][j]);
        }
        printf("\n");
    }
}


It does work, but I think it the "NewArray" function can be made better.

Here is an example output:

You've entered:
1 4 9
2 6 5
3 8 2
New matrix is:
0 0 0 
-1 -4 -9
0 0 0
Use error detection

With non-numeric input, scanf("%d", N); and following code will not function as expected.  Handle bad input.  For now, simple whine and take your toys home.  Later you can add more sophisticated error handling.

void InputArray(int a[][50], int M, int *N){
    // scanf("%d", N);
    if (scanf("%d", N) != 1) {
      perror("Invalid size");
      exit(EXIT_FAILURE);
    }


Seek clarification or state functionality on edge cases

"program needs to find the min element in each column. Then it needs to multiply the row in which the element is, with the difference of its indexes".  What should happen if the min value occurs twice?

Fold like functions together

Only difference between PrintA() and PrintB() is the header text.  Pass that in.  

void PrintArray(const char *header, const int a[][50], int N){
    fputs(header, stdout);
    for (int i = 0; i &lt; N; i++){  
        for (int j = 0; j &lt; N; j++){
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}
...
PrintArray("You've entered:\n", a, N);
PrintArray("New matrix is :\n", b, N);


Avoid naked magic numbers

Rather than code 50 in many places, define it once and use that.

#defined ARRAY_DIM_MAX 50
// int a[50][50], b[50][50], N;
// InputArray(a, 50, &amp;N);
int a[ARRAY_DIM_MAX][ARRAY_DIM_MAX], b[ARRAY_DIM_MAX][ARRAY_DIM_MAX], N;
InputArray(a, ARRAY_DIM_MAX, &amp;N);


Further, consider using different constant names should you later want a rectangle matrix.  Even if both are 50, this better self documents the code.  Recommend to not declare many variables declares on a line.

#defined ARRAY_ROW_SZ 50
#defined ARRAY_COL_SZ 50

// int a[50][50], b[50][50], N;
// InputArray(a, 50, &amp;N);
int a[ARRAY_ROW_SZ][ARRAY_COL_SZ];
int b[ARRAY_ROW_SZ][ARRAY_COL_SZ];
int N = 0; // Defensive coding to assign simple small objects.
InputArray(a, ARRAY_ROW_SZ, &amp;N);




Minor stuff

No need for j==0 iteration

for (i = 0; i &lt; N; i++){
    min = a[0][i];   
    d = 0 - i;      
    // for (j = 0; j &lt; N; j++){ 
    for (j = 1; j &lt; N; j++){ 


Use const when referenced data is unchanged

This allows for wider applications, convey to users of the function of data involatility and may allow additional optimizations.

// void PrintA(int a[][50], int N){
void PrintA(const int a[][50], int N){


Use standard signatures

void main() may be acceptable on your platform, but not others.   Better to return int.

Avoid non-portable code

system("cls"); is non-standard.

Use size_t

For array sizing and indexing, size_t is the Goldilocks type, neither not too narrow nor too wide.  As you develop, use that rather than int for indexing.  int is OK for learner code.

// void InputArray(int a[][50], int M, int *N){
void InputArray(int a[][50], size_t M, size_t *N){