In: Computer Science
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE)
Write a C++ program that computes a student’s grade for an
assignment as a percentage given
the student’s score and total points. The final score must be
rounded up to the nearest whole
value using the ceil function in the <cmath> header file. You
must also display the floating-point
result up to 5 decimal places. The input to the program must come
from a file containing a single
line with the score and total separated by a space.
In addition, you must print to the console “Excellent” if the grade
is greater than or equal to 90,
“Well Done” if the grade is less than 90 and greater than or equal
to 80, “Good” if the grade is
less than 80 and greater than or equal to 70, “Need Improvement” if
the grade is less than 70 and
greater than or equal to 60, and “Fail” if the grade is less than
60.
Submit C++ Programming Assignment 3 by 11:59 p.m. (ET) on Monday of
Module/Week 3
Step 1:- In the above problem we can write a c++ program and read value from text file line by line and also used ceil function and give a grade to student and we can make a new text file to store value and grade
Step 2:-
program file:-code
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
int main()
{
fstream infile("Marks.txt",ios::in);
//read file and check that file is found or not
if(!infile){cerr<<"file could not be found!";exit(1);}
fstream outfile("output.txt",ios::out);
if(!outfile){cerr<<"file could not be created!";exit(1);}
// initialize variable
char fname[20];
char lname[20];
float grades;
int percentage;
char c;
int lines=1;
string grade;
double avg=0;
while(infile.get(c))
{if(c=='\n') lines++;}
infile.clear();
infile.seekg(0);
//visit lines for tale marks
for(int k=0;k<lines;k++)
{
//show student name
infile>>fname;
infile>>lname;
outfile<<fname<<" "<<lname<<" ";
float sum=0;
// red value of 5 subjects
for(int i=0;i<5;i++)
{
if(infile>>grades)
{sum+=grades;
outfile<<grades<<" ";}
}
//using ceil function by using cmath
percentage=ceil(sum)/5;
//by using setprecision function we can print value
outfile << fixed << setprecision(5) <<
sum<<endl;
.//Conditions for show the grade of students
if (percentage >= 90)
{
grade = "Excellent";
}
else
{
if (percentage >= 80 && percentage <= 89)
{
grade = "Well done";
}
else
{
if (percentage >= 70 && percentage <= 79)
{
grade = "good";
}
else
{
if (percentage >= 60 && percentage <= 69)
{
grade = "need improvement";
}
else
{
grade = "Fail";
}
}
}
outfile<<grade;
}
}
system("pause");
return 0;
}
output:-
54.00000
60.60000
Step 3:- Text File:-
Input file
name-> Marks.txt
First_name Last_name Marks1 Marks2 Marks3 Marks4 Marks5 - format
Joker Tom 14 99 26 74 57 mosco tom 67 38 56 95 47
output file :-
name-> output.txt
Joker Tom 14 99 26
74 57 54.00 Fail
mosco tom 67 38 56
95 47 60.60 need improvement
Summary:- In this above problem we can make a c++ program and full fill all requirement of question. for further query please comment.