When I try to run build on this code it doesn't succeed but says 1 up-to-date then when I try to run the code the for loop doesn't run and it brings an error of vector script out of range.
Could you please help me?
Thanks in advance.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
{
int j = m + n;
vector<int> nums3;
for (int s = 0; s < j; s++)
{
nums3[s] = nums1[s];
nums3[s + m] = nums2[s];
}
cout << "The elements of nums3 array are:\n";
for (int u = 0; u < j; u++)
{
cout << nums3[u] << endl;
}
}
int main()
{
int m, n;
cout << "What is the size of the array nums1?\n";
cin >> m;
vector<int> nums1;
cout << "Please key in the values in nums1:\n";
for (int o = 0; o < m; o++)
{
cin >> o;
nums1.push_back(o);
cout << endl;
}
cout << "What is the size of the array nums2?\n";
cin >> n;
vector<int> nums2;
cout << "Please key in the values in nums1:\n";
for (int k = 0; k < n; k++)
{
cin >> k;
nums2.push_back(k);
cout << endl;
}
merge(nums1, m, nums2, n);
}
the user inputs m = 10.
the user types in the value 100 for o in the loop.
the loop ends because 100 is > 10.
you have over-written the loop variable with user input, and literally anything crazy could happen here due to that, depending on what the user types in.
you should cin some other variable and leave the loop variable alone.
also use code tags, the editor's <> button around code.
also keep to one thread on the same code, even if its a different issue.
--my bad on the warning, was stray code from a different thread
to see what is in the result just loop with a cout. you can loop until var < nums3.size()