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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
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