- #1
polinkk1
- 1
- 0
Hi!
I have a homework problem that I'm stuck on. The question asks:
Thanks!
I have a homework problem that I'm stuck on. The question asks:
My code is able to output the first three numbers in the vector, but it won't output the last one. This is what I have so far:If the vector oldData is the same as the vector newData, print "Data matches!" ended with a newline. Otherwise, assign oldData with newData. Ex: If oldData = {10, 12, 18} and newData = {25, 27, 29, 23}, then oldData becomes {25, 27, 29, 23}.
Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> oldData(3);
vector<int> newData(4);
unsigned int i = 0;
oldData.at(0) = 10;
oldData.at(1) = 12;
oldData.at(2) = 18;
newData.at(0) = 25;
newData.at(1) = 27;
newData.at(2) = 29;
newData.at(3) = 23;
//Start student code
for (int i = 0; i < oldData.size(); i++) {
if (oldData.at(i) == newData.at(i)) {
cout << "Data matches!" << endl;
}
else {
oldData.at(i) = newData.at(i);
}
}
//End student code
for (i = 0; i < oldData.size(); ++i) {
cout << oldData.at(i) << " ";
}
cout << endl;
return 0;
}
Thanks!