Professional Documents
Culture Documents
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];
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.
for(i=0;i<5;i++)
{
printf(“%d”,a[i]);
}
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;
}
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;
}
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
}
Int a[5][5],I,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf(“%d”,&a[i]);
}
}
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++)
{
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;
}
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;
}