Array

The array is s collection of similar type of data items, which are stored under a common name. It is a derived data type. The individual data firms can be integer or float or character and so on. An array is one of the data structures. its used to solve the real problem-solving statement.

syntax for Array

             
data_type arrayname[size]={ value1,value 2...};


example:
       
  int name[50];
 float height[20];

types of array:

  1. one dimensional array
  2. two-dimensional array
  3. multidimensional-array
one dimensional array 

 the collection of similar data items that stored under one variable the number using only one subscript(index), such variable is called a one-dimensional variable. A one-dimensional array or single-dimensional array is a type of linear array, whose elements are accessed with a single subscript, which can either represent column or row. The array must be declared, in the same manner, is an ordinary variable except that each array name just has the size of the array(number of the elements can be able to store an array variable). when we give the size of the array is starting to store the value from 0 to (n-1).
n which means our size of the array

example;
                 int name[5];



 name[0]        name[1]            name[2]         name[3]      name[4]
                                                                                                  

this the example for a normal one-dimensional array. here, we are going to create one name array. then, the size of the name array is 5.    when we used array with the array size our compiler suddenly it will create one array table like above the example.


      int marks[6]={10,20,30,40,50,60};

      mark[0]=10   mark[1]=20  mark[2]=30   mark[3]= 40    mark[4]=50         mark[5]=60
            10              20                  30                   40                   50                      60            

here,  we have given one another example. In this example we have directly given the which values should be placed on our array.

sample program:


#include<stdio.h>
int main()
{
  int weight[4];
 printf("enter the number:");                           
 for(i=0;i<=5;i++)
  {
   scanf("%d\n", &weight[i]);  \\ get the data from users
  }
 for(i=0;i<=5;i++)
{
  printf("\n array is:%d",weight[i]); \\ print our given array   
}
return 0;
}


      

output:


 enter the number:
 1
2
3
4
array is:1
2
3
4


this the output our program. first, we got input from the users and just we are printing the whole set of array.  A two-dimensional array needs to be used when a table of the values needs to store in an array. This can be defined in the same fashion as a one-dimensional array except for a separate pair of square brackets are required.a two-dimensional array is mainly used for matrixes problems..etc. 




syntax :
 
data_type array_name[row_size][colomn_size];

here. is the syntax for the two-dimensional array. The first brackets indicate row size, the next packets indicate the column size.

example:
 
int array[4][4];

here is the example for a two-dimensional array. we going to create one table of the array it contains 4 rows  4 columns.

int a[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
                                (or)
int a[4][4]={ {1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

for an understanding purpose;



a[0][0]=1

a[0][1]=2

a[0][2]=3

a[0][3]=4

a[1][0]=5

a[1][1]=6

a[1][2]=7

a[1][3]=8

a[2][0]=9

a[2][1]=10

a[2][2]=11

a[2][3]=12

a[3][0]=13

a[3][1]=14

a[3][2]=15

a[3][3]=16




when we give any array our compiler sets one empty table based on our given row and column size.
The next stride it will assume the value that we are going to give in input, that values go to your array table.

another one syntax for, get the input array values from the user,

int a[rows][colomn];
for(i=0;i<row;i++)
{
 for(j=0;j<colomn;j++)
 {
  scanf("%d",&a[row][colomn];
 }
}

In this, we can get input from users. The first for loop, to get rows, then another one for loop for columns.

sample program for two-dimensional array,



 #include<stdio.h>
      int main(){                                            
   int a[3][3],i,j;
   printf("enter the values");
   for(i=0;i<3;i++)
   { 
     for(j=0;j<3;j++)
      {
          scanf("%d\n",&a[i][j]);   \\ get the input values from users
         }
     }
  printf("output ");
   for(i=0;i<3;i++)
   {
     for(j=0;j<3;j++)                                                                           
     {
           printf("%d\t\t",a[i][j]); \\ print the values
      }
  }
return 0;
}
 

output:


 enter the values
1
2                                                                         
3
4
5
6
7
8
9

output
1      2       3  
         
7      8       9
Any array with more than two-dimensional array it is called a multidimensional array.


 MULTI DIMENSIONAL 

syntax:
 
 data_type array_name[row_size][coloumn_size][array_size];

example,

int a[4][4][4];
 

this the 3D image of a multidimensional array.

sample program for a multidimensional array,




#include<stdio.h>
int main()
{
  int a[2][2][2],i,j,k;
  print("enter the value);
  for(i=0;i<2;i++)    
  {
    for(j=0;j<2;j++)
     {
       for(k=0;k<2;k++)
        {
           scanf("%d\n",&a[i][j][k]); \\ get the input values from user
         }
       }
   }
printf("output);
 for(i=0;i<2;i++)
  {
    for(j=0;j<2;j++)
     {
       for(k=0;k<2;k++)
        {
           printf("%d",a[i][j][k]);   \\ print the array
         }
       }
   }
return 0;                                                                
}


output:

 enter the value
1
2
3
4
5
6
7
8
output:
12345678

No comments:

Post a Comment