Wednesday, April 30, 2025

C Program - How do you Add Elements to a 2D Array in C

This C program reads input from the user and stores it in a 2-dimensional integer array called number_arr. It then prints the contents of the array to the console. 

//get stuff into 2d array
#include <stdio.h>

int main()
{

    int a, b; //insertion of elements
    int x, y; //extraction - printing

    int number_arr[2][4];

    //Insertion of elements into 2d array

    for (a = 0; a < 2; a++)
    {
        for (b = 0; b < 4; b++)
        {
            printf("Enter value for number_arr[%d][%d] ", a, b);
            scanf("%d", &number_arr[a][b]);
        }
    }

    //Printing

    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;
}

Result: 

Enter value for number_arr[0][0] 1
Enter value for number_arr[0][1] 2
Enter value for number_arr[0][2] 3
Enter value for number_arr[0][3] 4
Enter value for number_arr[1][0] 5
Enter value for number_arr[1][1] 6
Enter value for number_arr[1][2] 7
Enter value for number_arr[1][3] 8
1 2 3 4
5 6 7 8
Process returned 0 (0x0)   execution time : 20.221 s
Press any key to continue.

Code explanation: 

#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 a, b; //insertion of elements
    int x, y; //extraction - printing

    int number_arr[2][4];

These lines declare the integer variables a, b, x, and y, which will be used to iterate over the rows and columns of the 2-dimensional array number_arr. The array is initialized with 2 rows and 4 columns. 

    //Insertion of elements into 2d array

    for (a = 0; a < 2; a++)
    {
        for (b = 0; b < 4; b++)
        {
            printf("Enter value for number_arr[%d][%d] ", a, b);
            scanf("%d", &number_arr[a][b]);
        }
    }

These lines begin a nested for loop that iterates over the rows and columns of the number_arr array. Inside the loop, the printf function is called to prompt the user to enter a value for the current element of the array, and the scanf function is called to read the user's input and store it in the array. 

    //Printing

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

These lines begin a nested for loop that iterates over the rows and columns of the number_arr array. Inside the loop, the printf function is called to print the current element of the array to the console.

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

    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 .  ...