Array: PPT CREATED BY: Prof. Rajesh K. Jha
Array: PPT CREATED BY: Prof. Rajesh K. Jha
Array
So far we have used only the fundamental data types, namely char, int, float, double and variations of int and
double. Although these types are very useful, they are constrained by the fact that a variable of these types can
store only one value at any given time. Therefore, they can be used only to handle limited amount of data. In
many application, however, we need to handle a large volume of data in terms of reading, processing and
printing. To process such large amounts of data, we need a powerful data type that would facilitate efficient
storing, accessing and manipulation of data items. C support a derived data type known as array that can be
used for such application.
An array is a fixed-size sequenced collection of elements of the same data type. It is simply grouping of like-type
data. In its simplest form, an array can be used to represent a list of numbers, or a list of names. Some example
where array can be used.
Data Structure Arrays and structures are referred to as structured data types
because they can be used to represent data values that have a
1. Derived Type structure of some sort. Structured data types provide an
organizational scheme that shows the relationships among the
Arrays individual elements and facilitate efficient data manipulations.
Functions In programming, such data types are known as data structures.
Pointers
In addition to arrays and structures, C supports creation and
2. Fundamental Types manipulation of the following data structures.
Direct Initialization : is the technique by which a Note: array without size definition.
value is being assigned at the time of declaration on
array.
# include <stdio.h>
# include <stdio.h> int main()
int main() {
{ int arr[] = {10,20,30,40,50};
int arr[5] = {10,20,30,40,50}; int i;
int i;
printf("\n Output of array element=\n");
printf("\n Output of array element=\n"); for(i=0; i<5;i++)
for(i=0; i<5;i++) {
{ printf("\n%d",&arr[i]);
printf("\n%d",&arr[i]); }
} return 0;
return 0; }
}
{
int main()
{ int arr[5]={1,2,3,4,5}, sum=0,i,avg=0;
int arr[5], i;
printf("enter your array element=\n"); for(i=0;i<5;i++)
for(i=0; i<5;i++) {
sum= sum+ arr[i];
{
}
scanf("%d",&arr[i]); avg=sum/5;
}
printf("\n Output of array element=\n"); printf("Sum of array elements=%d",sum)
for(i=0; i<5;i++) printf("Avg of array element=%d",avg)
{
printf("\n%d",&arr[i]); return 0;
}
return 0; }
}
b[0][0] = 10
b[0][1] = 20
b[1][0] = 30
b[1][1] = 40
[1].
printf("\n Output element of two D. array =\n");
for(i=0; i<2;i++)
# include <stdio.h> {
int main() for(j=0; j<2; j++)
{ {
int arr[2][2], i,j; printf("\n%d",arr[i][j]);
}
printf("\n Enter element in two D. array =\n"); }
[2].
# include <stdio.h> avg = sum/4;
int main()
{ printf("\n Sum of two d array element=%d”, sum");
int arr[2][2], i,j , sum=0, avg=0; printf("\n Avg of two d array element=%d”, avg");
for(i=0; i<2;i++) }
{
for(j=0; j<2; j++)
{
scanf("\n%d",&arr[i][j]);
sum = sum+arr[i][j];
}
}
PPT CREATED BY : Prof. Rajesh K. Jha 9
C - Programming Language
2. WAP in C to accept two array element printf("Enter second Array element:");
}
#include<stdio.h>
#include<conio.h> for(r=0;r<3;r++)
int main() {
{ for(c=0;c<3; c++)
int num[3][3],diag1=0,diag2=0,r,c; {
printf("Enter first Array element:"); printf("%d\t",num[r][c]);
for(r=0;r<3;r++) }
{ printf("\n");
for(c=0;c<3; c++) }
{
scanf("%d",&num[r][c]); printf("\n Sum of First diagonal
} element:%d",diag1);
}
printf("\n Sum of Second diagonal
for(c=0;c<3; c++) element:%d",diag2);
{ return 0;
diag1=diag1+num[c][c]; }
}
PPT CREATED BY : Prof. Rajesh K. Jha 11
C - Programming Language
4. Array element transposition program in 3*3
for(r=0;r<3;r++)
#include<stdio.h> {
#include<conio.h> for(c=0;c<3; c++)
{
int main() num1[r][c]=num[c][r];
{ }
int num[3][3],num1[3][3],num2[3][3], }
int r,c,sum=0,avg=0;
printf("\n Transpose of array element:\n");
printf("Enter first Array element:"); for(r=0;r<3;r++)
{
for(r=0;r<3;r++) for(c=0;c<3; c++)
{ {
for(c=0;c<3; c++) printf("%d\t",num1[r][c]);
{ }
scanf("%d",&num[r][c]); printf("\n");
} }
} return 0;
}
PPT CREATED BY : Prof. Rajesh K. Jha 12
C - Programming Language
in 3*3
1 2 3 1 4 7
4 5 6 2 5 9
7 8 9 3 6 9
for(i=0; i<2;i++)
# include <stdio.h>
{
int main()
for(j=0; j<3; j++)
{
{
int arr[2][3][4] , i,j,z;
for(k=0; k<4; k++)
{
printf("\n Input array element in 3 D. array =\n");
printf (“%d”,arr[i][j][k]);
}
for(i=0; i<2;i++)
printf(“\n”);
{
}
for(j=0; j<3; j++)
printf(“\n”);
{
}
for(k=0; k<4; k++)
{
return 0;
scanf (“%d”,arr[i][j][k]);
}
}
}
}
PPT CREATED BY : Prof. Rajesh K. Jha 17