Hi, I was wondering if i could get some help with this cs homework I have (C++).
ID: 3756545 • Letter: H
Question
Hi, I was wondering if i could get some help with this cs homework I have (C++).
Heres the assignment,
CS 109/110
C++ Programming for Engineers with MATLAB
Fall 2018
Overview
Your lab attendance is
optional
. If you are able, please consider working in the
physical
lab
where the TAs are available to answer questions and offer help. Also, where other students are
there to collaborate with.
We would LOVE to have you in person in lab. Please do come! Your
lab assignment submission is
required
.
We are taking attend
ance in lab for data collection and
future planning, it will absolutely not affect your grade.
Expectations for Lab
Be kind. Be honest. Work hard.
Lab 5 Description
:
You will write a code that reads in a text file of scores and outputs a
summary of useful metrics.
Suppose
the file “scores.txt” contains the following integer values:
92
0
88
100
60
42
0
6
7
When your program runs, the first step is to input a filename from the keyboard. Let’s assume
the user enters
scores.txt
:
scores.txt
The program opens this file, inputs the values, counts, computes the average, and outputs the
following:
# students: 8
#
zero
: 2
# with 100: 1
Average: 74.8333
Therefore, your code needs to count the number of
total
scores (which will be the number of
students). It n
eeds to count the number of
zero scores. It needs to count the number of perfect
scores (those that equal
100). And finally, it needs to compute the average score, where t
he 0’s
are not included in the average. In general, assume the input file contains 1 or more valid
integers in the range 0..100, inclusive.
Please also
include the
error mess
age, “
Error:
unable to open file!
”
if the filename provided by the user
does not exist
.
Explanation / Answer
If you have any doubts, please give me comment...
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void print_histogram(int n, int max);
int main()
{
string filename;
int max;
cin >> filename >> max;
ifstream in;
in.open(filename.c_str());
if (in.fail())
{
cout << "Error: unable to open file!" << endl;
return 0;
}
int no_students(0), zero(0), score100(0), countA(0), countB(0), countC(0), countD(0), countF(0), score;
double average = 0.0;
while (!in.eof())
{
in >> score;
if(in.fail())
break;
no_students++;
if(score==0)
zero++;
else if(score==100)
score100++;
average += score;
if (score >= 90)
countA++;
else if (score >= 80)
countB++;
else if (score >= 70)
countC++;
else if (score >= 60)
countD++;
else
countF++;
}
in.close();
average /= (no_students-zero);
cout<<"# students: "<<no_students<<endl;
cout<<"# zero: "<<zero<<endl;
cout<<"# with 100: "<<score100<<endl;
cout<<"Average: "<<average<<endl;
cout<<"Grade Distribution:"<<endl;
cout<<"A: ";
print_histogram(countA, max);
cout<<"B: ";
print_histogram(countB, max);
cout<<"C: ";
print_histogram(countC, max);
cout<<"D: ";
print_histogram(countD, max);
cout<<"F: ";
print_histogram(countF, max);
return 0;
}
void print_histogram(int n, int max){
for(int i=0; i<n && i<max; i++){
cout<<"*";
}
if(n>max){
cout<<"..."<<n;
}
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.