Using inheritance, extend the template class indexList (posted on UR Courses) wi
ID: 2246384 • Letter: U
Question
Using inheritance, extend the template class indexList (posted on UR Courses) witih the following functions: e Concatenate two indexed lists. . Sort an indexed list using the selection sort algorithm (declared in indexList but not Find the position of a target using the binary search algorithm. Write a driver program that will create two indexed lists of integers, concatenate the two lists, sort the resuing list and then displayi. In addition e driver will search for a target in the resulting list A sample run follows. List 1 of type "int" Enter nunber of list itens: 4 Enter next item: 11 Enter next item: 7 Enter next item: 5 Enter next item: 34 List 2 of type -int" Enter nunber of list itens: 3 Enter next item: 0 Enter next item: 2 Enter next item: 49 List 3: List 1 concatenated to List 2 11, 7, 5, 34, 0, 2, 49 List 3 sorted in ascending order 0, 2, 5, 7, 11, 34, 49 Enter the number to search in List 3: 11 11 is at position 5 1. The header, implementation and driver programs should be respectively named: IndexList.h IndexList.cpp, ExtIndexList.h, ExtIndexList.cpp and TestExtIndexList.cpp.Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r)
{
int m = l + (r-l)/2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
int main()
{
int n1,n2;
cout<<"List 1 of type int"<<endl;
cout<<"Enter the number of list items: ";
cin>>n1;
int list1[n1];
for (int i=0;i<n1;i++){
cout<<" Enter next item";
cin>>list1[i];
}
cout<<"List 2 of type int"<<endl;
cout<<"Enter the number of list items: ";
cin>>n2;
int list2[n2];
for (int i=0;i<n2;i++){
cout<<" Enter next item";
cin>>list2[i];
}
cout<<" List 3 list 1 concatneted to list1 ";
int m=n1+n2;
int list[m];
int max=n1>n2?n1:n2;
for(int i=0;i<max;i++){
if(i<n1)
list[i]=list1[i];
if(i<n2)
list[n1+i]=list2[i];
}
for (int i=0;i<m;i++){
cout<<list[i]<<" ";
}
cout<<endl;
selectionSort(list, m);
cout<<"List3 sorted in assending order ";
for (int i=0;i<m;i++){
cout<<list[i]<<" ";
}
cout<<endl;
int num;
cout<<" enter the number in search in list 3: ";
cin>>num;
int find=binarySearch(list,0,m,num);
if(find==-1)
cout<<" item not found";
else
cout<<num<<" is at position "<<find+1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.