Module3_TPS
Module3_TPS
MODULE III
ARRAYS
Introductions
Definition:
An array is a collection of elements of the same data type which is stored in consecutive
memory locations. Each data item of an array is called an element and each element is located in
separated memory location.
Each value in an array is indicated by the same name that is array name and an index which
indicates the position of value in an array. The datatype of an array can be int, float, double and
char.
Representation of index:
In the below example, ‘a’ represents the name of an array. ‘0’ to ‘4’ represents the index.
The index always begins from zero (0) and continues till (size-1) position.
Ex: int a[5];
a[0] a[1] a[2] a[3] a[4]
a
Types of Arrays:
3. Partial Initialization:
Ex: int a[5]={10,20};
a[0] a[1] a[2] a[3] a[4]
a 10 20 0 0 0
C PROGRAMMING FOR PROBLEM SOLVING
Ex:
To access individual element,
printf(“ %d”,&a[0]);
To access all the elements,
for(i=0;i<n;i++)
C PROGRAMMING FOR PROBLEM SOLVING
{
printf(“%d\t”, &a[i]); (“/t” is used to get spaces in between the elements)
}
#include<stdio.h>
main()
{
int a[10], n, i;
printf(“ Enter the size of an array”);
scanf(“%d”,&n);
printf(“ Enter the elements of an array”);
for(i=0;i<n;i++)
{
scanf(“%d”, &a[i]);
}
printf(“ The array elements are:”);
for(i=0;i<n;i++)
{
printf(“%d\t”, &a[i]);
}
}
Output:
Enter the size of an array
5
Enter the elements of an array
1 2 3 4 5
The array elements are:
1 2 3 4 5
#include<stdio.h>
void main()
{
int i,a[20],n,key;
printf("Enter the number of
elements: ");
scanf("%d",&n);
C PROGRAMMING FOR PROBLEM SOLVING
small=a[i];
pos=i;
}
}
printf("The smallest element is %d
and the position is
%d",small,pos+1);
}
Output:
Enter the number of elements: 5
Enter the elements: 2 3 4 5 6
The smallest element is 2 and the position is 1
Operations on array
1. Traversing an array
2. Inserting an element in an array
3. Deleting an element from an array
4. Merging 2 arrays
5. Searching an element in an array
6. Sorting an array in ascending or descending order
Traversing an array
Traversing an array means accessing each and every element of the array for a specific
purpose.
Inserting an element in an array
Inserting an element in an array means adding a new data element to an already existing
array.
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be inserted: ");
scanf("%d",&num);
printf("Enter the postion at which number has to be inserted: ");
scanf("%d",&pos);
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=num;
n++;
printf("The array after insertion of %d is :",num);
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
Output:
Enter the number of elements: 5
Enter the elements: 1 2 4 5 6
Enter the number to be inserted: 3
Enter the postion at which number has to be inserted: 2
The array after insertion of 3 is : 1 2 3 4 5 6
for(i=pos;i<n-1;i++)
a[i]=a[i+1];
n--;
printf("The array after deletion is :");
for(i=0;i<n;i++)
printf("\nA[%d]=%d",i,a[i]);
}
Output:
Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
Enter the postion from which number has to be deleted: 4
The array after deletion is :
A[0]=1
A[1]=2
A[2]=3
A[3]=4
Merging 2 arrays
Merging of 2 arrays in a third array means first copying the contents of the first array into the
third array and then copying the contents of second array into the third array.
Hence, the merged array contains contents of the second array.
{
a3[index]=a2[i];
index++;
}
printf("\n\nThe merged array is\n");
for(i=0;i<m;i++)
printf("\t Arr3[%d]=%d\n",i,a3[i]);
}
Output:
Enter the number of elements in array1:3
Enter the elements in array1:1 2 3
Enter the number of elements in array2:3
Enter the elements in array2:4 5 6
The merged array is
Arr3[0]=1
Arr3[1]=2
Arr3[2]=3
Arr3[3]=4
Arr3[4]=5
Arr3[5]=6
}
else
{
a3[index]=a2[index_2];
index_2++;
}
index++;
}
if(index_1==n1)//if elements of the first array are over and the second array
has some elements
{
while(index_2<n2)
{
a3[index]=a2[index_2];
index_2++;
index++;
}
}
else if(index_2==n2) //if elements of the second array are over and the
first array has some elements
{
while(index_1<n1)
{
a3[index]=a1[index_1];
index_1++;
index++;
}
}
printf("\n\nThe contenets of merged array are");
for(i=0;i<m;i++)
printf("\n Arr[%d] = %d",i,a3[i]);
}
Output:
Enter the number of elements in array1:3
Enter the elements in array1:4 5 6
Enter the number of elements in array2:3
Enter the elements in array2:1 2 3
C PROGRAMMING FOR PROBLEM SOLVING
}
if(found==0)
printf("Element not found in the array");
}
Output:
Enter the number of elements in an array: 5
Enter the elements: 50 9 6 7 1
Enter the number that has to be searched: 6
6 is found in the array at position 3
Binary Search:
#include<stdio.h>
void main()
{
int i,low,high,mid,n,key,a[20];
printf("Enter the number of elements in an array: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the value to find: ");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("%d found at location %d",key,mid+1);
break;
}
else if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
if(low>high)
C PROGRAMMING FOR PROBLEM SOLVING
}
}
printf("Array after implememting bubble sort:");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
Output:
Enter the number of elements in an array: 5
Enter the elements: 90 7 6 100 99
C PROGRAMMING FOR PROBLEM SOLVING
a[1][1]=40;
10 20
30 40
Ex:
To access individual element,
printf(“ %d”,a[0][1]);
#include<stdio.h>
main()
{
int a[10][10],m, j, n, i;
printf(“ Enter the no of rows and columns of an array”);
scanf(“%d%d”,&m, &n);
printf(“ Enter the elements of an array”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”, &a[i][j]);
}
}
printf(“ The array elements are:”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”, a[i][j]);
}
printf(“\n”);
}
}
Output:
Enter the no of rows and columns of an array
2 2
Enter the elements of an array
1 2 3 4
The array elements are:
1 2
3 4
#include<stdio.h>
void main()
{
int a[5][5]={0},row=2,col,i,j;
a[0][0]=a[1][0]=a[1][1]=1;
C PROGRAMMING FOR PROBLEM SOLVING
while(row<5)
{
a[row][0]=1;
for(col=1;col<=row;col++)
a[row][col]=a[row-1][col-1]+a[row-1][col];
row++;
}
for(i=0;i<5;i++)
{
printf("\n");
for(j=0;j<=i;j++)
printf("%d\t",a[i][j]);
}
}
Output:
1
11
121
1331
14641
1. Transpose
2. Sum
3. Difference
4. Product
#include<stdio.h>
void main()
{
int a[20][20],m,n,i,j,b[20][20];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[j][i];
}
}
printf("The elemnts of transposed matrix are:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
}
Output:
Enter the number of rows and columns: 3,3
Enter the elements of the array:1 2 3 4 5 6 7 8 9
The array elements are:
123
456
789
The elements of transposed matrix are:
147
258
369
Write a program to input 2 m x n matrices and then calculate the sum of their corresponding
elements and store it in third m x n matrix.
#include<stdio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],m,n,p,q,r,t,i,j;
C PROGRAMMING FOR PROBLEM SOLVING
}
for(i=0;i<r;i++)
{
for(j=0;j<t;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("The elements of the resultant matrix are:\n");
for(i=0;i<r;i++)
{
for(j=0;j<t;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
Enter the number of rows and columns in first matrix: 2,2
C PROGRAMMING FOR PROBLEM SOLVING
Write a program to input 2 m x n matrices and then calculate the product of their corresponding
elements and store it in third m x n matrix.
#include<stdio.h>
void main()
{
int a[20][20],b[20][20],c[20][20],m,n,p,q,k,i,j;
printf("Enter the number of rows and columns in first matrix: ");
scanf("%d,%d",&m,&n);
printf("Enter the number of rows and columns in second matrix: ");
scanf("%d,%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication is not possible");
}
printf("Enter the elements of the array 1:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
}
printf("The elements of the resultant matrix are:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
Enter the number of rows and columns in first matrix: 2,2
Enter the number of rows and columns in second matrix: 2,2
Enter the elements of the array 1:2 2 2 2
Enter the elements of the array 2:2 2 2 2
The elements of the resultant matrix are:
88
88
for(i=0;i<n;i++)
square(a[i]);
}
void square(int x)
{
printf("%d\t",x*x);
return;
}
Output:
Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
The square of given elements are: 1 4 9 16 25
printf("Average=%d",Average);
}
Output:
Average=3
• Like we have 1 index in 1-D array, 2 index in 2-D array, we have n index in n-dimensional
array.
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf("a[%d][%d][%d]=%d\t",i,j,k,a[i][j][k]);
}
printf("\n");
}
}
}
Output:
Enter the elements of the matrix: 1 2 3 4 5 6 7 8 9
The matrix is:
a[0][0][0]=1 a[0][0][1]=2
a[0][1][0]=3 a[0][1][1]=4
a[1][0][0]=5 a[1][0][1]=6
a[1][1][0]=7 a[1][1][1]=8
Applications of array
Storing and accessing data: Arrays are used to store and retrieve data in a specific order. For
example, an array can be used to store the scores of a group of students, or the temperatures
recorded by a weather station.
• Sorting: Arrays can be used to sort data in ascending or descending order. Sorting algorithms
such as bubble sort, merge sort, and quick sort rely heavily on arrays.
• Searching: Arrays can be searched for specific elements using algorithms such as linear search
and binary search.
• Matrices: Arrays are used to represent matrices in mathematical computations such as matrix
multiplication, linear algebra, and image processing.
C PROGRAMMING FOR PROBLEM SOLVING
• Stacks and queues: Arrays are used as the underlying data structure for implementing stacks
and queues, which are commonly, used in algorithms and data structures.
• Graphs: Arrays can be used to represent graphs in computer science. Each element in the array
represents a node in the graph, and the relationships between the nodes are represented by the
values stored in the array.