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

Lab 8 Attached Files: D alcohol bot 3.086 KB Analyze Data We will analyze data c

ID: 3843497 • Letter: L

Question

Lab 8 Attached Files: D alcohol bot 3.086 KB Analyze Data We will analyze data collected about student alcohol use. A simplifed text file with alcohol usage data is attached to this lab. It is based on http/larchive.ics, ESILUDENI The data has been restricted to only show the student's gender, age, and weekend alcohol use. Big Picture: Ask the user for an age range (min and maxp Search the file to collect the information the user asked for Display the data as a bar chart Details You want to read the file and make a vector with the total for each Gender/Alchohol frequency. The vector will have 10 spaces, they are numbered 0-9. We need to encode the gender and frequency into these spaces. The gender can be either M or F The frequency can be 1 (ery low) to 5 (very high). To fit these in the numbers 0-9, we can store M values in even spaces and F values in odd spaces. If M then store in position: frequency 2-2 If F then store in position: frequency 2-1 Remember to take into account that the user only cares about certain ages. In each position of the vector, count how many people met the requirement. If7 males said To make the data easy to visualize, draw it as a bar chart. Draw an X for a count of 10 and show the remainder as an integer. Here are some function prototypes that may help you. You are not required to use them, they are only a suggestion. VISearchs the file given and fill in a vector with the number //of people who drink at each level 1-5, for both males and females I/Only count people with min age age max age oid analyze(string filename, vectorkint> & results, int min age, int max age: //Prints out the vector as a bar chart void print chart (vectorkint> & results); //Print a single row X for a 10, int 1-9 for remainder //Example: Prints 27 as XX7 and 31 as XX1 void print bar(int num); Example Run

Explanation / Answer

main.cpp

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

void analyze(string filename, vector<int> & results, int min_age, int max_age);
void print_chart(vector<int> & results);
void print_bar(int num);

int main(int argc, char const *argv[]) {
int minAge = 0;
int maxAge = 0;

cout << "Analyze Alcohol Data" << endl;
cout << "Enter minimum age as integer: ";
cin >> minAge;
cout << "Enter maximum age as integer: ";
cin >> maxAge;

vector<int> results(10, 0);
analyze("alcohol.txt", results, minAge, maxAge);
print_chart(results);
return 0;
}

void analyze(string filename, vector<int> &results, int min_age, int max_age) {
ifstream data;
data.open(filename);
if(!data.is_open()) {
    cout << "ERROR: File could not be read." << endl;
} else {
    string sex = "";
    while(data >> sex) {
      int age = 0;
      int freq = 0;
      int mod = (sex == "M") ? 2 : 1;
      data >> age;
      data >> freq;
      if(age >= min_age && age <= max_age) {
        results[(freq * 2) - mod]++;
      }
    }
}
}

void print_chart(vector<int> &results) {
for(int i = 0; i < results.size(); i++) {
    if(i % 2 == 0)
      cout << "M";
    else
      cout << "F";
    cout << " Freq. " << ((i / 2) + 1) << " ";
    print_bar(results[i]);
  
alcohol.txt

F 18 1
F 17 1
F 15 3
F 15 1
F 16 2
M 16 2
M 16 1
F 17 1
M 15 1
M 15 1
F 15 2
F 15 1
M 15 3
M 15 2
M 15 1
F 16 2
F 16 2
F 16 1
M 17 4
M 16 3
M 15 1
M 15 1
M 16 3
M 16 4
F 15 1
F 16 3
M 15 2
M 15 4
M 16 1
M 16 5
M 15 4
M 15 1
M 15 1
M 15 1
M 16 1
F 15 1
M 15 1
M 16 1
F 15 1
F 15 1
F 16 2
M 15 4
M 15 1
M 15 1
F 16 2
F 15 1
F 16 4
M 16 1
M 15 2
F 15 1
F 16 3
F 15 1
M 15 4
F 15 3
F 15 4
F 16 1
F 15 1
M 15 1
M 15 1
F 16 1
F 16 3
F 16 5
F 16 1
F 16 4
F 15 4
F 16 2
M 15 5
F 16 2
F 15 3
F 15 3
M 16 1
M 15 1
F 15 4
M 16 2
F 16 4
M 15 3
M 15 1
F 16 3
M 17 1
F 16 2
M 15 3
M 15 2
F 15 1
M 15 3
F 15 3
F 15 3
F 16 2
F 15 3
M 16 1
M 16 5
F 16 3
F 15 3
F 16 3
F 16 1
M 15 1
F 15 1
M 16 1
F 16 1
F 16 2
F 16 1
M 16 5
M 16 1
M 15 1
F 15 1
M 15 1
F 15 1
F 15 1
M 16 1
M 15 5
F 16 1
M 15 1
F 16 1
F 16 1
M 15 1
M 15 1
M 16 2
M 15 1
M 16 1
M 17 4
M 15 2
F 15 2
M 15 2
F 16 2
M 16 4
F 16 1
M 15 2
F 15 1
F 19 1
M 18 2
M 16 5
F 15 2
F 15 2
F 17 3
F 16 4
M 15 1
F 15 1
M 17 4
F 16 1
M 16 3
F 15 1
M 15 1
M 16 2
F 15 1
F 16 3
M 17 2
F 15 2
F 15 1
F 15 1
M 16 1
M 15 5
M 18 5
M 16 5
F 15 3
M 19 1
F 17 1
M 15 1
M 17 3
F 18 5
M 16 2
M 16 4
M 17 2
M 15 4
M 16 4
M 17 4
M 17 5
M 16 1
M 16 4
F 16 1
F 16 1
F 16 1
M 16 4
M 16 1
M 17 3
F 16 1
F 16 1
M 17 4
F 16 4
M 17 4
M 16 4
M 17 1
M 16 3
M 16 2
F 17 3
F 17 3
F 16 2
M 17 3
M 16 2
M 16 2
F 17 3
M 17 5
F 16 1
F 17 1
M 17 5
M 16 4
M 16 1
F 17 1
M 17 2
M 16 5
F 17 3
F 16 2
F 16 5
F 16 3
F 17 3
F 17 2
F 16 1
F 17 4
F 16 2
F 16 1
F 16 4
F 17 1
F 19 2
M 17 5
F 16 1
M 18 4
F 17 2
F 17 3
F 17 4
M 18 4
F 17 4
F 17 1
F 17 2
F 17 1
F 16 1
M 18 5
F 16 1
F 18 1
F 17 3
M 17 3
M 18 5
F 17 2
F 17 2
M 17 1
M 17 3
M 16 4
M 16 1
M 16 3
M 17 5
F 16 1
F 17 1
M 18 5
M 17 4
M 17 3
M 16 1
M 16 2
F 18 1
M 16 1
M 17 1
M 22 5
M 18 3
M 16 4
M 18 4
M 16 3
M 18 5
M 16 3
M 17 4
M 17 2
F 17 1
M 19 1
M 18 2
F 17 1
F 18 3
M 18 1
M 18 1
F 17 1
F 18 1
M 18 4
M 17 4
F 18 2
M 18 3
F 18 2
F 19 3
F 18 3
F 18 1
M 17 2
F 17 1
F 17 3
F 18 1
M 18 4
F 18 1
M 18 2
M 17 4
M 17 4
F 18 1
F 18 1
F 17 2
M 17 2
F 18 2
F 17 1
M 18 3
M 18 1
M 18 4
F 17 2
F 18 1
F 17 1
M 18 1
M 17 3
F 19 3
F 18 2
F 18 1
M 18 2
F 18 1
M 17 2
F 17 1
F 17 2
M 19 1
F 18 1
M 20 1
M 19 1
M 19 2
F 19 3
F 19 2
F 19 1
M 19 2
F 19 2
F 19 1
F 19 1
F 18 2
F 18 1
F 17 5
F 18 3
F 17 2
F 17 1
F 17 2
F 17 3
F 17 3
M 18 2
M 17 5
M 17 5
F 17 3
F 17 2
M 18 4
F 17 1
F 18 1
F 18 1
F 18 1
F 17 3
F 19 2
F 17 3
F 18 1
F 17 3
F 19 3
M 18 2
M 18 3
F 17 2
F 18 2
F 18 3
M 18 2
M 18 3
F 17 3
M 18 5
M 19 3
M 17 3
M 18 3
M 19 3
M 17 3
F 18 1
F 17 2
F 17 2
M 18 2
F 18 1
F 18 4
M 18 3
F 18 3
F 17 1
F 17 2
M 18 4
M 18 2
F 17 2
F 18 2
F 18 2
F 19 1
M 18 3
F 17 1
F 17 3
F 18 1
F 18 2
F 20 1
F 18 4
F 18 2
F 17 3
M 18 4
M 18 3
M 17 1
M 19 3
M 18 3
F 18 3
F 18 2
F 19 2
F 18 1
F 18 1
M 20 5
M 17 4
M 21 3
M 18 4
M 19 3