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.
#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;
}
Comments
Leave a comment