Array: Array is one kind of variable declaration of same types of data with a name and identify them one from another followed by subscripts or index.
Example: int arr[10];
Hence int is data type.arr is array name.arr[10] means we can keep ten integer type value in arr[10];
The declaration int arr[10]; would reserve enough space for an array called arr that could hold up to 10 integers. The picture given below clear to concept about the Array reserved storage space.
arr[0] | |
arr[1] | |
arr[2] | |
arr[3] | |
arr[4] | |
arr[5] | |
arr[6] | |
arr[7] | |
arr[8] | |
arr[9] |
Note: Array index range from 0 to n-1.Here index of arr[10] is start from arr[0] and end at arr[9]
Array Declaration:
data_type variable_name[size];
Example:int month[12];
float number[10];
char name[10];
Array Representation:
Shown below some example of array defination for assignment of initial values:
int digit[10] ={1,2,3,4,5,6,7,8,9,10};
float f[10]={0.3,0,0.25}
For example, i want to represent five integer number in an array,say(10,20,30,40,50) by an array variable name number,then declaration of array variable number as followes:
int number[5] ;
The values to the array elements can be assigned as follows:
number[0]=10;
number[1]=20;
number[2]=30;
number[3]=40;
number[4]=50;
For this case the array number stored valued as shown below:
number[0] | 10 |
number [1] | 20 |
number [2] | 30 |
number [3] | 40 |
number [4] | 50 |
Also consider the following array definition:
int number[5]={1,5,8};
The values to the array elements can be assigned as follows:
number[0]=1;
number[1]=5;
number[2]=8;
number[3]=0;
number[4]=0;
Note:character type array use last index for null('\0') .
char name[6]={"hasan"};
the array elements are stored as follows:
Note:character type array use last index for null('\0') .
char name[6]={"hasan"};
the array elements are stored as follows:
name[0]='h';
name[1]='a';
name[2]='s';
name[3]='a';
name[4]=n'';
name[5]='\0'; Difference between array variable and ordinary variable: In an ordinary variable we can assign same types of only one value, but in an array variable we can assign same types of more then one values. For example: int number; number=5; In this number variable we can take only one integer value. For an array variable we can assign more then one same types of values.Shown in below: int number[5]; number[0]=10; number[1]=20; number[2]=30; number[3]=40; number[4]=50; | |||||||||||||||||
Taking input throw keyboard and output with a C program: | |||||||||||||||||
#include<stdio.h> #include<conio.h> void main() { clrscr(); int number[5],i; printf("Take five integer value throuh keyboard:\n\n"); for(i=0;i<5;i++) { printf("number[%d]=",i+1); scanf("%d",&number[i]); } printf("\n\nDisplay the array\n\n"); for(i=0;i<5;i++) { printf("number[%d]=%d\n",i+1,number[i]); } getch(); } |
josh !!! hasan vai chalie jan ,, arektu complex kore .
ReplyDelete