Saturday, December 14, 2019

What is array in c. Types of array

Tags
Declaration of array in crograms

what is array in cprograms.




Array is continuous memory allocation of a same data types. If you have a data of same datatype than you can use an array.Array is used to store similar types of data.In array all elements

store in continuous memory.



If you have a lots of books and you want to store the data of that books than you can not use the different variables for store the data of books in that situation  you can use array.



Declaration Of Array:


 Array is always declared with integer  and float datatype.


ex. int a[5].   where int is datatype of the array and [a] is name of the array and [5] is size of the array.



This image is pictorial look of the array.






Array is always start with 0th index.

The above array is stored in memory as:

a[0]:10

a[1]:11

a[2]:40

a[3]:10

a[4]:11





Types Of Array:

Their are two  types of array.



1.Single dimensional Array

2.Multi dimensional Array





1.Single dimensional Array

Array having a only one value is called single dimensional array. It is also called one dimensional array.It stores the elements in a linear form.



Ex.  int a[5];



The following example is used to understand the  single dimensional array concept briefly.



#include<stdio.h>

#include<conio.h>

void main()

{

    int book[5];  //declaration af array.

    clrscr();

   

   printf("Enter the elements");

 

   for(i=0; i<=4; i++)  //take input and store the value of an array

   {

   scanf("%d",&a[i]);

   }



   printf("The array elements are:-")



   for(i=0; i<=4; i++)  // To display the array elements

  {

     printf(%d \n ",book[i];



}

getch();

}





2.Multi dimensional Array

Array having more than one subscript value is called multi dimensional array. Multi dimensional array is also called two dimensional array or matrix.



Ex. int a[5][5];



The following example is used to understand the  single dimensional array concept briefly.

 #include<stdio.h>

#include<conio.h>

void main()

{

   int books[5][5];

 clrscr();

printf("Enter the value of matrix:");



for(i=0; i<=4; i++) //Takin input from user.

{

   for(j=0; j<=4; j++)

  {

     scanf("%d, &book[i][j]);

  }

}

 printf("The entered array element is:");

 for(i=0; i<=4; i++)

{

   for(j=0; j<=4; j++)

  {

     printf(%d \n", books[i][j]);

  }

}

getch();

}