In: Computer Science
write a simple program to explain vector type in
c++...
use comments to explain plz
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<double> dollars; // declare vector of type
double
bool more = true;
while (more) //enter double type values in it using push_back
method
{
double d;
cout << "\nPlease enter dollars, 0 to quit: ";
cin >> d;
if (d == 0)
more = false; // exit loop
else
dollars.push_back(d); // add value of d into dollars vector
}
double sum = 0;
int i;
for (i = 0; i < dollars.size(); i++) // sum of all elements of
vector
sum = sum + dollars[i]; // sum of all dollars
cout << "\nSum = $"<<sum << "\n"; // display
sum
return 0;
}
Output:
Please enter dollars, 0 to quit: 100.34
Please enter dollars, 0 to quit: 200.56
Please enter dollars, 0 to quit: 300.67
Please enter dollars, 0 to quit: 400.85
Please enter dollars, 0 to quit: 500.56
Please enter dollars, 0 to quit: 0
Sum = $1502.98
Do ask if any doubt.