One dimensional array (1-D): – When we put one subscript simple ([]) then the array will be called as one dimensional array. One dimensional array is called vector.

Declaration 1-D array: –

Data_Type Array_Variable_Name[Size];

EX: –

int a[4];
char ch[15];
float f[8];

Initialization of 1-D array –

Case (1) –

Syntax: –

Data_Type Array_Variable_Name[Size]={Value};

Remark: – From the above syntax the array variable is initialized at the time of declaration or creation.

Ex: –

int a[5]={10,20,30,40,50}
char ch[10]={‘v’,’i’,’v’,’e’,’k’};
or
char ch[10]=“vivek”;

Anonymous array: – When the size is not given when we initialize at the time of declaration then the compiler assume the size of array as counting the total number of element.

EX: –

int a[]={10,20,30,40,50}
char ch[]={‘v’,’i’,’v’,’e’,’k’};

Case (2) –

int array[4];
array[0]=10;
array[1]=20;
array[2]=30;
array[3]=40;

Case (3) – By using for loop –

int array[4],i;
for(i=0;i<4;i++)
     {
     cout<<”enter value for array[“<<i<<”]=”;
     cin>>array[i];
     cout<<”\n”;
     }

Values of array regarding garbage of zero –

Q.1.
int a[5]={2,1,3}; then a[3] & a[4] what value will hold _ _ _ _ _ ?
Answer: - 0 & 0
Q.2.
int a[5]={0};
Answer: - All subscript will hold zero (0)
Q.3.
int a[5];
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
What value will be hold by a[4] _ _ _ _ _ ?
Answer: - Garbage

Pass an element of array to function: – It is also possible to pass an element of array to a function. In this process, call by value technique will be followed.

Pass whole array to a function: – We can also pass the whole array to a function as actual argument inside the parenthesis of function call to the formal argument of defined function. A call by address technique is use in this concept.

Whatever the changes made with formal argument, the actual argument will be changed permanently.

Advertisements