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

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...
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...
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?
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
can someone change this code so that timestandard can be calculated at the end of the...
can someone change this code so that timestandard can be calculated at the end of the code using the inputs n,RF,PFD, and the measured cycles, instead of being called from the beginning of the code using namespace std; float timestandard(float time, float rf, float pfd) { return((time / 100) * rf * (1 + pfd)); /* calculating the time standard using the given formula*/ } int main() { int n, rf, pfd, x; /* inputs are taken*/ cout << "****...
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.
please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
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!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT