Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

31) The investment company of Pickum & Loozem has been recording the trading pri

ID: 3711717 • Letter: 3

Question

31) The investment company of Pickum & Loozem has been recording the trading price of a particular stock over a 15-day period. Write a program that reads these prices and stores them in a sequential container and then sorts them into increasing order. The program should find and display a. The trading range (the lowest and the highest prices recorded). b. A sequence that shows how much the price rose or fell each day

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------.

Adapted from Page 583, Problem #31

This assignment is designed to ascertain that you understand the Standard Template Library, and the appropriate use of standard containers and standard algorithms found in that library.

The investment company of Pickem & Loozem has been recording the trading price of one particular stock over a fifteen day period. Write a program that:

1. queries the user for these prices and stores them in a sequential standard container. Keep in mind that stock prices are recorded in dollars and cents.

2. Finds and displays the following - all calculations must use standard algorithms

   a. the lowest price

   b. the highest price

   c. a sequence (stored in a standard container) showing the daily changes in prices (how much the stock price rose or fell each day)

3. Sort and display the stock prices from lowest to highest using a standard algorithm.

THE PROGRAM ASSIGNMENT IS BASED ON ABOVE GIVEN QUESTION 31

NEED THE PROGRAM TO BE WRITTEN IN C++ WITH COMPLETE DOCUMENTATION

Explanation / Answer

#include <iostream>

#include <vector>

#include <algorithm>

#include <string.h>

#include <string>

using namespace std;

int main() {

// your code goes here


// Set numDays = 15 if you want data for 15 days
int numDays = 3;


//This vector stores all price data sequentially
vector<float> price;


//loop to take data input. Prices are in string format and hence are converted to int before storing in vector
cout<<"Enter stock prices in the format: 17$ 20c"<<endl;

for(int i=0;i<numDays;i++){

string dollar, cent;

cout<<"Enter price for day "<<i+1<<endl;

cin>>dollar>>cent;

replace(dollar.begin(), dollar.end(), '$', '');

replace(cent.begin(), cent.end(), 'c', '');

price.push_back(stoi(dollar) + ((float)stoi(cent)/100));

}


// stl function used to calculate min and max stock price and output with correct formatting
float minPrice = *min_element(price.begin(), price.end());

float maxPrice = *max_element(price.begin(), price.end());

cout<<"Mininum price is: "<<(int)minPrice<<"$ "<<(int)((minPrice-(int)minPrice)*100)<<"c"<<endl;

cout<<"Maximum price is: "<<(int)maxPrice<<"$ "<<(int)((maxPrice-(int)maxPrice)*100)<<"c"<<endl;


//Loop for calculating price change per day, starts from day 2 and subtracts price of prev day
cout<<"Daile price changes: "<<endl;

for(int i=1;i<numDays;i++){

cout<<"day "<<i+1;

float diff = price[i]-price[i-1];

if(diff>0){

cout<<"Increase by "<<(int)diff<<"$ "<<(int)((diff-(int)diff)*100)<<"c"<<endl;

}

else

cout<<"Decrease by "<<-(int)diff<<"$ "<<(int)(-(diff-(int)diff)*100)<<"c"<<endl;

}

//stl function sort used to sort the prices in ascending order. tmp variable is used so that original array is intact
//sorted prices are printed using for loop amd correct formatting
vector<float> tmp(price);

sort(tmp.begin(), tmp.end());

cout<<"Prices in sorted order: "<<endl;

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

cout<<(int)tmp[i]<<"$ "<<(int)((tmp[i]-(int)tmp[i])*100)<<"c"<<" ";

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote