MHB Vector Sizing Help: Solve Your Problem

  • Thread starter Thread starter polinkk1
  • Start date Start date
  • Tags Tags
    Sizing Vector
AI Thread Summary
The discussion focuses on a programming homework problem involving vector comparison and assignment in C++. The original code fails to handle the case where the vectors have different sizes, leading to incomplete output. A suggested solution is to use the overloaded operators for vectors to simplify the comparison and assignment. Additionally, the importance of checking vector sizes and using a loop to ensure all elements are compared is emphasized. Understanding these concepts is crucial for effectively working with vectors 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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top