Vector Sizing Help: Solve Your Problem

  • Context: MHB 
  • Thread starter Thread starter polinkk1
  • Start date Start date
  • Tags Tags
    Sizing Vector
Click For Summary
SUMMARY

The discussion focuses on a C++ programming problem involving vector comparison and assignment. The original code fails to handle vectors of different sizes correctly, leading to incomplete data assignment. A more efficient solution is provided using vector operator overloading for equality and assignment, which simplifies the code significantly. Key insights include the importance of understanding vector size and element-wise comparison in C++.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of the C++ Standard Library's vector class
  • Knowledge of operator overloading in C++
  • Basic debugging techniques in C++
NEXT STEPS
  • Study C++ vector class documentation to understand its methods and properties
  • Learn about operator overloading in C++ for custom types
  • Practice writing functions that compare and manipulate vectors
  • Explore C++ debugging tools to identify and fix common coding errors
USEFUL FOR

Beginner C++ programmers, computer science students, and anyone looking to improve their understanding of vector operations and data manipulation in C++.

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!
 
Technology 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:

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 6 ·
Replies
6
Views
7K