Question

In: Computer Science

c++ We can dynamically resize vectors so that they may grow and shrink. While this is...

c++

We can dynamically resize vectors so that they may grow and shrink. While this is very

convenient, it is inefficient. It is best to know the number of elements that you need for your vector

when you create it. However, you might come across a situation someday where you do not know

the number of elements in advance and need to implement a dynamic vector.

Rewrite the following program so that the user can enter any number of purchases, instead of just

10 purchases. Output the total of all purchases. Hint: us do while loop with your vector..

#include

#include

using namespace std;

int main()

{

vector purchases(10);

for (int i = 0; i < 10; i++)

{

cout << "Enter a purchase amount";

cin >> purchases[i];

}

return (0);

}

Write your code here:

Use the Snipping Tool to show output

Solutions

Expert Solution

C++ program :-

#include <iostream>
#include <vector>
  
using namespace std;
  
int main()
{
vector<int> purchases;
int check = 1;
int temp;
  

while(check)
{
cout << "Enter a purchase amount:"<<endl;
cin >> temp;
purchases.push_back(temp);
  
cout<< "Wnat to add more...press 1 for yes and 0 to stop :"<<endl;
cin>>check;
}

cout <<endl<< "Output : ";
for (auto i = purchases.begin(); i != purchases.end(); ++i)
cout << *i << " ";
  
return 0;
}

Output :-


Related Solutions

(Code in C++) 1. We can dynamically resize vectors so that they may grow and shrink....
(Code in C++) 1. We can dynamically resize vectors so that they may grow and shrink. While this is very convenient, it is inefficient. It is best to know the number of elements that you need for your vector when you create it. However, you might come across a situation someday where you do not know the number of elements in advance and need to implement a dynamic vector. Rewrite the following program so that the user can enter any...
Why do some small towns disappear while others grow larger? Shouldn’t they all shrink or grow...
Why do some small towns disappear while others grow larger? Shouldn’t they all shrink or grow at similar rates? What determines the long-run equilibrium city size?
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if...
C++ Assignment Inheritance This uses some single arrays of doubles. We can use Vectors instead if we want! Choice either vectors or arrays either one is fine We will implement a classic Inheritance hierarchy. A simple console based interface is all that is needed. Build your classes first, each in their own .h and .cpp files, then test them with the simple main method provided below. Phase 1 : Here is the following set of classes you will implement and...
C++ Linkedlist Create a food menu so the customer can see what we are selling, if...
C++ Linkedlist Create a food menu so the customer can see what we are selling, if the users place order that not on the menu, prompt the users to enter again. Food code and food must be matched when users enter.
So we have concepts of sublethal injury, that may or may not be reversible, lethal injury...
So we have concepts of sublethal injury, that may or may not be reversible, lethal injury that causes a catastrophic break up of the cell, necrosis, or a more dainty and controlled form of programmed cell death known as apoptosis. What are your thoughts about these types of injury - can you think of any examples? How you might expect them to manifest themselves!
Which of the following provides nutrients, including water, to microorganisms so they can grow and produce?...
Which of the following provides nutrients, including water, to microorganisms so they can grow and produce? Group of answer choices Growth media Salt compounds only Yeast extract is the main component Only amino acids
C++ Make this class generic so that the sides of the shape may be integer or...
C++ Make this class generic so that the sides of the shape may be integer or float. ---------------shape.h------------ class Shape { public: Shape(); virtual int getSide(); virtual bool setSide(int x); virtual int getArea(); virtual int getPerimeter(); void display(); ~Shape(); protected: private: int side; // T_sides[numSides]; }; ----------------shape.cpp---------- #include "Shape.h" #include <iostream> using namespace std; Shape::Shape() { } void Shape::display() { cout<<"side: "<<getSide()<<endl; cout<<" area: "<<getArea()<<endl; cout<<"perimeter: "<<getPerimeter()<<endl; } bool Shape::setSide(int x) { if(x>0){ side = x; return true; }else return...
How can we view media products so that we are not “taken in” by the messages...
How can we view media products so that we are not “taken in” by the messages they give us about our age, social class, gender, race or religion?
While we may think that healthcare policies are created to ensure that patients have the best...
While we may think that healthcare policies are created to ensure that patients have the best experience in the healthcare system, the cost-benefit analysis of the problem plays a large part in the decision making process. What can be done to improve patient outcomes without adding too much cost to either the system or the patient. For this discussion, you will need to look at federal and state regulatory agencies, insurance - both public and private, professional organizations, and healthcare...
While we may think that healthcare policies are created to ensure that patients have the best...
While we may think that healthcare policies are created to ensure that patients have the best experience in the healthcare system, the cost-benefit analysis of the problem plays a large part in the decision making process. What can be done to improve patient outcomes without adding too much cost to either the system or the patient. For this discussion, you will need to look at federal and state regulatory agencies, insurance - both public and private, professional organizations, and healthcare...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT