Question

In: Computer Science

Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a,...

Hi i have this for a practical tomorrow (C++):

a) Write a function vector merge(vector a, vector b) that merges two vectors, alternating elements from both vectors. If one vector is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer vector.

For example, if a is 1 4 9 16 and b is 9 7 4 9 11

Then merge returns the vector 1 9 4 7 9 4 16 9 11

b) Write a function vector merge_sorted(vector a, vector b) that merges two sorted vectors, producing a new sorted vector. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 Then merge_sorted returns the vector 1 4 4 7 9 9 9 11 16

c) Write a procedure void pie_chart(vector data) that displays a pie chart of the values in data, assuming that all values in data are positive.

d) Write the selection sort algorithm as a template function, which can sort both numeric and string data types.

Solutions

Expert Solution

The language used is C++.

(a) Function:

vector<int> merge(vector<int> a, vector<int> b) {

vector<int> result;
int n = max(a.size(), b.size());

for(int i=0;i<n;i++) {
result.push_back(a[i]);
result.push_back(b[i]);
}

for(int i=n;i<a.size();i++) {
result.push_back(a[i]);
}

for(int i=n;i<b.size();i++) {
result.push_back(b[i]);
}

return result;
}

(b) Function:

vector<int> merge_sorted(vector<int> a, vector<int> b) {
vector<int> result;

int i = 0;
int j = 0;

while(i < a.size() && j < b.size()) {
if(a[i] <= b[j]) {
result.push_back(a[i]);
i++;
} else {
result.push_back(b[j]);
j++;
}
}

for(int k=i;k<a.size();k++) {
result.push_back(a[k]);
}

for(int k=j;k<b.size();k++) {
result.push_back(b[k]);
}

return result;
}

(c) Function:

//Pie chart will not be drawn in code blocks
//Therefore, displaying the percentage of occurences of a number.
void pie_chart(vector<int> data) {
unordered_map<int, int> m;
for(int i=0;i<data.size();i++) {
m[data[i]]++;
}

unordered_map<int, int>::iterator it = m.begin();

while(it != m.end()) {
double percentage = (it->second*100.0)/data.size();
cout<<it->first<<" ----> "<<percentage<<"%"<<endl;
it++;
}
}

(d) Function:

template <typename T>
void selectionSort(vector<T> & arr) {

int n = arr.size();
for (int i = 0; i < n-1; i++) {

int min_index = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min_index])
min_index = j;
}

swap(arr[min_index], arr[i]);
}
}

Complete Code:

#include<bits/stdc++.h>
using namespace std;

vector<int> merge(vector<int> a, vector<int> b) {

vector<int> result;
int n = max(a.size(), b.size());

for(int i=0;i<n;i++) {
result.push_back(a[i]);
result.push_back(b[i]);
}

for(int i=n;i<a.size();i++) {
result.push_back(a[i]);
}

for(int i=n;i<b.size();i++) {
result.push_back(b[i]);
}

return result;
}

vector<int> merge_sorted(vector<int> a, vector<int> b) {
vector<int> result;

int i = 0;
int j = 0;

while(i < a.size() && j < b.size()) {
if(a[i] <= b[j]) {
result.push_back(a[i]);
i++;
} else {
result.push_back(b[j]);
j++;
}
}

for(int k=i;k<a.size();k++) {
result.push_back(a[k]);
}

for(int k=j;k<b.size();k++) {
result.push_back(b[k]);
}

return result;
}

//Pie chart will not be drawn in code blocks
//Therefore, displaying the percentage of occurences of a number.
void pie_chart(vector<int> data) {
unordered_map<int, int> m;
for(int i=0;i<data.size();i++) {
m[data[i]]++;
}

unordered_map<int, int>::iterator it = m.begin();

while(it != m.end()) {
double percentage = (it->second*100.0)/data.size();
cout<<it->first<<" ----> "<<percentage<<"%"<<endl;
it++;
}
}

template <typename T>
void selectionSort(vector<T> & arr) {

int n = arr.size();
for (int i = 0; i < n-1; i++) {

int min_index = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[min_index])
min_index = j;
}

swap(arr[min_index], arr[i]);
}
}

//Main method
int main() {

vector<int> x{1, 7, 2, 4, 9, 16};
vector<int> y{4, 3, 9, 11};
vector<int> z = merge(x, y);

cout<<"merge is:\n";
for(int v : z) {
cout<<v<<" ";
}
cout<<endl;

vector<int> a{1, 4, 9, 16};
vector<int> b{4, 7, 9, 9, 11};
vector<int> res = merge_sorted(a, b);

cout<<"Sorted merge is:\n";
for(int v : res) {
cout<<v<<" ";
}
cout<<endl;

pie_chart(b);

vector<string> d {"def", "xyz", "abc"};
selectionSort(d);

for(string s : d) {
cout<<s<<" ";
}

}

Code Snippets:

Sample Output:


Related Solutions

Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed to change the parameter list in the functions. This is what I have so far, written in C. So far I've been getting segfaults, and I feel like it's because I'm off by 1, somewhere... void merge_the_array(int *list, int l1, int h1, int l2, int h2){ // create a temp array for l1-h1 int arr_a[(h1-l1)+1]; // create a temp array for l2-h2, adding 1...
hi I have search on IKEA Company and I have to write about below information 1-...
hi I have search on IKEA Company and I have to write about below information 1- Describe the roles of directional,marketing, operations and human resource strategies in the overall well-being of your selected company?
hi I have search on IKEA Company and I have to write about below information Evaluate...
hi I have search on IKEA Company and I have to write about below information Evaluate the performance of the main activity of IKEA (performance of principal product/service). What type(s) of criteria do you use to evaluate this performance?
I am working on a lab practical write up in which we have to measure the...
I am working on a lab practical write up in which we have to measure the acceleration due to gravity by observing carts rolling down an incline. I have to plot a scatter plot and use a program with Linear Least Squares Fit to calculate the gradient of the graph and uncertainty for this value. This program works but I don't know how to combine the uncertainty given for the Linear Fit with the uncertainties associated with my input x...
Hi there, I have to watch this movie (READY PLAYER ONE) and then write 1 to...
Hi there, I have to watch this movie (READY PLAYER ONE) and then write 1 to 2 pages about what I understand about the movie. I already watched the movie but I don't what to write. So can someone help me, please. write what you understand about this movie. use your own. write 1 or 2 pages. Business ethic Thank you
Hi, I am working on this assignment where we have to read and write to a...
Hi, I am working on this assignment where we have to read and write to a file. Change any code that needs to be changed, I need the wfile and rfile functions to be edited to work and the rest of the program. please change the gets() to something that works. if I need to create a .txt file to run the program pls let me know Thanks in advance for the help //agalloc.c // maintains list of agents infile,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT