Two dimensional array in C

Declaring array and displaying the elements
#include <stdio.h>
int main(void){
int i,j;
int num[3][3]={
                {1,2,3},
                {4,5,6},
                {7,8,9}
                };
for(i=0;i<3;i++){
printf("Top row number %d \n\n", i);
for(j=0;j<3;j++){
printf("Column number %d , value=%d \n\n", j,num[i][j]);
}
}
return 0;
}
Output is here
Top row number 0

Column number 0 , value=1

Column number 1 , value=2

Column number 2 , value=3

Top row number 1

Column number 0 , value=4

Column number 1 , value=5

Column number 2 , value=6

Top row number 2

Column number 0 , value=7

Column number 1 , value=8

Column number 2 , value=9

Reading and displaying elements of two dimensional arrays

#include <stdio.h>
int main(void){
int i,j;
int num[3][3];

num[2][2]=100;
printf("%d",num[2][2]);

for(i=0;i<3;i++){
printf("Top row number %d \n\n", i);
for(j=0;j<3;j++){
printf("Column number %d , Enter value=", j);
scanf("%d",&num[i][j]);
}
}
printf("\n Data entry is over , now displaying \n");
for(i=0;i<3;i++){
printf("\n value : %d", i);
for(j=0;j<3;j++){
printf("\n value : %d", num[i][j]);
}
}
return 0;
}
Output is here
Top row number 0

Column number 0 , Enter value=1
Column number 1 , Enter value=2
Column number 2 , Enter value=3
Top row number 1

Column number 0 , Enter value=4
Column number 1 , Enter value=5
Column number 2 , Enter value=6
Top row number 2

Column number 0 , Enter value=7
Column number 1 , Enter value=8
Column number 2 , Enter value=9

 Data entry is over , now displaying

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

Enter data for a two dimensional array and display in a matrix form.

#include <stdio.h>
int main(void){
int i,j;
int num[3][3];
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Number %d, %d : ",i,j);
scanf("%d", &num[i][j]);
}
}
////////////
for(i=0;i<3;i++){

for(j=0;j<3;j++){
printf(" %d  ", num[i][j]);
}
printf("\n");
}
////////////
return 0;
}
In above code double the value of 2nd row, 2nd element and display.
Solution : Add this code at the end.
printf("\n after chnaing \n");
num[1][1]=2*num[1][1];
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf(" %d  ", num[i][j]);
}
printf("\n");
}

Practice Questions

  1. Create one num1[3][3] matrix and ask user to enter elements.
  2. Display the above matrix ( output should be in matrix format with line break at end of each row )
  3. Before displaying the matrix multiply the 2nd row 2nd element by 2 and then display.
  4. Create two matrix ( num1[3][3] and num2[3][3] ) and display the addition in another matrix.
  5. Create two matrix ( num1[3][3] and num2[3][3] ) and display the difference in another matrix.
  6. Create two matrix, the second matrix should be double of each element of first matrix. You will only ask data from the user for one matrix only.
Matrix Multiplication Matrix Sum
Array in C


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer