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

80.2K

Verified Solution

Question

Programming

c++

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

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

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

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

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

10 purchases. Output the total of all purchases. Hint: us dowhile 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

Answer & Explanation Solved by verified expert
4.3 Ratings (738 Votes)
C program    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students