I need to complet this program: /* Problem description: The text file DJI.txt co
ID: 3619909 • Letter: I
Question
I need to complet this program:/* Problem description: The text file DJI.txt contains the information of a stock market index --
Dow Jones Industrial Average Index (DJI for short) -- for a continuous time
in year 2008. Each line contains the date and the DJI's value on that day.
For the format of the file, please view DJI.txt.
Please write a program that reads DJI values stored in the data file DJI.txt,
and finds out the date with highest DJI value during this period stored in the file.
*/
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main( )
{
const string inputFile = "/var/www/html/programs2/2170_1_5/dataFiles/DJI.txt";
ifstream ifs; //an input file stream
int day; //Today's day
int maxDay; //The day of the date with max. DJI
string month; //Today's month
string maxMonth; //The month of the date with max. DJI
double maxDJI; //The max. DJI value got so far.
double dji; //Today's DJI
ifs.open( inputFile.c_str() ); //open the file
Please write a program that reads DJI value from the file opened above, and find out the date with highest DJI value during this period.
ifs.close(); //close the input file
cout << "DJI is highest on " << maxMonth << " " << maxDay << " with " << maxDJI << endl;
return 0;
}
Explanation / Answer
I am assuming that each line of input is formatted as follows: [Month as string] [space] [Day as integer] [space] [Average as float] Example: April 4 10300.12 #include <algorithm>#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct ticker
{
string month;
int day;
float avg;
};
int main()
{
ifstream inFile("/var/www/html/programs2/2170_1_5/dataFiles/DJI.txt");
vector<ticker> tickers;
ticker t;
inFile >> t.month >> t.day >> t.avg;
while(inFile.good())
{
tickers.push_back(t);
inFile >> t.month >> t.day >> t.avg;
}
sort(tickers.begin(), tickers.end());
ticker max = tickers.back();
cout << " Maximum: " << max.month << ' ' << max.day << ' ';
cout << setprecision(7) << max.avg << endl;
return 0;
}
bool operator<(const ticker& left, const ticker& right)
{
return left.avg < right.avg;
}
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct ticker
{
string month;
int day;
float avg;
};
int main()
{
ifstream inFile("/var/www/html/programs2/2170_1_5/dataFiles/DJI.txt");
vector<ticker> tickers;
ticker t;
inFile >> t.month >> t.day >> t.avg;
while(inFile.good())
{
tickers.push_back(t);
inFile >> t.month >> t.day >> t.avg;
}
sort(tickers.begin(), tickers.end());
ticker max = tickers.back();
cout << " Maximum: " << max.month << ' ' << max.day << ' ';
cout << setprecision(7) << max.avg << endl;
return 0;
}
bool operator<(const ticker& left, const ticker& right)
{
return left.avg < right.avg;
}
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct ticker
{
string month;
int day;
float avg;
};
int main()
{
ifstream inFile("/var/www/html/programs2/2170_1_5/dataFiles/DJI.txt");
vector<ticker> tickers;
ticker t;
inFile >> t.month >> t.day >> t.avg;
while(inFile.good())
{
tickers.push_back(t);
inFile >> t.month >> t.day >> t.avg;
}
sort(tickers.begin(), tickers.end());
ticker max = tickers.back();
cout << " Maximum: " << max.month << ' ' << max.day << ' ';
cout << setprecision(7) << max.avg << endl;
return 0;
}
bool operator<(const ticker& left, const ticker& right)
{
return left.avg < right.avg;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.