Answer to Question #277781 in C++ for Zain Abbas

Question #277781

Write a program to merge three sorted(ascending) arrays, A1, A2 and A3 and store results back in these arrays. You are required to place results (after sorting) in A3, A2 and A1 in ascending order, i.e., A3 contains smallest set of numbers then A2 and then A3. Take user input for each array of size 5.


1
Expert's answer
2021-12-09T17:37:17-0500
#include<iostream>
using namespace std;
void selectionSort(int array[], int size) {
   int x, j, min, t;
   for (x = 0; x < size - 1; x++) {
      min = x;
      for (j = x + 1; j < size; j++)
      if (array[j] < array[min])
      min = j;
      t = array[x];
      array[x] = array[min];
      array[min] = t;
   }
}
void merge(int a1[], int a2[], int a3[], int a1_size, int a2_size, int a3_size){
	int size = a1_size + a2_size + a3_size;
int res[size];
for(int i=0; i<a1_size; i++){
	res[i] = a1[i];
}
int start  = a1_size;
int last  = a1_size + a2_size;
for(int i=start; i<last; i++){
	res[i] = a2[i];
}
selectionSort(res, size);
start = last - 1;
last = last + a3_size;


for(int i=start; i<last; i++){
	res[i] = a3[i];
}


for(int i= 0; i<size; i++){
	cout<<res[i]<<"  ";
}


}
int main(){
	int n;
	 cout<<"Enter the elements of the of the first array: \n";
	 int A1[5], A2[5], A3[5];
	 for(int i=0; i<5; i++){
	 	cin>>A1[i];
	 }
	 
	 for(int i=0; i<5; i++){
	 	cin>>A2[i];
	 }
	 
	 for(int i=0; i<5; i++){
	 	cin>>A3[i];
	 }
	 merge(A1, A2, A3, 5,5,5);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Zain
10.12.21, 08:51

Great! I was facing some problems. And you removed them in a very professional manner. I would like to give you a five star rating.

Leave a comment

LATEST TUTORIALS
New on Blog