ctt Exercise1 You are interested in analyzing the frequency of numbers selected
ID: 3733532 • Letter: C
Question
ctt Exercise1 You are interested in analyzing the frequency of numbers selected for the Powerball lottery. You have downloaded the previous year's Powerball numbers which are saved in a text file called pb_2014.txt and each record in the file has the following format: 01/01/14 15 24 40 48 52 23 x PP 01/04/14 19 20 37 41 58 14 x PP 01/08/14 10 28 39 47 58 22x PP 01/11/14 10 15 33 48 54 34 x PP Unfortunately this is an incompatible file format for the program you are using to analyze the frequency. The correct record file format needed by the program is shown below: 15 24 40 48 52 23 19 20 37 41 58 14 0 28 39 47 58 22 10 15 33 48 54 3-4 Write a program to convert the pb 2014.txt file into the required file format shown above saving the new format to the file powerball.txt, Copy and paste your program into your word document. Copy and paste the first 10 records of each file directly below your program.Explanation / Answer
I have written cpp program for the above.
Main.cpp
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
//Function declaration
void readData(ifstream& dataIn,ofstream& dataOut);
int main() {
ifstream dataIn;
//Defines an output stream for the data file
ofstream dataOut;
//Setting the precision
dataOut<<setprecision(2)<<fixed<<showpoint;
//Opening the input file
dataIn.open("pb_2014.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
//creating and Opening the output file
dataOut.open("pb_20141.txt");
readData(dataIn,dataOut);
//Closing the intput file
dataIn.close();
//Closing the output file.
dataOut.close();
}
return 0;
}
//This function will read and process the data and write to outfile
void readData(ifstream& dataIn,ofstream& dataOut)
{
string skip,f1,f2,f3,f4,f5,f6,skip2,skip3;
while(dataIn>>skip>>f1>>f2>>f3>>f4>>f5>>f6>>skip2>>skip3)
{
cout<<f1<<" "<<f2<<" "<<f3<<" " << f4 << " " << f5 << " "<< f6 <<endl;
dataOut<<f1<<" "<<f2<<" "<<f3<<" " << f4 << " " << f5 << " "<< f6 <<endl;
}
}
Input file:
01/01/14 15 24 40 48 52 23 * PP
01/04/14 19 20 37 41 58 14 * PP
01/08/14 10 28 39 47 58 22 * PP
01/11/14 10 15 33 48 54 34 * PP
Output:
15 24 40 48 52 23
19 20 37 41 58 14
10 28 39 47 58 22
10 15 33 48 54 34
and output written to output file.
Please check the above and let me know any issues. Thank you. All the best.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.