Program to Read and Print 2d Array in C Language

program-to-read-and-print-2d-array-in-c-language

Read and Print 2d Array in C Program Description:

Write a Program to Read and Print 2D Array in C programming language. The program should take the user input for the 2D Array or Maxtrix and update the Matrix. Then it should print the given 2D-Array on the console.

This program will help us to understand how to take user input and update the 2D Array (Matrix) and How to iterate over the 2D Array and print the elements of the 2D Array.

What is a 2D Array or Matrix:

The Two Dimensional Arrays or 2-D arrays are collections of one-dimensional arrays ( Array of 1-D Arrays). We can create two-dimensional arrays by specifying the two subscripts  [ ].

The two-dimensional array elements are stored in the contiguous memory. We can represent a 2-D array as the Matrix.

The 2-D arrays are used to store and manipulate data in tabular format, with each row representing a different record and each column representing a different field or attribute of that record.

Declaring the 2D Array or Matrix:

data_type array_name[row_size][column_size];

We have already covered the 2D Arrays in detail in earlier articles, Please go through the following links to learn more about the 2D Arrays and Multi-dimensional arrays in C.

Let’s look at the Example Input and Output of the Program.

Example Input and Output:

Input:

Output:

As we can see from the above output, The program should take the user-provided values and update the 2D array and print it back on the console.

Read and Print 2d Array in C Program Explanation:

Let’s look at the step-by-step explanation of the Read and Print 2D array in the C Program.

  1. Start the program by creating two Constants called ROWS and COLUMNS. The ROWS constant holds the number of rows in the 2D Array and the COLUMN constant holds the number of columns in the 2D Array. ( We can also use Macros instead of constants)
  2. Create a 2D Array or Matrix named values with the row size of ROWS and column size of COLUMN. ( values[ROWS][COLUMNS];)
  3. Now, We need to Read the input from the user and update the values 2D Array.
  4. To Read user input, We need to create two For Loops, One For Loop will iterate over the ROWS, and the Second For Loop will iterate over the COLUMNS.
    • Create the First For Loop ( Outer Loop), Start the iteration from i=0 and go till the i<ROWS (i.e for(i=0; i<ROWS; i++) ). The Outer Loop will iterate over the ROWS and At Each iteration of the Outer Loop.
    • Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<COLUMNS (i.e for(j=0; j<COLUMNS; j++) ). The Inner Loop will iterate over the COLUMNS. At each iteration of the Inner Loop
      • Prompt the user to provide the Matrix or 2D Array element and read it using the scanf function and Update the values[i][j] element.
      • Repeat the above step for all elements in the array
  5. Once the above Two Loops are completed, The values[][] 2D Array will be updated with user-provided values.
  6. Now, We need to Print the Array elements on the Standard Output.
  7. To Print the 2D Array elements or Matrix Elements, Similar to the reading of a 2D array, We need to use two For Loops. One for iterating over the ROWS and another for Iterating over the COLUMNS.
    • Create Outer For Loop. Start from i=0 to i<ROWS (which is for(i=0; i<ROWS; i++) )
      • Create Inner For Loop, Start from j=0 to j<COLUMNS ( which is for(j=0; j<COLUMNS; j++) )
        • Print the 2D Array or Matrix Element values[i][j] using the printf function.
        • printf("%d  ", values[i][j]);
  8. Once the above two for loops are completed, All the elements of the values array will be printed on the console.
  9. Stop the Program.

Program to Read and Print 2d Array in C Language:

Here is the program to Read and Print 2D array or Matrix in C programming language.

Program Output:

Let’s compile and Run the Program.

Compile the program using the following command.

$ gcc read-print-2darray.c

Run the executable file.

read-and-print-2d-array-in-c-program-output

Let’s try a few more examples

As we can see from the above output, The program is Reading the 2D array elements from the user and displaying the 2D array on the console.

Read and Print Matrix in C with Custom Row and Column sizes:

In the above program, We have hard-coded the array size as 3X3, Let’s modify the program to allow us to use the Custom Row and Column sizes.

To use the custom Row size and column size, We can create an array that can hold a large number of elements. Here we are taking 100 as the max row( MAXROWS) and column( MAXCOLUMNS) sizes. So user can input any row and column size between one(1) to hundred(100).

Here is the modified version of Read and Print 2D Array program, Where we will ask user fo the custom Row and Column size.

Program Output:

Compile and Run the Program.

Test 1: Normal Cases:

read-and-print-2d-array-in-c-with-custom-row-and-column-size-program

As we can see from the above output, We are able to use the custom Row size and Columns size for the 2D Array or Matrix.

Test 2: When size goes beyond limit.

When the input size for row or column goes beyond the limit, Then program will display an error message ( Invalid Row or Column, Please Try again)

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

6 Responses

  1. […] our previous article, We have looked at How to Read and Print the 2D Array elements in C language, In today’s article, We will look at Passing 2d Array to Function in C programming language with […]

  2. […] have looked at the Reading and Printing Matrices and How to Pass Matrices to functions programs in earlier articles, In today’s article, We will […]

  3. […] our previous articles, we looked at the Read and Print Matrices Program, In today’s article, We are going to look at the program to perform the Subtraction of […]

  4. […] Read and Print 2D Arrays in C […]

  5. […] C Program to Read and Print Matrix or 2D Array […]

  6. […] Read and Print 2D Arrays in C […]

Leave a Reply