One-Dimensional and Two-Dimensional Array

After learning the basics of array, let's see what's One-Dimensional Array and Two-Dimensional Array? One-Dimensional Array is normal array, which is basically linear and only in one-direction. Two-Dimensional Array is array in which can store data in table form or matrices. Two-Dimensional Array has rows and columns i.e we can form tables. 

NOTE : All the codes which we learnt in the basics of Array tutorial is for One-Dimensional Array.

How to declare Two-Dimensional Array?

datatype arrayName[rowSize][columnSize];
Example : int table[3][5];

Here, we need to mention two sizes, one for the rows and one for the columns. In the example, we have 3 rows and 5 columns in each row.

How to store data in Two-Dimensional Array?

You have to run two loop to store data in 2-D array, as everytime you need to increment the array index. So, let's learn how to store data in 2-D array.

for(i=0;i<=sizeOfRows;i++)
{
 for(j=0;j<=sizeOfColumns;j++)
 {
  scanf("%d", &arrayName[i][j]);
 }
}

NOTE : In old compilers, you need to declare the size of the array i.e. keep the size as constant and it can't be variable but in new compilers you can keep the size of an array as variable.

How to print the stored value from the array?

for(i=0;i<=sizeOfRows;i++)
{
 for(j=0;j<=sizeOfColumns;j++)
 {
  printf("%d", arrayName[i][j]);
 }
 printf("\n");
}

To print the stored value from the array, follow the above syntax, if the array is of Integer data type. This will print the 2-D array in rows and columns form.
Previous
Next Post »
0 Comment