Hello, i want to put two arrays in one and arrange it so that it is constantly increasing. The precondition is that both arrays have elements in an increasing order.
This is what I have done so far but I am a bit lost now. Hope you can help me! :)
#include<iostream>
#include<vector>
using namespace std;
vector<double> merge(const vector<double>& v1, const vector<double>& v2) {
int i = 0;
int j = 0;
int k = 0;
int n1 = v1.size();
int n2 = v2.size();
int n3 = n1 + n2;
vector<double> v3(n3);
while (j < n1) {
bool found = false;
while ((k < n2) and (not found)) {
if (v1[j] <= v2[k]) {
v3[i] = v1[j];
found = true;
++j;
}
else {
v3[i] = v2[k];
++k;
}
++i;
}
}
return v3;
}
void read_vector(vector<double>& v) {
for (int i = 0; i < v.size(); ++i) cin >> v[i];
}
void print_vector(vector<double>& v) {
for (int i = 0; i < v.size(); ++i) cout << v[i] << " ";
}
int main() {
int n1;
cin >> n1;
vector<double> v1(n1);
read_vector(v1);
int n2;
cin >> n2;
vector<double> v2(n2);
read_vector(v2);
vector<double> v3;
v3 = merge(v1, v2);
print_vector(v3);