Vector Sizing Help: Solve Your Problem

In summary: This should do the trick. In summary, your code doesn't work because you don't initialize oldData to the same value as newData.
  • #1
polinkk1
1
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
  • #2
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:

FAQ: Vector Sizing Help: Solve Your Problem

What is vector sizing and why is it important?

Vector sizing is the process of determining the appropriate size and dimensions of a vector in order to accurately represent and analyze data. It is important because using an incorrect vector size can lead to inaccurate results and conclusions.

How do I know what size vector to use for my data?

The size of a vector depends on the specific data being analyzed. Generally, a larger vector size allows for more precise analysis, but too large of a vector can also lead to unnecessary computational resources. It is important to consider the complexity of the data and the desired level of accuracy when choosing a vector size.

Can I resize a vector after it has been created?

Yes, it is possible to resize a vector after it has been created. However, this may require additional computational resources and may also alter the accuracy of the data analysis. It is best to determine the appropriate vector size before creating it.

Are there any tools or software available to help with vector sizing?

Yes, there are various tools and software available that can assist with vector sizing. These include mathematical software such as MATLAB or Python, which have built-in functions for creating and manipulating vectors. Additionally, there are online calculators and tutorials that can provide guidance on vector sizing.

What are some common mistakes to avoid when determining vector size?

One common mistake is using a vector size that is too small, which can result in inaccurate or incomplete data analysis. It is also important to consider the dimensionality of the data and choose a vector size that can adequately represent all dimensions. Additionally, it is important to avoid using overly large vector sizes, as this can cause unnecessary computational resources and may not significantly improve accuracy.

Back
Top