Write a c++ program to calculate gpa in a semester


Asked by admin @ in Computers and Technology viewed by 346 People


Write a C++ program that opens and reads file question2.txt. File question1.txt has 3 students and 3 grades of each. You program should read values from file and assign values to 4 variables: (string) name, (double) grade1, (double) grade2, (double) grade3. Then, it should calculate the average grade of each student with the precision of 2 (use setprecision(n) function from header file) and find maximum and minimum grade in the class. Finally, you should have a report on both: • console screen • Output file named output2.txt in the following output format: Max Grade max Min Grade min Adam ave_grade John ave_grade Bill ave_grade

Answered by admin @



Answer:

see explaination

Explanation:

#include <fstream>

#include <iostream>

#include <string>

//to set precision

#include <iomanip>

#include <bits/stdc++.h>

//structure of student is created.

struct Student

{

std::string name;

double grade1;

double grade2;

double grade3;

double Average;

// Constructor initializes semester scores to 0.

Student()

{

grade1 = 0;

grade2 = 0;

grade3 = 0;

Average = 0;

}

//Read from the file and calculate the average, min and max grade with name.

void read(std::fstream& fil)

{

std::string temp;

fil >> name;

fil >> temp;

grade1 = std::stod(temp);

fil >> temp;

grade2 = std::stod(temp);

fil >> temp;

grade3 = std::stod(temp);

Average = ((grade1 + grade2 + grade3) /3.0);

}

//write to file- name and average with precision 2.

void write(std::fstream& fil)

{

fil << name << " " << std::setprecision(2) << std::fixed << Average << std::endl;

}

//Logic to calculate the minimum grade of the class.

double min()

{

double min = grade1;

if(min > grade2)

{

min = grade2;

}

if(min > grade3)

{

min = grade3;

}

return min;

}

//Logic to calculate the maximum grade of the class.

double max()

{

double max = grade1;

if(max < grade2)

{

max = grade2;

}

if(max < grade3)

{

max = grade3;

}

return max;

}

//Print the name and average with precision 2 in the console.

void print()

{

std::cout << name << " " ;

std::cout << std::setprecision(2) << std::fixed << Average << std::endl;

}

};

//As per this question,students are set to 3.

Student students[3];

//main method.

int main()

{

// Read from the file question2.txt

std::fstream fil;

fil.open("question2.txt", std::ios::in);

for(int i = 0; i < 3; i++)

students[i].read(fil);

fil.close();

// finding the max grade of the class.

int max = students[0].max();

int temp = students[1].max();

if(max <temp)

max = temp;

temp = students[2].max();

if(max < temp)

max = temp;

// finding the min grade of the class.

int min = students[0].min();

temp = students[1].min();

if(min > temp)

min = temp;

temp = students[2].min();

if(min > temp)

min = temp;

// print the output in console.

std::cout << "Max Grade " << max << std::endl;

std::cout << "Min Grade " << min << std::endl;

for(int i = 0; i < 3; i++)

students[i].print();

// write to output file output2.txt .

fil.open("output2.txt", std::ios::out);

fil << "Max Grade " << max << std::endl;

fil << "Min Grade " << min << std::endl;

for(int i = 0; i < 3; i++)

students[i].write(fil);

fil.close();

return 0;

}


Similar Questions

Write a program to calculate area of circle in c++

Asked by admin @ in Computers and Technology viewed by 300 persons

I want to write a C++ program to read the diameter of a circle. The program should find and display the area and circumference of the circle. Define a constant …

C++ program to calculate area of rectangle using constructor

Asked by admin @ in Computer Science viewed by 331 persons

C++ program to calculate area of traingle circle and rectangle using constructor overloading

Write a c++ program to accept and print your name

Asked by admin @ in Computer Science viewed by 317 persons

Write a C++ program to accept and print your name?​

Write a program to calculate compound interest in python

Asked by admin @ in Physics viewed by 404 persons

Write a program in python to calculate the compound interest​

C programing

Asked by pranav94 @ in Engineering viewed by 453 persons

To write the correct and effective program we much first

Asked by admin @ in Computer Science viewed by 1009 persons

To write the correct and effective program we must first

To write the correct and effective program we must first

Asked by admin @ in Computer Science viewed by 767 persons

To write the correct and effective program be much first

Write a program to split number into digits in java

Asked by admin @ in Computers and Technology viewed by 318 persons

Write a Java program that asks the user to enter a 10-digit string as a typical U.S. telephone number, extracts the 3-digit area code, the 3-digit "exchange," and the remaining …

Write a php program to read the employee details

Asked by admin @ in Computer Science viewed by 308 persons

Write a php program to read the employee details

Write a program to find average of three numbers

Asked by admin @ in Computer Science viewed by 322 persons

Write a program to find average of three numbers.

Write a program to display your name in python

Asked by admin @ in Computer Science viewed by 330 persons

Write a code in python to display your name and age.

A programmer writes a program to find an element

Asked by admin @ in Computer Science viewed by 804 persons

A programmer writes a program to find an element in the array A[5] with the elements:8 30 40 45 70 the program is run to find a number X,that is …

What is the first step in write a program

Asked by admin @ in Computer Science viewed by 321 persons

Write true or false: 1- The first step in writing a program is to develop an algorithm for it. 2- Java code is also called as ‘write once run anywhere’ …

Most viewed questions in Computers And Technology


A customer wants to increase the storage capacity by 25gb

Asked by admin @ in Computers and Technology viewed by 4402 persons




Solid yellow line bordering your lane of traffic marks a

Asked by admin @ in Computers and Technology viewed by 583 persons



Which of the following is not a windows utility program

Asked by admin @ in Computers and Technology viewed by 570 persons


Which operation on a pwc requires more than idle speed

Asked by admin @ in Computers and Technology viewed by 553 persons


When is a wrecker considered to be an emergency vehicle

Asked by admin @ in Computers and Technology viewed by 551 persons



Which of the following statements is true of a database

Asked by admin @ in Computers and Technology viewed by 532 persons


Which of the following is not true about an entrepreneur

Asked by admin @ in Computers and Technology viewed by 517 persons


Kitchen gadgets draw a context diagram for the order system

Asked by admin @ in Computers and Technology viewed by 486 persons