Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

Array

An Array is a collection of similar data


items, that are stored under a common
name.
Types

One-Dimensional array

Two-Dimensional array

Multi-Dimensional array

4/29/12

One-Dimensional array
Array Declaration

Syntax:
data_type array_name[size];

Example: int x[3];

x
X[0
]
X[1
]
4/29/12

Array initialization

At compile time

At run time

4/29/12

At compile time

Syntax:

data_type
array_name[size]={variables};
Example: int x[3]={5,3,7};

x
5
3
7
4/29/12

X[0
]
X[1
]

At Run time

Array can also initialize at the run time.

Example:
while(i<10)
{
if(i<5)
sum[i]=0;
else
sum[i]=sum[i]+i;
}

4/29/12

Example:
scanf(%d%d,&a[0],&a[1]);

4/29/12

Example
#include<stdio.h>
#include<conio.h>
void main()
{
int x[2],i;
printf("\nEnter the inputs:");
for(i=0;i<2;i++)
scanf("%d",&x[i]);
for(i=0;i<2;i++)
printf("\nThe value in x[%d] is %d",i,x[i]);
getch();
4/29/12

Output
Enter the inputs:3
6
The value in x[0] is 3
The value in x[1] is 6

4/29/12

Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char x[5]={'a','b','c','d','e'};
clrscr();
for(i=0;i<5;i++)
printf("\nThe value in x[%d] is %c",i,x[i]);
getch();

4/29/12

Output
The value in x[0] is a
The value in x[1] is b
The value in x[2] is c
The value in x[3] is d
The value in x[4] is e

4/29/12

Two-Dimensional array
Array Declaration

Syntax:

data_type array_name[row_size]
[col_size];
Example:Colint
x[3][2];
0
Col 1
0
row 1
row 2
row

4/29/12

X[0][0]

X[0][1]

X[1][0]

X[1][1]

X[2][0]

X[2][1]

Array Initialization

Syntax:

data_type array_name[row_size]
[col_size];={variables};

Example: int x[2][2]={1,50,2,75};

4/29/12

int x[2][2]={ {1,50},


{2,75}
};
(or)
int x[ ][2]={ {1,50},
{2,75}
};
4/29/12

Col 0

0
row 1
row

4/29/12

Col 1

50

75

Example

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int x[2][2]={{1,50},{2,75}};
clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]);
getch();
}4/29/12

Output
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75

4/29/12

Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int x[][2]={ {1,50},{2,75},{3,65}};
clrscr();
for(i=0;i<=2;i++)
for(j=0;j<2;j++)
printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]);
getch();
4/29/12

Output
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
The value in x[2][0] is 3
The value in x[2][1] is 65
4/29/12

Matrix Addition
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r1,r2,c1,c2;
int a[5][5],b[5][5],c[5][5];
clrscr();
step1:
printf("\n Enter the size of matrix A:");
scanf("%d%d",&r1,&c1);
printf("\n Enter the size of matrix B: ");
scanf("%d%d",&r2,&c2);
if((c1==c2)&&(r1==r2))
4/29/12

step2:
printf("\n Enter the elements of matrix A \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the elements of matrix B \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
4/29/12

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=0;
c[i][j]=c[i][j]+a[i][j]+b[i][j];
}
}
printf("\n The resultant matrix after addition of A & B is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);
4/29/12
printf("\n");

Output
Enter the size of matrix A: 2
2
Enter the size of matrix B: 2
2
Enter the elements of matrix A
2
2
2
2
Enter the elements of matrix B
3
3
3
3

4/29/12

Matrix Multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r1,r2,c1,c2;
int a[5][5],b[5][5],c[5][5];
clrscr();
step1:
printf("\n Enter the size of matrix A \n");
scanf("%d%d",&r1,&c1);
printf("\n Enter the size of matrix B \n");
scanf("%d%d",&r2,&c2);
4/29/12
if(c1==r2)

step2:
printf("\n Enter the elements of matrix A \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the elements of matrix B \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
4/29/12

for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",c[i][j]);

4/29/12

Output
Enter the size of matrix A:2
2
Enter the size of matrix B:2
2
Enter the elements of matrix A
4
4
4
4
Enter the elements of matrix B
4
4
4
4/29/12

Enter the size of matrix A:2


3
Enter the size of matrix B:3
2
Enter the elements of matrix A
1
2
3
4
5
6
Enter the elements of matrix B
2
4
4/29/12

20

32

50

80

4/29/12

Passing array to Function

Here an array is transferred as parameter to a


function.
void main()
void fun(n,b[])
{
{
void fun(int,int);
int x,b[5];
int a[5],n;
..

..
fun(n,a);

}
}
4/29/12

#include<stdio.h>

Example

#include<conio.h>
void add(int,int b[]);
void main()
{
int a[5],i,n;
clrscr();
printf("\n Enter the Number: ");
scanf("%d",&n);
printf("\n Enter the Values: ");
for(i=0;i<n;i++)
4/29/12scanf("%d",&a[i]);

void add(int x,int b[])


{
int sum=0,i;
for(i=0;i<x;i++)
sum=sum+b[i];
printf("\nThe sum is: %d",sum);
}

4/29/12

Output
Enter the Number: 5
Enter the Values: 1
2
3
4
5
The
sum
is:
15
4/29/12

Array of Characters

In array the characters are


terminated by the null (\0)
character.
Example: char a[]={a,b,c};
a

4/29/12

\0

Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
char a[]="abcd";
clrscr();
while(a[i]!='\0')
{
printf("\t%c",a[i]);
i++;
}
4/29/12

Output
a

4/29/12

Multi Dimensional Array


Syntax

datatype array_name [size1][size2].[size


n]
datatype - type of the data.
array_name -name of the array.
size
4/29/12

-size of the array.

Example:
int a[3][3][3];
Col 0

0
row 1
row 2
row

4/29/12

Col 1

Col 2

X[0][0]

X[0][1]

X[0][2]

X[1][0]

X[1][1]

X[1][2]

X[2][0]

X[2][1]

X[2][2]

/* Program on multi dimensional


array */

2 #include<stdio.h>

3 #include<conio.h>

4 void main()

5 {

/* Local definitions */

7
int
i,j,k;
4/29/12

20

} /* for */

21

print_onedim(a);

22

print_twodim(a);

23

print_threedim(a);

24 } /* main */

25 print_onedim(int a[])

26 {
27

4/29/12

int i;

34 /* Statements */

35

for(j=0;j<3;j++ )

36

print_onedim(a[j]);

37

printf("\n");

38 } /* print_twodim() */39
print_threedim(int a[][3][4])
40 {
41
4/29/12

int j;/* Local definitions */

Operations on arrays
insertion

deletion
array
searching

merging

sorting

4/29/12

You might also like