How to take 2-D array elements as input?

  • As we used loops for taking input of 1-Dimensional array elements, same  like 1-D array we will use loops for 2-D array as well.
  • As we know using loops for taking input is a good approach and it is more efficient than the normal approach of using number of scanf() functions.
  • For taking input of element of 2-D array, we have to use Nested for loop

Syntax:

for(initialization; condition; increment )
{

for(initialization; condition; increment)
        {
              //body of this for loop
          
        }
}

Let's take some elements as inputs of 2-d array.and print

Q:declare a 2-d array of 3 rows and 4 cols and take their element as input from the user and print them on screen like an table.?

#include<stdio.h>

void main()
{     //integer type of multi-dimensional array having 3 rows and 4 columns.
    int i,j;
    int arr[3][4];



         for(i = 0; i < 3; i++)
         {
             for(j = 0; j<4; j++)
             {   printf("enter the element:");   //for each value of i this for loop will iter 4 times.
                 scanf("%d",&arr[i][j]);       //means total iteration will be 3*4=12 ,it will take 12 iteration.
             }
         }

          // printing array  elements

          printf("Array Elements are:")
          for(i=0; i<3; i++)
          {
              for(j=0; j<4; j++)
              {
                  printf("%d\t",arr[i][j]);
              }
              //after completing above loop cursor should go in New line.
              printf("\n");
          }
}

Output:

enter the element:1
enter the element:2
enter the element:3
enter the element:4
enter the element:5
enter the element:6
enter the element:7
enter the element:8
enter the element:9
enter the element:10
enter the element:11
enter the element:12

Array Elements are:
     1    2     3    4    
     5    6    7    8    
     9    10   11   12   
  • this is how we take element of 2-D array as input.
  • Do programs on taking inputs of element of 2-D array ,for understanding more better.
  • It looks hard but ,as you will start doing programs you will find it easy and you will have fun while doing it.







Practice Programs

  • Programs regarding Arrays
  • Programs Regarding 2d arrays
  • Programs regarding matrix
  • And lot more


Further Topics:


People Also Searched: