#include<iostream> #include<vector> using namespace std; vector<int> combine(con
ID: 3623741 • Letter: #
Question
#include<iostream>
#include<vector>
using namespace std;
vector<int> combine(const vector<int>& A, const vector<int>& B);
int main()
{
vector<double> L1;
vector<double> L2;
vector<double> L3;
int number;
int number2;
cout << "Number of students in this group: ";
cin >> number;
L1.resize (number);
// asks user to enter 3 numbers and put them in vector L1
for (int i = 0; i < number; i++)
{
cout << "Enter numbers in increasing order #" << i+1 << ": ";
cin >> L1[i];
}
cout <<"Enter number of elements: ";
cin >> number2;
L2.resize (number2);
// asks user to enter 3 numbers and put them in vector l2
for (int i = 0; i < number2; i++)
{
cout <<"Enter 3 more elements in increasing order#" << i+1 << ":";
cin >> L2[i];
}
// im suppose to call the function below so it will sort L1 and L2
// How can i call the function below and also how do i show the result
// of the combining vecots
return 0;
}
vector<int> combine(const vector<int>& A, const vector<int>& B)
{
// Fill the resultant vector with sorted results from both vectors
vector<int> R;
unsigned L1 = 0, L2 = 0;
while(L1 < A.size() && L2 < B.size())
{
// If the left value is smaller than the right it goes next
// into the resultant vector
if(A[L1] < B[L2])
{
R.push_back(A[L1]);
L1++;
}
else
{
R.push_back(B[L2]);
L2++;
}
}
// Push the remaining data from both vectors onto the resultant
while(L1 < A.size())
{
R.push_back(A[L1]);
L1++;
}
while(L2 < B.size())
{
R.push_back(B[L2]);
L2++;
}
}
Explanation / Answer
please rate - thanks
#include<iostream>
#include<vector>
using namespace std;
void fill( vector<int>& R, const vector<int> B,int n);
void combine(const vector<int> A, const vector<int> B,vector<int>&);
int main()
{
vector<int> L1;
vector<int> L2;
vector<int> L3;
int number;
int number2;
cout << "Number of students in this group: ";
cin >> number;
L1.resize (number);
// asks user to enter 3 numbers and put them in vector L1
for (int i = 0; i < number; i++)
{
cout << "Enter numbers in increasing order #" << i+1 << ": ";
cin >> L1[i];
}
cout <<"Enter number of elements: ";
cin >> number2;
L2.resize (number2);
// asks user to enter 3 numbers and put them in vector l2
for (int i = 0; i < number2; i++)
{
cout <<"Enter 3 more elements in increasing order#" << i+1 << ":";
cin >> L2[i];
}
// im suppose to call the function below so it will sort L1 and L2
// How can i call the function below and also how do i show the result
// of the combining vecots
combine(L1,L2,L3);
for (int i = 0; i < L3.size(); i++)
cout<<L3[i]<<" ";
cout<<endl;
system("pause");
return 0;
}
void fill( vector<int>& R, const vector<int> B,int n,int m)
{R.resize(n+m);
while(n<B.size())
{R.push_back(B[n]);
n++;
}
return;
}
void combine(const vector<int> A, const vector<int> B,vector<int>& R)
{
// Fill the resultant vector with sorted results from both vectors
unsigned L1 = 0, L2 = 0;
while(R.size()< A.size() + B.size())
{
// If the left value is smaller than the right it goes next
// into the resultant vector
if(A[L1] < B[L2])
{
R.push_back(A[L1]);
L1++;
if(L1==A.size())
fill(R,A,L1,L2);
}
else
{
R.push_back(B[L2]);
L2++;
if(L2==B.size())
fill(R,B,L2,L1);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.