You are on page 1of 8

UNIT-3 ARRAYS

Arrays:
Array is a collection of elements of same datatype.
Or
Array is a homogeneous collection of elements of same datatype
Or
Array is a group of logically related data items of the same datatype referred by single name or
common name.
• In array all the elements are contiguous memory located.
• All the values are contiguous are stored in the memory location.
Syntax datatype variable[size];
eg: int students[64];
float height[64];
char name[64];

• What is the difference between normal variable and array variable


Normal Variable Array Variable
Normal variable is a capability of holding one Array Variable is a capability of holding
value at a time multiple values under common name
depending on their size

Types of Array:
• Depending on its size it can be divided into 3 types
1) One-Dimensional Array
2) Two-Dimensional Array
3) Multi-Dimensional Array
1) One Dimensional Array:
• They are represents the rows of values.

Syntax datatype variable[size];


eg: int students[64];

Processing One Dimensional Array:



Processing the array depends on two factors i.e.,
1) Reading the Array
2) Writing the Array
1) Reading the Array:
• It is the easiest way to read the values into a array by using for loop
• For eg:
Int a[5];

a[0] a[1] a[2] a[3] a[4]


• th
Array index position must be starting from 0 position to (n-1) position.
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
• If the user enter 5 values they are stored in a[0],a[1],a[2],a[3],a[4],a[5].

Basi Reddy Page 1 of 8


UNIT-3 ARRAYS

2) Writing the Array:


• It is the easiest way to write or display array elements by using for loop

for(i=0;i<5;i++)
{
printf(“%d”,a[i]);
}

• If the user enter 5 values they display in a[0],a[1],a[2],a[3],a[4],a[5].

Write a Program to display the given array elements by using one dimensional array
#include<stdio.h> Enter rows =5
int main() 1
{ 2
int a[100],r,i; 3
printf("Enter rows ="); 4
scanf("%d",&r); 5
for(i=0;i<r;i++) 1 2 3 4 5
{
scanf("%d",&a[i]);
}
for(i=0;i<r;i++)
{
printf("%d\t",a[i]);
}
return 0;
}

Write a Program to sum the given array elements by using one dimensional array
#include<stdio.h> Array size is=5
int main() 10
{ 20
int a[100],n,i,sum=0; 30
printf("Array size is="); 40
scanf("%d",&n); 50
for(i=0;i<n;i++) Sum=150
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("Sum=%d",sum);
return 0;
}

Basi Reddy Page 2 of 8


UNIT-3 ARRAYS

Write a Program to find the maximum number in the array elements by using one
dimensional array
#include<stdio.h> Enter array elements=1
int main() 2
{ 3
int a[10],max,i; 4
printf("Enter array elements="); 5
for(i=0;i<10;i++) 6
scanf("%d",&a[i]); 4
max=a[0]; 3
for(i=1;i<=9;i++) 2
{ 1
if(max<a[i]) Max=6
{
max=a[i];
}
}
printf("Max=%d",max);
}

Write a Program to find the minimum number in the array elements by using one
dimensional array
#include<stdio.h> Enter array elements=1
int main() 2
{ 3
int a[10],min,i; 4
printf("Enter array elements="); 0
for(i=0;i<10;i++) 6
scanf("%d",&a[i]); 7
min=a[0]; 8
for(i=1;i<=9;i++) 9
{ 10
if(min>a[i]) Minimum=0
{
min=a[i];
}
}
printf("Minimum=%d",min);
return 0;
}

Basi Reddy Page 3 of 8


UNIT-3 ARRAYS

Write a Program to find the reverse order of array elements by using one dimensional
array
#include<stdio.h> enter array of the elements=1
int main() 2
{ 3
int a[10],i,j,t; 4
printf("enter array of the elements="); 5
for(i=0;i<10;i++) 6
scanf("%d",&a[i]); 7
i=0; 8
j=9; 9
while(i<j) 10
{ Reverse of a array is=
t=a[i]; 10
a[i]=a[j]; 9
a[j]=t; 8
i++; 7
j--; 6
} 5
printf("Reverse of a array is=\n"); 4
for(i=0;i<10;i++) 3
printf("%d\n",a[i]); 2
return 0; 1
}

2) Two Dimensional Array:


• They are represents the rows and columns of values to prepare the tables, matrix
formats.

Syntax datatype variable[size][size];


eg: int students[64][64];

Processing the Two Dimensional Array:


• Processing the array depends on two factors i.e.,
1) Reading the Array
2) Writing the Array
1) Reading the Array:
• It is the easiest way to read the values into a array by using nested for loop
• For eg:

Int a[5][5],I,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf(“%d”,&a[i]);
}
}

Basi Reddy Page 4 of 8


UNIT-3 ARRAYS

2) Writing the Array:


• It is the easiest way to write or display array elements by using nested for loop

for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf(“%d”,a[i]);
}
}

Write a Program to display the given array elements by using two dimensional array
#include<stdio.h> Enter rows and columns=3
int main() 3
{ enter elements=1
int a[100][100],r,c,i,j; 2
printf("Enter rows and columns="); 3
scanf("%d%d",&r,&c); 4
printf("enter elements="); 5
for(i=0;i<r;i++) 6
{ 7
for(j=0;j<c;j++) 8
{ 9
scanf("%d",&a[i][j]); output=
} 1 2 3
} 4 5 6
printf("output=\n"); 7 8 9
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}

Write a Program to display the transpose of a given array elements by using two
dimensional array
#include<stdio.h> enter rows and columns=2
int main() 2
{ 1
int a[100][100],r,c,i,j; 2
printf("enter rows and columns="); 3
scanf("%d%d",&r,&c); 4
for(i=0;i<r;i++) 1 3
{ 2 4
for(j=0;j<c;j++)
{

Basi Reddy Page 5 of 8


UNIT-3 ARRAYS

scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
return 0;
}

Write a Program for matrix addition by using two dimensional array


#include<stdio.h> enter rows and columns do u want A=2
int main() 2
{ enter rows and columns do u want B=2
int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j; 2
printf("enter rows and columns do u want A="); enter values in A=1 2 3 4
scanf("%d%d",&r1,&c1); enter values in B=1 2 3 4
printf("enter rows and columns do u want B="); Display result matrix=
scanf("%d%d",&r2,&c2); 2 4
printf("enter values in A="); 6 8
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("enter values in B=");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Display result matrix=\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}

Basi Reddy Page 6 of 8


UNIT-3 ARRAYS

Write a Program for matrix trace by using two dimensional array


#include<stdio.h> enter size=2 2
int main() enter elements=1
{ 2
int a[10][10],i,j,t=0,r,c; 3
printf("enter size="); 4
scanf("%d %d",&r,&c); trace=5
printf("enter elements=");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
t=t+a[i][j];
}
}
}
printf("trace=%d",t);
return 0;
}

Write a Program for matrix multiplication by using two dimensional array


#include<stdio.h> enter rows and columns do u want
int main() A=2 2
{ enter rows and columns do u want
int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k; B=2 2
printf("enter rows and columns do u want A="); enter values in A=1 2 3 4
scanf("%d%d",&r1,&c1); enter values in B=1 2 3 4
printf("enter rows and columns do u want B="); Display result matrix=
scanf("%d%d",&r2,&c2); 7 10
printf("enter values in A="); 15 22
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("enter values in B=");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{

Basi Reddy Page 7 of 8


UNIT-3 ARRAYS

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

3) Multi-Dimensional Array:
• These are represented more than two dimensional.
datatype variable[size][size][size]-----;
eg: int a[10][10][10][10];

Write a Program to display the given array elements by using multi-dimensional array
#include<stdio.h> enter array elements=1 2 3 4 5
int main() 6 7 8 9 10
{ 11 12 13 14 15
int a[3][3][3],i,j,k; 16 17 18 19 20
printf("enter array elements="); 21 22 23 24 25
for(i=0;i<=2;i++) 26 27
for(j=0;j<=2;j++) output=
for(k=0;k<=2;k++) 1 2 3
scanf("%d",&a[i][j][k]); 4 5 6
printf("output=\n"); 7 8 9
for(i=0;i<=2;i++)
{ 10 11 12
for(j=0;j<=2;j++) 13 14 15
{ 16 17 18
for(k=0;k<=2;k++)
{ 19 20 21
printf("%5d\t",a[i][j][k]); 22 23 24
} 25 26 27
printf("\n");
}
printf("\n");
}
return 0;
}

Basi Reddy Page 8 of 8

You might also like