Write a C++ program that will Declare a struct stock a. Name b. Price on Day 1 c
ID: 3725644 • Letter: W
Question
Write a C++ program that will
Declare a struct stock a. Name
b. Price on Day 1 c. PriceonDay2 d. Percent change
Ask the user to input how many stocks he’ll enter
Input that number
Declare a vector of stocks of that inputted number(step 3)
Use a loop that for each stock will
a. Input:
Name of Stock
Price on Day 1
PriceonDay2
b. Compute the Percent change
Loop that computes the stock whose percentage change increased
the most
Output the stock in the vector whose percent change increased the most
a. Output all contents of the struct
Name
Price on Day 1
PriceonDay2
Percent change
Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
struct Stock{
string name;
float day1;
float day2;
float perChng;
};
int main() {
int n;
cout<<"How many stock want to enter: ";
cin>>n;
vector<Stock> v(n);
cout<<"Enter details of "<<n<<" stocks: "<<endl;
for(int i=0; i<n; i++) {
cout<<"Name of Stock: ";
cin>>v[i].name;
cout<<"Price on Day 1: ";
cin>>v[i].day1;
cout<<"Price on Day 2: ";
cin>>v[i].day2;
// calculating % change
float per = (v[i].day2 - v[i].day1)*100/v[i].day1;
v[i].perChng = per;
}
int maxIndex = 0;
for(int i=1; i<n; i++) {
if(v[maxIndex].perChng < v[i].perChng)
maxIndex = i;
}
cout<<"Details of stock with maximum changes: "<<endl;
cout<<"Name: "<<v[maxIndex].name<<endl;
cout<<"Day1 peice: "<<v[maxIndex].day1<<endl;
cout<<"Day2 price: "<<v[maxIndex].day2<<endl;
cout<<"Percentage change : "<<v[maxIndex].perChng<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.