Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

C Program to Merge Two Arrays

by suresh

How to write a C Program to Merge Two Arrays with example?. Before going into this C Program to Merge Two Arrays example.

C Program to Merge Two Arrays Example 1

This program to merge two arrays in c allows the user to enter the Array size, Array elements of two different arrays. Next, it will merge two arrays one after the other using For Loop.

/* C Program to Merge Two Arrays using For Loop */
#include<stdio.h>
 
int main()
{
 	int aSize, bSize, mSize, i, j;
	int a[10], b[10], Merged[20];
  
 	printf("\n Please Enter the First Array Size  :  ");
 	scanf("%d", &aSize);
 
 	printf("\nPlease Enter the First Array Elements :  ");
 	for(i = 0; i < aSize; i++)
  	{
      	scanf("%d", &a[i]);
  	}
  	printf("\n Please Enter the Second Array Size  :  ");
 	scanf("%d", &bSize);
   
 	printf("\nPlease Enter the Second Array Elements  :  ");
 	for(i = 0; i < bSize; i++)
  	{
      	scanf("%d", &b[i]);
  	}
  	
  	for(i = 0; i < aSize; i++)
  	{
      	Merged[i] = a[i];
  	}
  	
	mSize = aSize + bSize;
 
 	for(i = 0, j = aSize; j < mSize && i < bSize; i++, j++)
  	{
  		Merged[j] = b[i];
  	}
 
 	printf("\n a[%d] Array Elements After Merging \n", mSize); 
 	for(i = 0; i < mSize; i++)
  	{
    	printf(" %d \t ",Merged[i]);
  	}
 
  	return 0;
}

OUTPUT

C Program to Merge Two Arrays using For Loop 1

ANALYSIS

In this C merge two arrays example, below For loop will help to iterate every cell present in a[3] array. Condition inside the for loops (i < Size) will ensure the compiler, not to exceed the array limit. Please refer to Array in C article to understand the concept of Array size, index position, etc.

Inside the C Programming for loop, we are assigning each and every arr array element to Merged array.  It means, Merged[0] = arr[0], Merged[1] = arr[1], Merged[2] = arr[2], Merged[3] = arr[3], Merged[4] = arr[4]

for(i = 0; i < aSize; i++)
{
      	Merged[i] = a[i];
}

Now merged[3] = {10, 20, 30}

In the next line, We have one more for loop to insert the second array elements into the Merged array

for(i = 0, j = aSize; j < mSize && i < bSize; i++, j++)
{
	Merged[j] = b[i];
}

From the above Program to Merge Two Arrays in C screenshot, you can observe that Second array elements are b[4] = {15, 25, 35, 45}

First Iteration: for(i = 0, j = aSize; j < mSize && i < bSize; i++, j++)
for (i = 0, j = 3; 3 < 7 && 0 < 4; 0++, 3++)
The condition (3 < 7 && 0 < 4) is True.
Merged[j] = b[i]
Merged[3] = b[0]
Merged[3] = 15

Second Iteration: for (i = 1, j = 4; 4 < 7 && 1 < 4; 1++, 4++)
The condition (4 < 7 && 1 < 4) is True.
Merged[j] = b[i]
Merged[4] = b[1]
Merged[4] = 25

Third Iteration: for (i = 2, j = 5; 5 < 7 && 2 < 4; 2++, 5++)
The condition (5 < 7 && 2 < 4) is True.
Merged[j] = b[i]
Merged[5] = b[2]
Merged[5] = 35

Fourth Iteration: for (i = 3, j = 6; 6 < 7 && 3 < 4; 3++, 6++)
The condition (6 < 7 && 3 < 4) is True.
Merged[j] = b[i]
Merged[6] = b[3]
Merged[6] = 45

Fifth Iteration: for (i = 4, j = 7; 7 < 7 && 4 < 4; 4++, 7++)
The condition (7 < 7 && 4 < 4) is False. So, it will exit from the For Loop

Next, we used one more for loop to print the output. I suggest you to refer the Print Array Elements article to understand this for loop.

C Program to Merge Two Arrays Example 2

This merge two arrays program will merge two arrays. While Merging it will check which number is the least value and then insert the least number first

/* C Program to Merge Two Arrays */
#include<stdio.h>
 
int main()
{
 	int aSize, bSize, mSize, i, j, k;
	int a[10], b[10], Merged[20];
  
 	printf("\n Please Enter the First Array Size  :  ");
 	scanf("%d", &aSize);
 
 	printf("\nPlease Enter the First Array Elements :  ");
 	for(i = 0; i < aSize; i++)
  	{
      	scanf("%d", &a[i]);
  	}
  	printf("\n Please Enter the Second Array Size  :  ");
 	scanf("%d", &bSize);
   
 	printf("\nPlease Enter the Second Array Elements  :  ");
 	for(i = 0; i < bSize; i++)
  	{
      	scanf("%d", &b[i]);
  	}
  	
	mSize = aSize + bSize;
	
	i = 0;
	j = 0;
	
 	for(k = 0; k < mSize; k++)
  	{
  		if(i >= aSize || j >= bSize)
  		{
  			break;
	  	}
	  	if(a[i] < b[j])
	  	{
	  		Merged[k] = a[i];
	  		i++;
		}
		else
		{
			Merged[k] = b[j];
			j++;
		}
  	}
  	
  	while(i < aSize)
  	{
  		Merged[k] = a[i];
  		k++;
  		i++;
	}
	
  	while(j < bSize)
  	{
  		Merged[k] = b[j];
  		k++;
  		j++;
	}
	 
 	printf("\n a[%d] Array Elements After Merging \n", mSize); 
 	for(i = 0; i < mSize; i++)
  	{
    	printf(" %d \t ",Merged[i]);
  	}
 
  	return 0;
}

OUTPUT

C Program to Merge Two Arrays using For Loop 2

Program to Merge Two Arrays Example 3

This program to merge two arrays is same as above, but we used Functions concept to arrange the code

/* C Program to Merge Two Arrays using Functions */
#include<stdio.h>

void Merge_Array(int a[], int aSize, int b[], int bSize, int Merged[]);
 
int main()
{
 	int aSize, bSize, mSize, i, j, k;
	int a[10], b[10], Merged[20];
  
 	printf("\n Please Enter the First Array Size  :  ");
 	scanf("%d", &aSize);
 
 	printf("\nPlease Enter the First Array Elements :  ");
 	for(i = 0; i < aSize; i++)
  	{
      	scanf("%d", &a[i]);
  	}
  	printf("\n Please Enter the Second Array Size  :  ");
 	scanf("%d", &bSize);
   
 	printf("\nPlease Enter the Second Array Elements  :  ");
 	for(i = 0; i < bSize; i++)
  	{
      	scanf("%d", &b[i]);
  	}
  	
  	Merge_Array(a, aSize, b, bSize, Merged);
  	
	mSize = aSize + bSize;
	
	printf("\n a[%d] Array Elements After Merging \n", mSize); 
 	for(i = 0; i < mSize; i++)
  	{
    	printf(" %d \t ",Merged[i]);
  	}
 
  	return 0;
} 

void Merge_Array(int a[], int aSize, int b[], int bSize, int Merged[])
{
	int i, j, k, mSize;
	j = k = 0;
	mSize = aSize + bSize;
	
	for(i = 0; i < mSize;)
	{
		if(j < aSize && k < bSize)
		{
			if(a[j] < b[k])
			{
				Merged[i] = a[j];
				j++;
			}
			else
			{
				Merged[i] = b[k];
				k++;
			}
			i++;
		}
		else if(j == aSize)
		{
			while(i < mSize)
			{
				Merged[i] = b[k];
				k++;
				i++;
			}
		}
		else
		{
			while(i < mSize)
			{
				Merged[i] = a[j];
				j++;
				i++;
			}
		}
	}
}

OUTPUT

C Program to Merge Two Arrays using For Loop 3

Placed Under: C Programs

  • C Hello World
  • C Add Two numbers
  • C Armstrong Number Program
  • C Convert Celsius to Fahrenheit
  • C Convert Decimal to Binary
  • C Convert Decimal to Octal
  • C Convert CM to Meter and KM
  • C Convert KM to M, CM & MM
  • C Convert Fahrenheit to Celsius
  • C Compound Interest Program
  • C Count Digits in a Number
  • C Count Notes in an Amount
  • C Cube of a Number
  • C Electricity Bill Program
  • C Factors of a Number Program
  • C Factorial of a Number Program
  • C GCD of Two Numbers
  • C Generic Root of a Number
  • C Gross Salary of an Employee
  • C Largest of Two Numbers
  • C Largest of Three numbers
  • C Last Digit Of a Number
  • C LCM of Two Numbers
  • C Leap Year program
  • C Multiplication Table program
  • C Natural Numbers from 1 to N
  • C Natural Numbers in reverse
  • C NCR Factorial of a Number
  • C Number is Divisible by 5 and 11
  • C Palindrome Program
  • C Palindrome String program
  • C Perfect Number Program
  • C Profit or Loss Program
  • C Print 1 to 100
  • C Print Prime Numbers 1 to 100
  • C Prime Number program
  • C Prime Factors of a Number
  • C Prime, Armstrong or Perfect
  • C Positive or Negative program
  • C Print Odd Numbers 1 to N
  • C Print Even Numbers 1 to N
  • C Print Integer, Char & Float
  • C Power of a Number program
  • C Product of Digits in a Number
  • C Roots of a Quadratic Equation
  • C Reverse a Number
  • C Simple Calculator Program
  • C Simple Interest
  • C Square of a Number
  • C Square Root of a Number
  • C Standard Deviation program
  • C Strong Number Program
  • C Student Grade program
  • C Sum of Odd Numbers program
  • C Sum of Even Nums program
  • C Sum of Even and Odd Nums
  • C Swap First & Last digit of Num
  • C Sum of First & Last digit Num
  • C Sum of Digits of a Number
  • C Sum of N Numbers Program
  • C Sum and Average of n Number
  • C Swap 2 Numbers
  • C Total, Avg & % of Five Subjects
  • C Quick Sort
  • C Selection Sort
  • C Insertion Sort
  • C Bubble Sort
  • C Fibonacci Series Program
  • C Nth Fibonacci number
  • C Sum of AP Series
  • C Sum of GP Series
  • C Sum of series 1³+2³+3³+….+n³
  • C Sum of series 1²+2²+3²+….+n²
  • C ASCII value of String chars
  • C Print Characters in a String
  • C Compare Two Strings
  • C Concatenate Two Strings
  • C Copy String
  • C String length program
  • C Convert String to Lowercase
  • C First Occurrence of string char
  • C First Occur of String Word
  • C Count Occur of string Char
  • C Count Alphabets, Digits & Special Chars
  • C Count Vowels & Consonants
  • C string remove all Occ of char
  • C remove 1st Occ of char in string
  • C remove last Occ char in string
  • C replace All Occ of char in string
  • C Replace last Occ of String Char
  • C Replace 1st occ char in string
  • C Reverse words Order in string
  • C Reverse a String
  • C Toggle Case of all char in string
  • C Array Arithmetic Operations
  • C Matrix Arithmetic Operations
  • C Count Frequency of array item
  • C Count Duplicate Array items
  • C Count +ve & -Ve Array items
  • C Count Even & Odd Array items
  • C Copy an Array to another
  • C Delete Duplicate Array items
  • C Delete an Element in an Array
  • C insert an Element in an Array
  • C 2nd largest Number in an Array
  • C Find Largest Array Number
  • C Find Smallest Array Number
  • C Largest & Smallest Array item
  • C Merge Two Arrays
  • C Print Unique Array Elements
  • C Print Arrayy Elements
  • C Print Negative Array Numbers
  • C Print Positive Array Numbers
  • C Put +ve & -Ve in 2 Arrays
  • C Put Even & Odd in 2 Arrays
  • C Program to Reverse an Array
  • C Search an Element in an Array
  • C Sum of array even, odd nums
  • C Sort Array in Ascending Order
  • C Sort Array in Descending
  • C Swap 2 Arrays without Temp
  • C Sum of all Array Elements
  • C Sum of each Matrix column
  • C sum of each row in a Matrix
  • C Sum of Matrix row & column
  • C Add Two Matrices
  • C Sparse Matrix
  • C Symmetric Matrix
  • C Identity Matrix
  • C Interchange Matrix Diagonals
  • C Check two Matrices Equal
  • C Lower Triangle of a Matrix
  • C Upper Triangle of a Matrix
  • C Sum of Lower Triangle Matrix
  • C Sum of Upper Triangle Matrix
  • C Subtract Two Matrices
  • C Transpose of a Matrix program
  • C Cuboid Volume & Surface Area
  • C Cone Volume & Surface Area
  • C Cube Volume & Surface Area
  • C Volume & Surface of Cylinder
  • C Volume & Surface of Sphere
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy