In: Computer Science
A certain advertising company has commissioned a study to determine if a recent advertising campaign is effective for certain age groups. Researchers have created a data file (results.txt) which contains the results attained by the study. The first column is the subject’s name (you may assume there is no whitespace in any name) and the second column indicates if the subject has seen the advertisement in question (Y or y means yes, N or n means no). The third column specifies the subject's age and the last column (the 'score') specifies how favorably the subject views the product being advertised (from 0 to 100 with a score of 100 being most favorable). A example of the file results.txt is attached. You should download this file to a convenient place on your hard drive. Please note that the file is a sample only. Your program should work on any data file that is formatted in a similar way.
results.txt example file -
Bailey Y 16 68 Harrison N 17 71 Grant Y 20 75 Peterson N 21 69 Hsu Y 20 79 Bowles Y 15 75 Anderson N 33 64 Nguyen N 16 68 Sharp N 14 75 Jones Y 29 75 McMillan N 19 80 Gabriel N 20 62 Huang Y 62 21 Willoughby Y 58 29 Davis N 33 65 McGregor Y 41 55
Your assignment is to prompt the user to enter the full pathname to the data file on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.
The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data. Your program should then calculate and display the the following results:
Average score for subjects under 18 who have not seen the ad.
Average score for subjects under 18 who have seen the ad.
Average score for subjects 18 to 35 (inclusive) who have not seen the ad.
Average score for subjects 18 to 35 (inclusive) who have seen the ad.
Average score for subjects over 35 who have not seen the ad.
Average score for subjects over 35 who have seen the ad.
Display your results to two places of decimals, and write your program to automatically list your six calculated averages one to a line, in the order above, along with a suitable label for each result.
Finally, your program should calculate and display to two places of decimals the overall average score for all of the subjects surveyed. (Note: Mathematically, this is NOT the average of the six averages you calculated previously).
Warning: There may be no subjects in any given category (such as over 35s who have not seen the ad). If so you cannot calculate the average (because you would get a divide by zero error) so instead of the average for that category you should display “No data to report”.
Program:
// C++ program to read data from file and calculate and display
the average scores
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string filePath, name;
char adStatus;
int age, score;
ifstream fin;
// initialize the variables to store the total score for various
categories to 0
int scoreU18_N = 0, scoreU18_Y = 0, score18_35_N = 0, score18_35_Y
= 0, scoreA_35_N = 0, scoreA_35_Y = 0;
// initialize the variables to store the total number of records
for various categories to 0
int n_U18_N = 0, n_U18_Y = 0, n_18_35_N = 0, n_18_35_Y = 0,
n_A_35_N = 0, n_A_35_Y = 0;
// input the path to file
cout<<"Enter the path to the file: ";
getline(cin, filePath);
fin.open(filePath); // open the file in read mode
// check that file opened successfully
if(fin.is_open())
{
// read till the end of file
while(!fin.eof())
{
// read a record from file
fin>>name>>adStatus>>age>>score;
// display the read data to console
cout<<left<<setw(30)<<name<<left<<setw(10)<<adStatus<<right<<setw(10)<<age<<right<<setw(10)<<score<<endl;
// add the score and increment the count based on the adStatus and
age
// ad seen
if(adStatus == 'y' || adStatus == 'Y')
{
// age < 18
if(age < 18)
{
scoreU18_Y += score;
n_U18_Y++;
}
else if(age <= 35) // 18<= age <= 35
{
score18_35_Y += score;
n_18_35_Y++;
}
else // age > 35
{
scoreA_35_Y += score;
n_A_35_Y++;
}
}
else // ad not seen
{
if(age < 18) // age < 18
{
scoreU18_N += score;
n_U18_N++;
}
else if(age <= 35) // 18 <= age <= 35
{
score18_35_N += score;
n_18_35_N++;
}
else // age > 35
{
scoreA_35_N += score;
n_A_35_N++;
}
}
}
fin.close(); // close the file
cout<<fixed<<setprecision(2);
// calculate and display the average of different
categories
cout<<"Average score for subjects under 18 who have not seen
the ad: ";
if(n_U18_N == 0)
cout<<"No data to report"<<endl;
else
cout<<(((double)scoreU18_N)/n_U18_N)<<endl;
cout<<"Average score for subjects under 18 who have seen
the ad: ";
if(n_U18_Y == 0)
cout<<"No data to report"<<endl;
else
cout<<(((double)scoreU18_Y)/n_U18_Y)<<endl;
cout<<"Average score for subjects 18 to 35 (inclusive) who
have not seen the ad: ";
if(n_18_35_N == 0)
cout<<"No data to report"<<endl;
else
cout<<(((double)score18_35_N)/n_18_35_N)<<endl;
cout<<"Average score for subjects 18 to 35 (inclusive) who
have seen the ad: ";
if(n_18_35_Y == 0)
cout<<"No data to report"<<endl;
else
cout<<(((double)score18_35_Y)/n_18_35_Y)<<endl;
cout<<"Average score for subjects over 35 (inclusive) who
have not seen the ad: ";
if(n_A_35_N == 0)
cout<<"No data to report"<<endl;
else
cout<<(((double)scoreA_35_N)/n_A_35_N)<<endl;
cout<<"Average score for subjects over 35 (inclusive) who
have seen the ad: ";
if(n_A_35_Y == 0)
cout<<"No data to report"<<endl;
else
cout<<(((double)scoreA_35_Y)/n_A_35_Y)<<endl;
// calculate and display the overall average score
cout<<"Overall average score: "<<
((double)(score18_35_N + score18_35_Y + scoreA_35_N + scoreA_35_Y +
scoreU18_N + scoreU18_Y))/(n_U18_N + n_U18_Y + n_18_35_N +
n_18_35_Y + n_A_35_N + n_A_35_Y)<<endl;
}
else // file open failed
cout<<"Unable to open file :
"<<filePath<<endl;
return 0;
}
//end of program
Output:
Input file : results.txt
Output: