Question

In: Computer Science

(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 number of purchases, instead of just 10. (Hint: use push_back().)

#include <vector>
#include <iostream>
 
using namespace std;
 
int main()
{
   vector<double> purchases(10);
   for (int i = 0; i < 10; i++)
   {
      cout << "Enter a purchase amount";
      cin >> purchases[i];
   }
   return (0);
}

You these input values to test your program: 6.3, 5.4, 12.3, 1234.45, 89.9, 34.56, 12.6

2. Now add to the code from the previous part to print out the vector in reverse order and remove each element after it is printed.

Solutions

Expert Solution

Answer:

Give me a thumbs up if you like my work !!!

I have written the below code based on your requirements.

The below code has no-error and it is working perfectly.

Here, I have used push_back() to insert as many elements that we want to insert.

And I'm printing the elements in reverse order.

After printing the elements, it will remove them.

I have also attached the Output Screenshot that I got by running the below program.

Output:

Code:

#include<vector>
#include<iostream>
using namespace std;

int main()
{
   vector<double> purchases;
  
   double temp;
  
   for(int i=0;i<7;i++)
   {
       cout<<"Enter a purchase amount ";
       cin>>temp;
  
       purchases.push_back(temp);
   }
  
   cout<<"\nPrinting in Reverse Order : \n\n";
   while(purchases.size() > 0)
   {
       vector<double>:: iterator it=purchases.end();
       it--;
       cout<<purchases[purchases.size() - 1]<<" ";
      
       purchases.erase(it);
   }
  
   return 0;
}


Related Solutions

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,...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
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...
Im asked to edit the C++ code given to me so that the user can enter...
Im asked to edit the C++ code given to me so that the user can enter as many courses as they would like. I've already built the array to house the courses, but Im a tiny bit stuck on writing the user input to the array every time a new line Is made. Code: //CSC 211 Spring 2019 //Program Description: // // Originally From: // CSC 211 Spring 2018 // Dr. Sturm // This program... // #include<iostream> #include<string> #include<fstream> using...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
C++ using vectors. in the following code, if a user adds duplicate names the votes should...
C++ using vectors. in the following code, if a user adds duplicate names the votes should be added to only one of the names: example display "Enter candidate name: " input john display "Enter candidate vote:" input 10 display "Enter candidate name: " input john display "Enter candidate vote:" input 10 so the output has to be john with 20 votes. #include<iostream> #include<iterator> #include<string> #include<algorithm> #include<array> #include<ctime> #include <vector> #include<bits/stdc++.h> using namespace std; int max_element(const vector<int>&stuff) { int max_index =0;...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a memory so you store that number of integers write integers in order starting from 1 until you fill all that memory print the address and values of the first and the last integer stored in the memory
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.
In C# code, how can we handle this dual nature of Y/y and N/n?
In C# code, how can we handle this dual nature of Y/y and N/n?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT