Question Programming using C You are asked to write a progra

Question: Programming using C++

You are asked to write a program which accepts an unknown number of grades from a file and displays the highest and lowest grades, the class average as well as the number of grades less than 50 and the number of grades greater than or equal to 50.

The grades entered are between 0 and 100. If the teacher enters 0 as the number of grades to process, display an appropriate message. Your program should read from the file until a negative grade is entered. You can assume that the grades entered in the file are valid. Be sure to include in the statistics displayed the number of grades read.

Your program should include:
1. Welcome the teacher.
2. Request the number of grades the teacher will enter.
3. Read the requested number of grades and display the requested information.
4. End with a closing message..

Restrictions: Your solution must contain a for loop.

How to create a data file:

Using the C++ editor, open a blank file, type in the text and save the file. Name your data file
a3q2.dat. Save it in the same folder as the .cpp file containing the source code that will read
the text file.

Below is how your program should display:

D DEBUG TEAM SQL TOOLS TEST ANALYZE WINDOW HELP Debug Win32 Local Windows Debugger Auto a3q2.dat + ) A3Q2.cpp 55.5 78 96 67 23.5 82.5 45 15 77 68 -1 Figure 3. Sample content of input file a3q2.dat

Solution

Ans:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream infile(\"a3q2.dat\");//open file in input mode i.e read mode
if(!infile)//if file not foud
{
cout<<\"\ Failed to open file\ \";
return 1;
}

double *p;
int ngrades;
cout<<\"Welcome to Teacher\ \";
grd: cout<<\"Number of grades entered: \";
cin>>ngrades;
if(ngrades==0){
cout<<\"grades can\'t be zero,enter correct grades\ \";
goto grd;
}
p=new double[ngrades];//dynamically allocating memory

double n;
int i=0;
double high,low,avg,sum=0;
int gl50=0,gg50=0;;
while(!infile.eof())
{
infile>>n;
if(n==-1)
break;
p[i]=n;//reading numbers from file and storing in an array
i++;
}
high=p[0];
low=p[0];
for(int j=0;j<i;j++)//to find results
{

sum=sum+p[j]; //to find sum of grades
if(p[j]>high) //to find highest grade
high=p[j];
if(p[j]<low) //to find lowest grade
low=p[j];
if(p[j]>=50) //to cound grades greater than 50
gg50++;
if(p[j]<50) //to coung grades less than 50
gl50++;

}
avg=sum/i;
cout<<\"\ Here are the requested stats for the \"<<ngrades<<\" grades entered\ \";
cout<<\"The class Average is \"<<avg<<\"\ \";
cout<<\"The highest grade is \"<<high<<\"\ \";
cout<<\"The lowest grade is \"<<low<<\"\ \";
cout<<\"The Number of students who scored less than 50 is \"<<gl50<<\"\ \";
cout<<\"The Number of students who scored 50 or mored is \"<<gg50<<\"\ \ \";
cout<<\"Bye Bye\";
return 0;
}//end of main

Question: Programming using C++ You are asked to write a program which accepts an unknown number of grades from a file and displays the highest and lowest grade
Question: Programming using C++ You are asked to write a program which accepts an unknown number of grades from a file and displays the highest and lowest grade

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site