CREATE A DETAILED PSEUDOCODE FOR THE FOLLOWING SOURCE CODE: #include <algorithm>
ID: 3846443 • Letter: C
Question
CREATE A DETAILED PSEUDOCODE FOR THE FOLLOWING SOURCE CODE:
#include <algorithm>
#include <iostream>
#include <climits>
#include<cstdlib>
#include <ctime>
#include <vector>
using namespace std;
void displayVector(vector<int> vect)
{
std::cout<<" Vector: ";
for(int i = 0; i < vect.size(); i++)
{
std::cout << vect[i] << " ";
}
}
void mergeVA(vector<int> &vect, int size, int l, int middle, int h)
{
int i, j, k;
int n1 = middle - l + 1;
int n2 = h - middle;
int leftSide[n1], rightSide[n2];
for (i = 0; i < n1; i++)
leftSide[i] = vect[l + i];
for (j = 0; j < n2; j++)
rightSide[j] = vect[middle + 1+ j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (leftSide[i] <= rightSide[j])
{
vect[k] = leftSide[i];
i++;
}
else
{
vect[k] = rightSide[j];
j++;
}
k++;
}
while (i < n1)
{
vect[k] = leftSide[i];
i++;
k++;
}
while (j < n2)
{
vect[k] = rightSide[j];
j++;
k++;
}
}
void sort(vector<int> &vect, int size, int l, int h)
{
if (l < h){
int middle = (l + h) / 2;
sort(vect, size, l, middle);
sort(vect, size, middle+1, h);
mergeVA(vect, size, l, middle, h);
}
}
unsigned search(vector<int> vect, int val)
{
size_t mid, left = 0 ;
size_t right = vect.size();
while (left < right)
{
mid = left + (right - left)/2;
if (val > vect[mid]){
left = mid+1;
}
else if (val < vect[mid]){
right = mid;
}
else {
if(val== vect[mid])
return mid;
}
}
return UINT_MAX;
}
int main()
{
vector<int> vect;
int value,n,RandMax;
cout<<"Enter maximum value: ";
cin>>RandMax;
cout<<"Enter the number 50 to create vector: ";
cin>>n;
for(int i=0;i<n;i++)
{
value=rand() % RandMax + 1;
vect.push_back(value);
}
displayVector(vect);
cout << endl;
sort(vect,vect.size(),0, vect.size());
cout<<"Sorted Vector: ";
displayVector(vect);
cout << endl;
cout << "Enter a number to search for: ";
cin >> value;
unsigned pos = search(vect, value);
if (pos != UINT_MAX)
{
cout << endl<<value<<" has been found at index: "<< pos << endl;
}
else
{
cout << endl <<value<<" Invalid Entry!" << endl;
}
cout << endl;
return 0;
}
Explanation / Answer
PSEUDOCODE for :
void displayVector(vector<int> vect)
loop the variable i from 0 to vect.size()-1
print vect[i]
void mergeVA(vector<int> &vect, int size, int l, int middle, int h)
copy contents of vector from 'I' to 'middle' to leftSide[]
copy contents of vector from 'middle+1' to 'h' to rightSide[]
i=0 -> index of leftSide[]
j=0 -> index of rightSide[]
k=I -> index of vect
//sorting should be done like in mergeSort algorithm
loop through these two arrays until i & j does not exceed to it their maximum possible size
{
if(leftSide[i] is less than or equal to rightSide[j])
copy leftSide[i++] to vect[k++];
else
copy rightSide[j++] to vect[k++];
}
if( any leftSide[] array contents are left unchecked)
then copy all unchecked leftSide[] elements to vect
if( any rightSide[] array contents are left unchecked)
then copy all unchecked rightSide[] elements to vect
void sort(vector<int> &vect, int size, int l, int h)
//this is method is like doing divide() method in mergeSort()
divide vect size until you get size to be 1 recursively
then pass those low and high positions to mergeVA() method.
unsigned search(vector<int> vect, int val)
//this method does search of 'val' in vect using binary search algorithm.
low=0 --> starting index of vect
high=vect.size() --> size of vect.
loop until low < high
{
find middle index of low and high
if(vect[middle]<val)
//means val will be in right side of vect
set low = middle+1;
else if(vect[middle]>val)
//means val will be in left side of vect
set high = middle;
else
//means we find the val in vect at middle index
return middle
return UINT_MAX
}
int main()
you need to create vector of size n
get value of n as user input.
You need to put random values in vector. so get MAXRAND value as user input.
loop until i reaches n
{
get random value between 1(inclusive) and MAXRAND(exclusive)
put that value into vector
}
print vector
sort the vector
print vector
take a input val from user to search for in vector
pos = search(val)
if( pos is not UINT_MAX )
print "we found val at" pos
else
print "invalid entry"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.