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...
In C++, please build a simple Distance Vector program that will communicate with N partners and...
In C++, please build a simple Distance Vector program that will communicate with N partners and do the following. (NOTE: All connections between a server and clients should be TCP/IP socket.) You will run N instances on your machine Each instance will run on a different port (instance 1 will run on port 18181, instance 2 on port 18182, instance 3 on port 18183, etc) The program will start by reading in the appropriate neighbors file and vector file. The...
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...
USE C++ for the program and kindly keep it as simple as possible , use recursion...
USE C++ for the program and kindly keep it as simple as possible , use recursion and do not use loops please keep program simple and comment if possible Suppose you have been given the task to design a text editor which will take any multiline text from user and then display the statistics like total number of characters i.e., characters_count (excluding the white space and punctuations), words_count, and redundant_words_count. Create a structure named Text_Editor having four type members namely...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT