Question

In: Computer Science

write a simple program to explain vector type in c++... use comments to explain plz

write a simple program to explain vector type in c++...
use comments to explain plz

Solutions

Expert Solution

#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.


Related Solutions

write a simple program to demonstrate the use of static type of variables in c++... use...
write a simple program to demonstrate the use of static type of variables in c++... use comments to explain plz
The program should be written in C++ with comments Write a program that takes graduation rates...
The program should be written in C++ with comments Write a program that takes graduation rates (per 1000 of the population) for North, South, East, West and Central United States. Input the number from each of the regions in a function returning an int with the graduation rate to the main program. Also figure out which region has the highest graduation rate in another function and display the result from inside that particular function. So your function prototypes should be...
i need C++ program with possible comments what is going on,(a) Write a program that reads...
i need C++ program with possible comments what is going on,(a) Write a program that reads a char as input, and determines if it is a lowercase letter, uppercase letter, a digit or something else (call the last one a special character).
Please write a complete C coding program (NOT C++) that has the following: (including comments) -...
Please write a complete C coding program (NOT C++) that has the following: (including comments) - declares two local integers x and y - defines a global structure containing two pointers (xptr, yptr) and an integer (z) - declares a variable (mst) by the type of previous structure - requests the values of x and y from the user using only one scanf statement - sets the first pointer in the struct to point to x - sets the second...
C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
PLEASE PROVIDE COMMENTS ON STEPS Write a C++ program that modifies a string (null terminated) as...
PLEASE PROVIDE COMMENTS ON STEPS Write a C++ program that modifies a string (null terminated) as follows: Consonants are positioned at the beginning of the string and vowels are moved to the end of the string. Example : Original string : washer New string : wshrae Note: The library string functions cannot be used. You must use pointers and the switch statement to execute this program. Assume that the vowels are a, e, i, o, and u. The modification has...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT