Write a C program to merge two unsorted arrays Output
#include<stdio.h> enter the number of elements in array1: 3
void main() enter the elements of the first array: 10 20 30
{ enter the elements of the second array : 15 25 35
int arr1[10],arr2[10],arr3[10]; the merged array is
int i,n1,n2,m, index=0; Arr[0]=10 Arr[1]=20 Arr[2]=30 Arr[3]=15 Arr[4]=25
printf(“\n enter the number of elements in array1”); Arr[5]= 35
scanf(“%d”,&n1);
printf(“enter the elements of the first array”);
for(i=0;i<n1;i++) Memory Representation:
scanf(“%d”,&arr1[i]); Array1 10 20 30
printf(“enter the elements of the second array”);
for(i=0;i<n2;i++) Array2 15 25 35
scanf(“%d”,&arr2[i]);
m=n1+n2; Array3 10 20 30 15 25 35
for(i=0;i<n1;i++)
{
arr3[index]=arr1[i];
index++;
}
for(i=0;i<n2;i++)
{
arr3[index]=arr2[i];
index++;
}
printf(“the merged array is “);
for(i=0;i<m;i++)
printf(“\t arr[%d]=%d”,i,arr3[i]);
}
Searching for a value in an array :
Searching means to find whether a particular value is present in the array or not. Two popular methods
are used : Linear Search and Binary search.
1. Linear Search :
It works by comparing every element of the array one by one in sequence until a match is found. Linear
search is mostly used to search an unordered list of elements.
Steps on how it works: Here is simple approach is to do Linear Search:
• Start from the leftmost element of array and one by one compare the element we are searching for
with each element of the array.
• If there is a match between the element we are searching for and an element of the array, return the
index.
• If there is no match between the element we are searching for and an element of the array, return -1.
BPOPS103/203 Notes by : Vandana Page 1
Program to carry out linear search Output
include<stdio.h>
void main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is
found */
{
printf("%d is present at location %d.\n", search,
c+1); break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
}
2. Binary Search : Refer to record book –Binary search lab program.
PASSING ARRAYS TO FUNCTIONS:
One dimensional array for inter-function
communication
Passing individual elements Passing an entire
array
Passing data Values Passing address
BPOPS103/203 Notes by : Vandana Page 2
1. Passing Individual elements:
Method 1 : Passing data values: Method 2 : Passing address:
Here the data type of the array element must We can pass the address of an individual array
match with the type of the function parameter. element by preceding the indexed array element
In the example , only one element of the array is with the address operator(&). Therefore, to pass
passed to the called expression. This is done by the address of the fourth element of the array to
using index expression. So arr[3] actually evaluates the called function, we write it as : &arr[3]
to only one value.
main()
main() {
{ int arr[5]={1,2,3,4,5};
int arr[5]={1,2,3,4,5}; func(arr[3]);
func(arr[3]); }
} void func(int *num)
void func(int *num) {
{ printf(“%d”, *num);
printf(“%d”, *num); }
} However, in the called function the value of the
array element must be accessed using the
indirection operator.
Passing the entire array:
We know that array name refers to the first byte of the array in memory. The address of the rest of the
elements in the array can be calculated using the array name and the index value of the element.
Therefore, when we need to pass an entire array to the function, we can simply pass the name of the
array.
Code below illustrates the code which passes the entire array to the called function.
main()
{
int arr[5]={1,2,3,4,5};
func(arr);
}
void func(int arr[5])
{
int i;
for(i-0;i<5;i++)
printf(“%d”, arr[i]);
}
Write a C program to read and print an array of n numbers
#include<stdio.h>
void read_array(int arr[],int); output:
void display_array(int arr[], int); enter the size of the array : 5
void main() enter the elements of the array : 1 2 3 4 5
{ the elements of the array are : 1 2 3 4 5
int num[10],n;
BPOPS103/203 Notes by : Vandana Page 3
printf(“ enter the size of the array”);
scanf(“%d”,&n);
read_array(num,n);
display_array();
}
void read_array(int arr[],int)
{
int i;
printf(“ enter the elements of the array”);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
}
void display_array(int arr[], int)
{
printf(“ the elements of the array are :”);
for(i=0;i<n;i++)
{
printf(“%d”,arr[i]);
}
Two dimensional arrays:
A list of items can be given one variable name using two subscripts and such a variable is called a single
subscripted variable or one dimensional array. It consists of both rows and columns. Ex: Matrix
Declaration of Two-Dimensional Array:
Here is general syntax for array declaration along with examples.
Initialization of Two-Dimensional Array:
After array is declared, next is storing values in to an array is called initialization. There are two types of
array initialization: 1. Compile-time initialization 2. Run-time initialization
1. Compile time initialization: If we assign values to the array during declaration it is called compile
time initialization. Following are the different methods of compile time initialization.
After initialization the array city_temper may appear as follows:
BPOPS103/203 Notes by : Vandana Page 4
2. Run time initialization:
Run time initialization is storing values in an array when the program is running or executing.
Following example illustrates run time storing
Example: printf(“Enter the marks”);
for(i=0; i<3;i++)
for(j=0;j<3;j++)
{
scanf(“%d”, &marks[i][j]);
}
More Examples: Other way of initialization:
int a[ ][3]= { 0, 1, 2, 3,4,5,6,7,8};
int b[ ][4] ={1,2,3,4,5,6,7,8,9,10,11,12};
Example for Invalid initialization : int A[3][ ]={1,2,3};
Note: Never have column size undeclared in two dimensional array.
Operations on two-dimensional arrays:
Assume m x n matrix
a. Transpose : Transpose of a mxn matrix A is given as nxm matrix B where,
Bi,j= Ai,j
b. Sum : the matrices should have the same number of rows and columns.
Ci,j = Ai,j + Bi,j
c. Difference : the matrices should have the same number of rows and columns.
Ci,j = Ai,j + Bi,j
d. Product : two matrices can be multiplied with each other if the number of columns in the first
matrix is equal to the number of rows in the second matrix.
Ci,j = ∑ Ai,k * Bk,j for k=1 to k<n
PASSING TWO – DIMENSIONAL ARRAYS TO FUNCTIONS:
Two dimensional array for inter-function communication
Passing an entire 2D array
Passing individual elements Passing a row
Method 1 : Passing individual elements : Method 2 : Passing a Row:
Exactly same as passing elements of a one- A row of two-dimensional array can be passed by
dimensional array. indexing the array name with the row number.
When we send a single row of a two-dimensional
Here the data type of the array element must array, then the called function receives a one-
BPOPS103/203 Notes by : Vandana Page 5
match with the type of the function parameter. dimensional array.
In the example , only one element of the array is main()
passed to the called expression. This is done by {
using index expression. So arr[3] actually evaluates int arr[2][3]={{1,2,3},{4,5,6}};
func(arr[1]);
to only one value.
}
void func(int arr[])
{
int i;
for(i=0;i<3;i++)
printf(“%d”, arr[i]*10);
}
Method 3 : Passing an entire 2D array : we use the array name as the actual parameter.
Write a menu-driven program to read and display an m x n matrox. Also, find the sum,transpose , and
product of two m x n matrices.
#include<stdio.h>
void read_matrix(int mat[2][2],int,int);
void sum_matrix(int mat1[2][2], int mat2[2][2] ,int,int);
void mul_matrix(int mat1[2][2], int mat2[2][2] ,int,int);
void transpose_matrix(int mat2[2][2] ,int,int);
void display_matrix(int mat[2][2], int r,int c);
void main()
{
int option,row,col;
int mat1[2][2], mat[2][2];
do
{
printf(“\n MAIN MENU”);
printf(“\n1. read the 2 matrices”);
printf(“\n2. add the matrices”);
printf(“\n3. multiply the matrices”);
printf(“\n4. transpose the matrix”);
printf(“\n5. exit”);
printf(“\n enter the option”);
scanf(“%d”,&option);
switch(option)
{
case 1 : printf(“\n enter the number of rows and columns of the matrix”);
BPOPS103/203 Notes by : Vandana Page 6
scanf(“%d%d”,&row,&col);
printf(“\n enter the first matrix”);
read_matrix(mat1,row,col);
printf(“\n enter the second matrix”);
read_matrix(mat2,row,col);
break;
case 2: sum_matrix(mat1,mat2,row,col);
break;
case 3: if(col==row)
mul_matrix(mat1,mat2,row,col);
else
printf(“\n to multiply two matrices , number of columns in the first matrix must be equal to the number
of rows in the second matrix”);
break;
}}while(option!=5)
}
void read_matrix(int mat[2][2],int r ,int c)
{ Output:
int i,j;
MAIN MENU
for(i=0;i<r;i++)
{ [Link] the two matrices
printf(“\n”); 2. Add the matrices
for(j=0;h<c;j++) [Link] the matrices
{ [Link] the matrix
printf(“\t mat[%d][%d] = “,i,j); 5. Exit
scanf(“%d”,&mat[i][j]);
Enter your option : 1
}}}
void sum_matrix(int mat1[2][2], int mat2[2][2] ,int r,intEnter
c) the number of rows and columns of the
{ matrix : 2 2
int i,j,sum[2][2]; Enter the first matrix:
for(i=0;i<r;i++) mat[0][0]=1 mat[0][1]=2
{ mat[1][0]= 3 mat[1][1]=4
for(j=0;h<c;j++) Enter the second matrix:
{
mat[0][0]=2 mat[0][1]=3
sum[i][j]=mat1[i][j]+mat2[i][j];
} mat[1][0]= 4 mat[1][1]=5
display_matrix(sum,r,c); MAIN MENU
} [Link] the two matrices
void mul_matrix(int mat1[2][2], int mat2[2][2] ,int r,int 2.
c) Add the matrices
{
[Link] the matrices
int i,j,k,prod[2][2];
[Link] the matrix
for(i=0;i<r;i++)
{ 5. Exit
for(j=0;h<c;j++) Enter your option : 2
{ mat[0][0]=3 mat[0][1]=5
prod[i][j]=0; mat[1][0]= 7 mat[1][1]=9
for(k=0;k<c;k++)
prod[i][j] + =mat1[i][k]*mat2[k][j];
}
}
BPOPS103/203 Notes by : Vandana Page 7
display_matrix(prod,r,c);
}
void transpose_matrix(int mat[2][2], int r,int c)
{
int i,j,tp_mat[2][2];
for(i=0;i<r;i++)
{
for(j=0;h<c;j++)
{
tp_mat[j][i]=mat[i][j];
}
display_matrix(tp_mat,r,c);
}
void display_matrix(int mat[2][2] ,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
printf(“\n”);
for(j=0;h<c;j++)
printf(“\tmat[%d][%d] = %d”,i,j,mat[i][j]);
}
}
Multi-Dimensional Array: it can have 3, 4 or more [Link] has rows,columns and depth associated
with it.
A simple program to read and display a 2x2x2 array.
#include<stdio.h> Output:
Void main() enter the elements of the matrix
{ 12345678
Int arr[2][2][2], I,j,k; The matrix is :
Printf(“enter the elements of the matrix”); Arr[0][0][0]= 1 Arr[0][0][1]= 1
For (i=0;i<2;i++) Arr[0][1][0]= 1 Arr[0][1][1]= 1
{ Arr[1][0][0]= 1 Arr[1][0][1]= 1
For (j=0;j<2;j++) Arr[1][1][0]= 1 Arr[1][1][1]= 1
{
For (k=0;k<2;k++)
{
Scanf(“%d”,&arr[i][j][k]);
}}}
Printf(“The matrix is :”);
For (i=0;i<2;i++)
{
Printf(“\n\n”);
For (j=0;j<2;j++)
{
Printf(“\n”);
For (k=0;k<2;k++)
BPOPS103/203 Notes by : Vandana Page 8
Printf(“\t arr[%d][%d][%d] = %d “ , i,j,k,arr[i][j][k]);
}}}
Application of arrays:
1. Arrays are mainly used to implement mathematical vectors, matrices or other kinds of
rectangular tables.
2. Many databases include one-dimensional arrays whose elements are records.
3. Arrays can be used to sort elements in ascending and descending order.
4. Arrays are also used to implement other data structures such as strings, stack, queues, heap,
hash tables .
Practice Questions:
Write a C-program to print the position of the smallest of n numbers using array.
Write a C-program to transpose a 3x3 matrix.
Write a C-program to add the 2 two-dimensional Matrix.
BPOPS103/203 Notes by : Vandana Page 9