Answer to Question #48964 in C++ for Kevin

Question #48964
Write a program that can enter marks for 1 student and calculate his total and
grade with the following format
The inputs:
ID (As Integer), First (As Double), Second (As Double), Final (As Double), Total (As
Double) and Grad (As Char)
The Grades are: A for 90’s, B for 80’s, C for 70’s, D for 60’s and F for less than 60.
The output:
St_Nm ID First Second Final Average Grade
1 20071156 15.5 16.5 30.1 62.1 D
1
Expert's answer
2014-11-18T00:49:48-0500
#include <iostream>
using namespace std;

int studentNumber = 1; //if we want to add one more functioncalling
//so number will
be increment

//define the prototypes
char GetGrade(double mark); //this function gets letterequivalent of numeric mark
void ShowResult(int id, double firstMark, doublesecondMark,
double finalMark);
bool IsValidMark(double mark); //here is if block
//one more if block is in ShowResult()

//I don't know do mark can be negative, so I've addedboolean function, that checks marks
// if it can be negative you can delete some if blocks(imarked it)
int main()
{
int studentId;
double firstMark;
double secondMark;
double finalMark;

cout << "Enter the srudentID: ";
cin >> studentId;
cout << "Enter the firstmark: ";
cin >> firstMark;
cout << "Enter the secondmark: ";
cin >> secondMark;
cout << "Enter the finalmark: ";
cin >> finalMark;

cout << "Theresults:\n";
//Headers
cout << "Student_Number"<< "\t" << "Student_Id" << "\t"
<< "First" << "\t" <<
"Second" << "\t" <<"Final" << "\t" << "Total" <<
"\t" << "Grade" << endl;
ShowResult(studentId, firstMark,secondMark, finalMark);

return 0;
}

void ShowResult(int id, double firstMark, double secondMark,double finalMark)
{
if (!(IsValidMark(firstMark))) //I'veadded one more brackets to make this line more clearly, to avoid mistakes
{
cout << "Thefirst mark is invalid!" << endl;
return;
}
if (!(IsValidMark(secondMark)))
{
cout << "Thesecond mark is invalid!" << endl;
return;
}
if (!(IsValidMark(finalMark)))
{
cout << "Thefinal mark is invalid!" << endl;
return;
}
double total = firstMark + secondMark +finalMark;
char grade = GetGrade(total);
//Values
cout << studentNumber <<"\t" << id << "\t" << firstMark <<
"\t" <<
secondMark << "\t" << finalMark<< "\t" << total << "\t" << grade
<< endl;

studentNumber++; //if you want to callthis function one more time
}

bool IsValidMark(double mark)
{
if (mark > 0)
{
return true;
}
return false;
}

char GetGrade(double mark)
{
if (mark >= 90)
{
return 'A';
}
else if (mark >= 80)
{
return 'B';
}
else if (mark >= 70)
{
return 'C';
}
else if (mark >= 60)
{
return 'D'
}
else
{
return 'F';
}
}
=================================================================
=================================================================
That's all.
Also I'll give you object-oriented code:
In main file

class Student
{
static int studentNumber = 1; //staticmeans that variable will not recreates during all program time
int studentId;
double firstMark;
double secondMark;
double finalMark;
public:
//constructor with initialization string
Student (int studentId, doublefirstMark, double secondMark, double finalMark) :
studentId(studentId),firstMark(firstMark), secondMark(secondMark), finalMark(finalMark)
{ }

//destructor - it is called when classinstance is destroyed; it hasn't parameters
~Student()
{
studentNumber--;
}

public void ShowResult()
{
if(!(IsValidMark(firstMark))) //I've added one more brackets to make this line
more clearly, to avoid mistakes
{
cout<< "The first mark is invalid!" << endl;
return;
}
if(!(IsValidMark(secondMark)))
{
cout<< "The second mark is invalid!" << endl;
return;
}
if(!(IsValidMark(finalMark)))
{
cout<< "The final mark is invalid!" << endl;
return;
}
double total = firstMark+ secondMark + finalMark;
char grade =GetGrade(total);
//Values
cout <<studentNumber << "\t" << id << "\t"
<< firstMark << "\t" <<
secondMark << "\t"<< finalMark << "\t" << total <<
"\t" << grade << endl;

studentNumber++; //if youwant to call this function one more time
}
private: //these methods are private, because they are partsof realisation, so they must be incapsulated
char GetGrade(double mark)
{
if (mark >= 90)
{
return 'A';
}
else if (mark >= 80)
{
return 'B';
}
else if (mark >= 70)
{
return 'C';
}
else if (mark >= 60)
{
return 'D'
}
else
{
return 'F';
}
}

bool IsValidMark(double mark)
{
return mark > 0;
}
}

int main()
{
int studentId;
double firstMark;
double secondMark;
double finalMark;

cout << "Enter the srudentID: ";
cin >> studentId;
cout << "Enter the firstmark: ";
cin >> firstMark;
cout << "Enter the secondmark: ";
cin >> secondMark;
cout << "Enter the finalmark: ";
cin >> finalMark;

cout << "Theresults:\n";
//Headers
cout << "Student_Number"<< "\t" << "Student_Id" << "\t"
<< "First" << "\t" <<
"Second" << "\t" <<"Final" << "\t" << "Total" <<
"\t" << "Grade" << endl;
Student student(studentId, firstMark,secondMark, finalMark);
student.ShowResults();

return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS