How to iterate over a matrix (2D array) in C

1 Answer

0 votes
#include <stdio.h>

#define ROW 5
#define COL 4

int main(void) {
    int matrix[ROW][COL] = {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 0, 1, 2},
                {6, 8, 1, 9},
                {7, 3, 4, 3}
    };
    
    for (int i = 0; i < ROW; i++) {
        for (int j = 0; j < COL; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}



/*
run:

1 2 3 4 
5 6 7 8 
9 0 1 2 
6 8 1 9 
7 3 4 3 

*/

 



answered Dec 26, 2020 by avibootz
edited Dec 26, 2020 by avibootz

Related questions

1 answer 160 views
1 answer 108 views
1 answer 158 views
1 answer 84 views
84 views asked Mar 9, 2025 by avibootz
1 answer 167 views
167 views asked May 2, 2021 by avibootz
1 answer 238 views
238 views asked Dec 30, 2020 by avibootz
2 answers 189 views
...