Arrays:
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. The following is an integer representation of an array A.
A
Different Types of Array There are three types of array 1) 1 dimension array
2) 2 dimension array 3) Multi-dimension array
Variables Declaration of Array
Variables declaration of one dimension array for integer data type:
int A[6];
Assigning values:
int A[6]={12,13,12,14,15,18}; //Compile time initialization A[0]=12;
A[1]=13;
A[2]=12;
A[3]=14;
A[4]=15;
A[5]=18;
Addition between two array elements:
int B[1]=A[3] + A[4]=29;
So B[1]=29;
Variables declaration of one dimension array for character (char) data type:
char A[6]
Assigning characters:
char name[5]={'a','b','3','µ','A'}; //Compile time initialization The characters are allocated as follows:
A[0]=’a’;
A[1]=’b’;
A[2]=’3’;
A[3]=’µ’;
A[4]=’A’;
10 20 30 40
Assigning characters as a string:
char name[10]=”WELL DONE” ; The characters are allocated as follows:
‘W’ ‘E’ ‘L’ ‘L’ ‘ ‘ ‘D’ ‘O’ ‘N’ ‘E’ ‘\0’
0 1 2 3 4 5 6 7 8 9 That is ==>
name [0]=’W’;
name [1]=’E’;
name [2]=’L’;
name [3]=’L’;
name [4]=’ ’;
name [5]=’D’;
name [6]=’O’;
name [7]=’N’;
name [8]=’E’;
name [9]=’ \0’;
char name[10]="well done" ; printf("%d\n",name[9]);
output: 0
When the compiler sees a character string, it terminates it with an additional null character. Thus the element name[9] holds the null character ‘\0’. When declaring character arrays, we must allow one extra element space for null terminator.
char name[4]={'a','b','3','µ','A'};
This is not right. Data overflow regarding array size.
Run time initialization:
int sum[100];
for (int i=0;i<100;i++) {
if(i<50)
sum[i]=0;
else
sum[i]=1;
}
The first 50 elements of the array sum are initialized to zero while the remaining 50 elements are initialized 1 at run time .
Default assigning:
int A[5]={3};
3 0 0 0 0
0 1 2 3 4 A[0]=3;
And the remaining locations are filled by 0.
Initialization of two dimension array:
int A[row][column];
int table[2][3]= {0,0,0,2,2,2};
printf("%d\n",table[1][0]);
output: 2 or
int b[2][3]={
{0,0,0}, {2,2,2}
};
printf("%d\n",table[1][0]);
output: 2
How to find the odd the numbers from the array :
#include<stdio.h>
int main() {
int a[10];
int n;
printf("Enter valuse of n:=\n");
scanf("%d",&n);
for( int k=0;k<n; k++)
{
scanf("%d",&a[k]);
}
printf("\nreading array:=");
for( int i=0;i<n;i++) {
printf(" %d",a[i]);
}
printf("\nOutput array:=");
for(int j=0;j<n;j++) {
if(a[j]%2!=0)
printf(" %d",a[j]);
} return 0;
}