
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Merging elements of two different arrays alternatively in third array in C++.
Problem statement
Given two arrays, we need to combine two arrays in such a way that the combined array has alternate elements of the first and second array. If one of the arrays has an extra element, then these elements should be appended at the end of the combined array.
arr1[] = {10, 20, 30, 40} arr2[] = {-10, -20, -30, -40} result[] = {10, -10, 20, -20, 30, -30, 40, -40}
Algorithm
1. Traverse both arrays and one by one put elements into result array. 2. If one of the array exhausts then put remaining elements of other array into result array.
Example
#include <iostream> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; void alternateMergedArray(int *arr1, int n1, int *arr2, int n2,int *result){ int i, j, k; i = 0; j = 0; k = 0; while (i < n1 && j < n2) { result[k] = arr1[i]; ++k; ++i; result[k] = arr2[j]; ++k; ++j; } while (i < n1) { result[k] = arr1[i]; ++k; ++i; } while (j < n2) { result[k] = arr2[j]; ++k; ++j; } } void displayArray(int *arr, int n){ for (int i = 0; i < n; ++i) { cout << arr[i] << " "; } cout << endl; } int main(){ int arr1[] = {10, 20, 30, 40}; int arr2[] = {-10, -20, -30, -40}; int result[SIZE(arr1) + SIZE(arr2)]; cout << "First array: " << endl; displayArray(arr1, SIZE(arr1)); cout << "Second array: " << endl; displayArray(arr2, SIZE(arr2)); cout << "Result array: " << endl; alternateMergedArray(arr1, SIZE(arr1), arr2, SIZE(arr2),result); displayArray(result, SIZE(result)); return 0; }
Output
When you compile and execute the above program. It generates the following output −
First array: 10 20 30 40 Second array: -10 -20 -30 -40 Result array: 10 -10 20 -20 30 -30 40 -40
- Related Articles
- Alternatively merging two arrays - JavaScript
- Merging two unsorted arrays in sorted order in C++.
- Print array elements in alternatively increasing and decreasing order in C++
- Merging two sorted arrays into one sorted array using JavaScript
- Merging two arrays in a unique way in JavaScript
- Merging Arrays in Perl
- Merging nested arrays to form 1-d array in JavaScript
- Maximum OR sum of sub-arrays of two different arrays in C++
- Quickly merging two sorted arrays using std::merge() in C++ STL(cute ho ap)
- Counting elements in two arrays using C++
- Merging and rectifying arrays in JavaScript
- Plotting two different arrays of different lengths in matplotlib
- Java program to merge two or more files alternatively into third file
- C++ Program to find Median of Elements where Elements are stored in 2 different arrays
- Combine two different arrays in JavaScript

Advertisements