Unit 04
Unit 04
Arrays: Concept, Declaration and Initialization of Arrays, Accessing Individual Elements of Array.
Use of Arrays in Sorting, Searching. Concept of 2-D array (Matrix),
Passing arrays to functions, Examples.
Arrays Limitations
#####################
Initialization:
Syntax: Data_type Array_name[size]={list of values};
Ex: int x[6]={1,2,3,4,5,6};
Department of BS&H Page 78
NRI Institute of Technology, Pothavarappadu
As a result , memory locations of X filledup as follows:
1 2 3 4 5 6
X[0] X[1] X[2] X[3] X[4] X[5]
Note:
1. if the number of values in initializer is less than the size of an array, only
those
many first locations of the array are assigned the value. The remaining locations are
assigned as zeros.
Example:
int x[6]={1,2,3};
Size of array = 6,
Values initialized =3.
Then the initialization is as follows
1 2 3 0 0 0
X[0] X[1] X[2] X[3] X[4] X[5]
Example:
Static int X[6];
0 0 0 0 0 0
X[0] X[1] X[2] X[3] X[4] X[5]
Example: 1.
int x[]={1,2,3,4,5,6};
Values initialized =6
Hence, size of array =6
And sizeof(X)=12bytes.
1 2 3 4 5 6
X[0] X[1] X[2] X[3] X[4] X[5]
2.
int x[]={1,2,3,4};
Values initialized =4
Hence, size of array =4
And sizeof(X)=8bytes.
1 2 3 4
X[0] X[1] X[2] X[3]
5. Array elements can not be initialized selectivily
Example:
Int X[5]={ , 20}
An attempt to initialize only second location is illegal.
Accesing elements:
The syntax template for accessing an array component is
ArrayName[ IndexExpression ]
Example:
int x[6]={1,2,3,4,5,6};
in the above statement
x[0]=1
x[1]=2
x[2]=3
x[3]=4
x[4]=5
x[5]=6
example1:
/*example to access 1-D array*/
#include<stdio.h>
Example2:
/*example to access 1-D array @ runtime initialization*/
#include<stdio.h>
#include<conio.h>
void main()
{
int A[5],i; /*array declaration*/
clrscr();
for(i=0;i<5;i++) /*reading values for array*/
{
printf("enter A[%d]\n”,i);
scanf(“%d”,&A[i]);
}
printf(“entered values are\n”);
for(i=0;i<5;i++) /*accessing the values of an
array*/
{
printf("A[%d]=%d\n”,i,A[i]);
}
getch();
}
Ouput:
Enter A[0]
10
Example3:
//Sum of the elemets in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int A[5],i,sum=0; /*array declaration*/
clrscr();
for(i=0;i<5;i++) /*reading values for array*/
{
printf("enter A[%d]\n",i);
scanf("%d",&A[i]);
}
for(i=0;i<5;i++) /*accessing the values of an
array*/
{
sum=sum+A[i];
}
printf("Sum of elemets in array is %d",sum);
getch();
}
Ouput:
Enter A[0]
10
Enter A[1]
20
Enter A[2]
30
Enter A[3]
Example 4:
//minimum and maximum elemets in Array
#include<stdio.h>
#include<conio.h>
void main()
{
int min=0,max=0,i,a[5];
clrscr();
for(i=0;i<5;i++) //reading array elements
{
printf("Enter the A[%d] elements ",i);
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
if(a[i]<min) //checking for minimum element
min=a[i];
if(a[i]>max) //checking for maximum element
max=a[i];
}
printf("\n Minimum element is %d",min);
printf("\n Maximum element is %d",max);
getch();
}
Ouput:
Enter A[0]
10
Enter A[1]
20
Enter A[2]
30
Enter A[3]
40
Enter A[4]
50
Maximum element is 50
Minimum element is 10
Syntax:
Example:
int a[3][3];
In above example, a is an array of type integer which has storage size of 3 * 3 matrix. The
total size would be 3 * 3 * 2 = 18 bytes.
Memory Allocation :
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], i, j;
clrscr();
printf("\n\t Enter matrix of 3*3 : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
Department of BS&H Page 82
NRI Institute of Technology, Pothavarappadu
{
Matrix is :
3 4 5
6 7 2
1 2 3
Limitations of two dimensional array :
o We cannot delete any element from an array.
o If we dont know that how many elements have to be stored in a memory in
advance, then there will be memory wastage if large array size is specified.
ARRAY APLICATIONS:
Matrix Addition:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter size of the matrix ");
scanf("%d",&n); //reading size of the matrix
printf("Enter Matrix 1 elements ");
for(i=0;i<n;i++) //for reading each row
{
for(j=0;j<n;j++) //for reading columns
{
scanf("%d",&a[i][j]); //reading the elements of matrix 1
}
}
printf("Enter Matrix 2 elements ");
for(i=0;i<n;i++) //for reading each row
Character Arrays
########################
Inter function communication
########################
Use of Arrays in Sorting, Searching. Concept of 2-D array (Matrix), Passing arrays to
functions, Examples.
########################