matrix display in C language

Asked 2 years ago, Updated 2 years ago, 115 views

I'd like to see a matrix in C language, but how do I pre-assign values using the for statement as a fixed value instead of substituting values with scanf?

I would like the results to be as follows.

A=
  1 2 3 4
  5 6 7 8
  9 10 11 12

c procession

2022-09-30 15:49

2 Answers

You can loop from 1 to the size of the array and replace the number.


2022-09-30 15:49

You may want to express the three rows and four columns in a two-dimensional array, and then set the values for the elements in order (using for-loop here).

const M=3;
    constint N = 4;

    int A[M][N];

    for(intm=0;m<M;m++){
        for(intn=0;n<N;n++){
            A[m][n]=n+(N*m)+1;
        }
    }

    printf("A=\n");
    for(intm=0;m<M;m++){
        printf("";
        for(intn=0;n<N;n++){
            printf("%d", A[m][n]);
        }
        printf("\n");
    }


2022-09-30 15:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.