Vector Sizing Help: Solve Your Problem

  • Context:
  • Thread starter Thread starter polinkk1
  • Start date Start date
  • Tags Tags
    Sizing Vector
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 4K views
polinkk1
Messages
1
Reaction score
0
Hi!

I have a homework problem that I'm stuck on. The question asks:
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}.
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:

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!
 
Physics news on Phys.org
Hi,
From your code, it appears that you are new to programming and C++ in particular. So first, my opinion is that this problem is inappropriate for a newbie; you really must understand the vector class, not a beginning topic.

Here's the easiest solution:

Code:
   if (newData == oldData) {
      cout << "Data matches!" << endl;
   } else {
      oldData = newData;
   }

However, to understand the above, you must know about vector's overloading of the operators == and =. Overloading of operators is not a first week topic.

Let me point out what's wrong with your code. First, two vectors are equal if and only if they have the same size and the corresponding components of the two are equal.

for (int i = 0; i < oldData.size(); i++) {
// you've already declared variable i, I'm surprised this compiles
// remove the int from int i
if (oldData.at(i) == newData.at(i)) {
cout << "Data matches!" << endl;
}
// Suppose your change the initialization of oldData to
// oldData.at(0)=25, leaving everything else the same. Then
// the two vectors are still not equal, but your code says they are!
else {
oldData.at(i) = newData.at(i);
}
// for the given vectors and content, this else clause is true for
// each i from 0 to 2. So the corresponding component is assigned
// to old data. But the component 3 of newData never gets into
// old data.
}

Here are some hints for a solution. First write code to determine if newData equals
oldData. Here's one way to do this:
bool equal = newData.size() == oldData.size();
If equal is now true, write a loop to test whether all corresponding components are the same.

After the above:
if (equal) {
cout<<"same"<<endl;
}
else {
// code to assign new Data to oldData; you need to make certain the size of
// oldData is the same as newData's size. Do this by a call to the vector method
// resize. Now write a loop to assign to each component of oldData the
// corresponding value in newData.
}
 
Last edited: