Monday, April 28, 2025

C Program - How to Print 2D Array in C with For Loops

This program creates a 2-dimensional integer array number_arr with 2 rows and 4 columns. It then uses a nested for loop to iterate through each element in the array and print its value to the console.

The outer loop iterates through the rows of the array (x), while the inner loop iterates through the columns of the array (y). The if statement inside the outer loop checks if x is equal to 1, and if so, prints a newline character to the console to separate the two rows of the array.

The program then prints each element in the array using the printf() function with the %d format specifier to print integers.

When run, this program will output the following to the console: 

1 2 3 4 
5 6 7 8 

Note that the two commented-out printf() statements at the bottom of the program demonstrate how to access individual elements of the array by their row and column indices. For example, number_arr[0][0] would access the element in the first row and first column of the array, which has a value of 1

#include <stdio.h>

int main()
{

    int x, y;

    int number_arr[2][4] =
    {
        {1, 2, 3, 4},
        {5, 6, 7, 8}
    };

    for (x = 0; x < 2; x++)
    {
        if (x == 1)
        {
            printf("\n");
        }
        for (y = 0; y < 4; y++)
        {
            printf("%d ", number_arr[x][y]);
        }
    }

    return 0;
}

//printf("%d \n", number_arr[0][0]);
//printf("%d \n", number_arr[1][0]);

Result: 

1 2 3 4
5 6 7 8
Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

Let's break down the C program line by line: 

#include <stdio.h>

This line includes the standard input/output library, which provides the necessary functions for input and output operations in C programs. 

int main()
{

This line declares the main function, which is the entry point for the program. The int before main indicates that the function returns an integer value to the operating system when it terminates. 

    int x, y;

This line declares two integer variables x and y, which will be used to iterate over the rows and columns of the 2-dimensional array number_arr

    int number_arr[2][4] =
    {
        {1, 2, 3, 4},
        {5, 6, 7, 8}
    };

This line initializes a 2-dimensional integer array number_arr with 2 rows and 4 columns. The first row contains the values 1, 2, 3, and 4, while the second row contains the values 5, 6, 7, and 8

    for (x = 0; x < 2; x++)
    {

This line begins a for loop that iterates over the rows of the number_arr array. The loop starts with x equal to 0 and continues until x is less than 2

        if (x == 1)
        {
            printf("\n");
        }

This line checks if the current row x is equal to 1. If it is, a newline character (\n) is printed to the console to start a new line before printing the elements of the second row. 

        for (y = 0; y < 4; y++)
        {
            printf("%d ", number_arr[x][y]);
        }

This line begins a nested for loop that iterates over the columns of the number_arr array. The loop starts with y equal to 0 and continues until y is less than 4.

Inside the nested loop, the printf function is called to print the current element of the array, which is accessed using the indexes x and y. The format specifier %d is used to print an integer value, followed by a space character to separate the values. 

    }

This line ends the nested for loop. 

    return 0;
}

This line ends the main function and returns the integer value 0 to the operating system, indicating that the program has terminated successfully.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...