Question
Please write in C++ only. CODE: #include #include using namespace std; //-------------------------------------------------------------- class ArrayIns { private : vector double > v; //vector v int nElems;
Please write in C++ only.
CODE:
#include
#include
using namespace std;
//--------------------------------------------------------------
class ArrayIns {
private:
vector double > v; //vector v
int nElems; /umber of data items
//--------------------------------------------------------------
public:
ArrayIns(int max): nElems(0) //constructor
{
v.resize(max); //size the vector
}
//--------------------------------------------------------------
void insert(double value) //put element into array
{
v[nElems] = value; //insert it
nElems++; //increment size
}
//--------------------------------------------------------------
void display() //displays array contents
{
for (int j = 0; j
cout
cout
}
//--------------------------------------------------------------
void insertionSort() {
int in , out;
for (out = 1; out
{
double temp = v[out]; //remove marked item
in = out; //start shifts at out
while ( in > 0 && v[ in -1] >= temp) //until one is smaller,
{
v[ in ] = v[ in -1]; //shift item to right
-- in ; //go left one position
}
v[ in ] = temp; //insert marked item
} //end for
} //end insertionSort()
//--------------------------------------------------------------
}; //end class ArrayIns
////////////////////////////////////////////////////////////////
int main() {
int maxSize = 100; //array size
ArrayIns arr(maxSize); //create array
arr.insert(77); //insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); //display items
arr.insertionSort(); //insertion-sort them
arr.display(); //display them again
return 0;
} //end main()
A[]={1,4,7,8,12}B[]={2,3,10} Output: A[]={1,2,3,4,7}B[]={8,10,12} The idea is simple. Consider each array element A[] and ignore it if it is already in the correct order (i.e., the element smallest among all remaining elements); otherwise, swap it with the smallest element, which happens to be the first element of B[]. After swapping, move the element (now present at B[0] ) to its correct position in B[] to maintain the sorted order. Using a temp variable when swapping is of course allowed. The steps are shown in blue. A={1,4,7,8,12}B={2,3,10}smallest=2A={1,2,7,8,12}B={4,3,10}moveB[0]B={3,4,10}smallest=3A={1,2,3,8,12}B={7,4,10}moveB[0]B={4,7,10}smallest=4A={1,2,3,4,12}B={8,7,10}moveB[0]B={7,8,10}smallest=7A={1,2,3,4,7}B={12,8,11}moveB[0]B={8,11,12}smallest=8 Write a program to ask he user for the two array sizes a and b. Fill two arrays A and B with random numbers 0-99 then sort and display them. Apply the above algorithm and display the resulting two arrays. You may use any sort method. Write a merge method that merges two sorted arrays A[ and B[ returns a new array C[] containing elements from both A and B in sorted order. You may not create then sort C[]. A[]={1,4,7,8,12}B[]={2,3,10}C[]={1,2,3,4,7,8,10,12} Sample run: Enter a, b : 53 Input: 1011225358146094 Output: 1011142253586094 Merged: 1011142253586094 Enter a, b : 35 Input: 567487 1919263397 Output: 191926 3356748797 Merged: 1919263356748797
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started