Answer to Question #198807 in C++ for adil

Question #198807

We are given two sorted array of size 15 and of type int (take input from user) (all elements are in

ascending order. We need to merge these two arrays such that the initial numbers (after complete

sorting) are in the first array and the remaining numbers are in the second array.


1
Expert's answer
2021-05-27T01:01:41-0400
#include <iostream> 



using namespace std;




void selectionSort(int values[]) {
	int i, j, indexmin;
	int size=30;
	for(i = 0; i<size-1; i++) {
		indexmin = i;  
		for(j = i+1; j<size; j++)
			if(values[j] < values[indexmin])
				indexmin = j;


		int temp;
		temp = values[i];
		values[i] = values[indexmin];
		values[indexmin] = temp;
	}
}


int main (){
	int sortedArray1[15];
	int sortedArray2[15];
	int tempArray[30];
	cout<<"Enter the values of the first array\n";
	for(int i=0;i<15;i++){
		cout<<"Enter the number["<<i<<"]: ";
		cin>>sortedArray1[i];
	}
	cout<<"\n\nEnter the values of the second array\n";
	for(int i=0;i<15;i++){
		cout<<"Enter the number["<<i<<"]: ";
		cin>>sortedArray2[i];
	}
	for(int i=0;i<15;i++){
		tempArray[i]=sortedArray1[i];
	}
	int counter=0;
	for(int i=15;i<30;i++){
		tempArray[i]=sortedArray2[counter];
		counter++;
	}


	selectionSort(tempArray);
	for(int i=0;i<15;i++){
		sortedArray1[i]=tempArray[i];
	}
	counter=0;
	for(int i=15;i<30;i++){
		sortedArray2[counter]=tempArray[i];
		counter++;
	}
	cout<<"\nThe values of the first array:\n";
	for(int i=0;i<15;i++){
		cout<<sortedArray1[i]<<" ";
	}
	cout<<"\n\nThe values of the second array\n";
	for(int i=0;i<15;i++){
		cout<<sortedArray2[i]<<" ";
	}
	cout<<"\n\n";


	system("pause");
	return 0;
}




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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog