Unit 2
Unit 2
Unit 2
6. int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
7. int a[5]= {0 }
8. int a[5] ={ } ;error
char b[ ]={‘c’, ‘s’,’E’}
At runtime :
int i ;
int a[5];
eg 1:
printf(“Enter the elements of array”};
for(i=0;i<5;i++)
{
Scanf(“%d”,&a[i]);
}
eg 2:
int a[100],i;
for(i=0;i<=100;i++)
{
if (i<30)
a[i]=1;
else
a[i]=0;
}
Memory Representation & accessing of array:
int a[5]={1,10,0,-1,2}
1 10 0 -1 2
………. ……
1 10 0 -1 2
base address 2000 2002 2004 2006 2008
Accessing :
To access an element of an array we have to use index value and name of the array.
0 1 2 3 4
1 10 0 -1 2
eg: if we want to take 1 element
a[0]=1 1st element
a[3]= -1 4th element
To calculate the address of the element :
B+(index * size of int)
2000+(3*2)
2000+6=2006
Note :
Array is a collection of more than one data item of same type.
All data items are stored in contiguous memory location.
Number of data items array hold is size of array.
Once size has declared it can’t be changed at run time ( fixed size).
Index starts from zero /0.
It is known as derived data type.
Accessing of any element is faster using index of array.
It allows to store data in multi dimensional form.
Inserting & Deleting is tough.
No boundary checking in C.
eg :
#include<stdio.h>
#include<conio.h>
void main()
int i,a[5];
for(i=0;i<5;i++)
{
scanf(“%d”,&a);
}
for(i=0;i<5;i++) // for(i=4;i>=0;i--)
{
printf(“%d\n”,a[i]);
}
getch();
}
Read marks of 5 students ,calculate sum & avg using array.
#include<stdio.h>
#include<conio.h>
void main()
int marks[5];
int sum=0,avg;
for(i=0;i<5;i++)
{
scanf(“%d”,&marks[i]);
}
for( i=0;i<5,i++)
{
sum=sum+marks[i];
}
avg=sum/5;
printf(“%d”,sum);
printf(“%d”,avg);
}
To read an array of 10 integers & count total no.of even and odd elements.
#include<stdio.h>
void main()
{
int a[10],i;
int even=0,odd=0;
printf(“enter array elements:”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
if(a[i]%2==0)
even++;
else
odd++;
}
printf(“total no.of even no are :%d”,even);
printf(“total odd no are :%d”,odd);
}
Two dimensional array:
Collection of 1d array
Also known as array of array.
Syntax:
Datatype arrayname [row][column];
eg: int arr[3][3];
It is also called matrix.
Memory allocation:
int a[4][3];
C1 C2 C3
R1
a[0][0] a[0][1] a[0][2]
R2
a[1][0] a[1][1] a[1][2]
R3
a[2][0] a[2][1] a[2][2]
R4
a[3][0] a[3][1] a[3][2]
7 7 40
7 6 26 Product
6 9 48
**column of 1st matrix=row of 2nd matrix we can do multiply**
#include<stdio.h>
#include<conio.h>
#define N 50
int main()
int a[N][N],b[N][N],c[N][N],I,j,k,sum,m,n,p,q;
printf(“Enter rows and columns for 1st matrix:\n”);
scanf(“%d %d”,&m,&n);
printf(‘Enter first matrix;\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,a[i][j]);
}
}
printf(“Enter rows and columns for 2nd matrix:\n”);
scanf(“%d %d”,&p,&q);
printf(‘Enter second matrix;\n”);
for(i=0;i<pi++)
{
for(j=0;j<q;j++)
{
scanf(“%d”,b[i][j]);
}
}
Printf(“\n first matrix is :\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
Printf(“\n second matrix is :\n”);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf(“%d\t”,b[i][j]);
}
printf(“\n”);
}
if (n!=p)
{
printf(“can not multiply “);
}
else
{
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
sum=0;
for (k=0;k<m;k++)
{
sum=sum+(a[i][j]+b[i][j]);
}
c[i][j]=sum;
}
}
printf(“multiplication is :\n”);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}}