MHB Determining the size of a vector

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Vector
AI Thread Summary
The discussion centers around a C++ program that involves a vector named `sensorReadings`. Initially, the vector is created with a size of 4 but is then resized to 10. The main issue raised is the assignment of the vector's size to the variable `currentSize`. Instead of manually setting `currentSize` to 10, it is suggested to use the `sensorReadings.size()` method to dynamically retrieve the current size of the vector. This approach ensures that `currentSize` accurately reflects the vector's size after any modifications.
needOfHelpCMath
Messages
70
Reaction score
0
May anyone help guide and explain this program for meAssign the size of vector sensorReadings to currentSize.
Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> sensorReadings(4);
   int currentSize = 0;

   sensorReadings.resize(10);

     
       currentSize = 10;

   cout << "Number of elements: " << currentSize << endl;

   return 0;
}
Testing sensorReadings.resize(10)
Your value: 10
✖ Testing sensorReadings.resize(2)
Expected value: 2
Your value: 10
 
Technology news on Phys.org
needOfHelpCMath said:
May anyone help guide and explain this program for me

Assign the size of vector sensorReadings to currentSize.

Hi needOfHelpCMath! (Smile)

We can retrieve the size of vector sensorReadings with [m]sensorReadings.size()[/m].
That's what we need instead of assigning a constant value of $10$.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top