Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let\'s consider an input file that contains student id (int), first name (string

ID: 3815300 • Letter: L

Question

Let's consider an input file that contains student id (int), first name (string), last name (string), and grade (float). We assume that the file contains between 1 and 100 grades. Write a program that: Declare the structure named student composed of id (int), first (string), last (string), and grade (float). Asks the user for the name of the input file. Reads the grades from the file to fill up an array of type student. Calculates and displays the maximum grade in the array with name. Calculates and displays the minimum grade in the array with name. Calculates and displays the average grade calculates and displays how many students were processed. Produce a file with the information above based on a file name provided by the user. The main idea is to explore the benefits of using structs. So every task should be done after the whole array is filled up The files must contain at least 5 records All items must be done with functions w/.parameters.

Explanation / Answer

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

void writeDataToFile(char f_name[20]);
void readDataFromFile(char f_name[20]);
void calculateResult(int size);

struct Student {
int stud_id;
string first, last;
float grade;
}record[5];

int main()
{
char file_name[20];

cout << "Enter file name : ";
cin >> file_name;

writeDataToFile(file_name);
readDataFromFile(file_name);
  
return 0;
}


void writeDataToFile(char f_name[20]) {

for (int i=0; i < 5; i++) {
cout << "Enter student detail " << (i+1) << endl;
cout << "stud_id : ";
cin >> record[i].stud_id;
cout << "first : ";
cin >> record[i].first;
cout << "last : ";
cin >> record[i].last;
cout << "grade : ";
cin >> record[i].grade;
}
  
ofstream ofile;
ofile.open(f_name);
  
for (int i=0; i < 5; i++) {
ofile.write((char *)&record[i], sizeof(record[i]));
}

ofile.close();
  
}

void readDataFromFile(char f_name[20]) {

ifstream ifile;
ifile.open(f_name);
  
int i=0;
while (ifile.read((char *)&record[i], sizeof(record[i]))) {
i++;   
}

ifile.close();
  
cout << endl << "-------------------------------" << endl;
cout << "Total records proceed : " << i << endl << endl;
  
for (int j=0; j < i; j++) {
cout << setw(4) << record[j].stud_id << setw(15) << record[j].first << " " << record[j].last << setw(5) << record[j].grade << endl;
}
  
calculateResult(i);
  
}

void calculateResult(int size) {
  
float max = 0, min = 0, total = 0;
int min_position = 0, max_position = 0;
  
for (int j=0; j < size; j++) {
  
if(j == 0) {
min = record[j].grade;
max = record[j].grade;
}
  
if(record[j].grade < min ) {
min = record[j].grade;
min_position = j;
}
  
if(record[j].grade > max ) {
max = record[j].grade;
max_position = j;
}
  
total = total + record[j].grade;
}
  
cout << "-------------------------------" << endl;
cout << "Maximum grade : " << endl << endl;
cout << setw(4) << record[max_position].stud_id << setw(15) << record[max_position].first << " " << record[max_position].last << setw(5) << record[max_position].grade << endl;
cout << "-------------------------------" << endl;
cout << "Minimum grade : " << endl << endl;
cout << setw(4) << record[min_position].stud_id << setw(15) << record[min_position].first << " " << record[min_position].last << setw(5) << record[min_position].grade << endl;
cout << "-------------------------------" << endl;
cout << "Average grade : " << (total / size) << endl << endl;
cout << "-------------------------------" << endl;
  
ofstream ofile;
ofile.open("Output.txt");
ofile << "-------------------------------" << endl;
ofile << "Maximum grade : " << endl << endl;
ofile << setw(4) << record[max_position].stud_id << setw(15) << record[max_position].first << " " << record[max_position].last << setw(5) << record[max_position].grade << endl;
ofile << "-------------------------------" << endl;
ofile << "Minimum grade : " << endl << endl;
ofile << setw(4) << record[min_position].stud_id << setw(15) << record[min_position].first << " " << record[min_position].last << setw(5) << record[min_position].grade << endl;
ofile << "-------------------------------" << endl;
ofile << "Average grade : " << (total / size) << endl << endl;
ofile << "-------------------------------" << endl;
ofile.close();
}

Output :::::::

---------------------------------------------
Total records proceed : 5

1 Hery Williams 60
2 Chrlie Smith 90
3 Oliver Jones 80
4 Jackson Brown 70
5 Sarah Brown 40
---------------------------------------------
Maximum grade :

2 Chrlie Smith 90
---------------------------------------------
Minimum grade :

5 Sarah Brown 40
---------------------------------------------
Average grade : 68

---------------------------------------------