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

create c++ to read the data and include these functions int menu(); int readFile

ID: 3597287 • Letter: C

Question

create c++ to read the data and include these functions
int menu();
int readFile(ifstream &infile, struct student s[]);
int displayStudents(struct student s[],int size);

double calculateAverage(struct student s[], int size,sting );
use this structure
struct student
{
string name;
int no_of_units;
int mark[5];
}

1 to display data
2 to find average mark
-1 to exit.

average mark is for signle student. It need seach the correct student name then calculate the average mark for that student.

data:
Ricky
2
55
70
Alex
4
96
67
88
98
Peter
3
45
37
61
Joseph
5
98
56
100
87
77
Tyler
4
65
80
79
85

Explanation / Answer

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

using namespace std;

struct student
{
string name;
int no_of_units;
int mark[5];
};

int menu();
int readFile(ifstream &infile, struct student s[]);
int displayStudents(struct student s[], int size);
float calculateAverage(struct student s[], int size, string name);

int main()
{
struct student s[100];
ifstream infile("data.txt");
  
int size = readFile(infile, s);
  
int choice = menu();
while(choice != -1) {
if (choice == 1) {
displayStudents(s, size);
}
else {
string name;
cout << "Enter name: ";
cin >> name;
float average = calculateAverage(s, size, name) ;
if (average != -1)
cout << "Average marks for " << name << " are " << average << endl;
else
cout << "Enter a valid name" << endl;
}
choice = menu();
}
  

return 0;
}

int readFile(ifstream &infile, struct student s[]) {
string name;
int size = 0;
while(infile >> name) {
s[size].name = name;
infile >> s[size].no_of_units;
for(int i = 0; i < s[size].no_of_units && i < 5; i++)
{
infile >> s[size].mark[i];
}
size++;
}
return size;
}

int menu() {
cout << "1 to display data ";
cout << "2 to find average mark ";
cout << "-1 to exit. ";
int choice;
cin >> choice;
return choice;
}

int displayStudents(struct student s[], int size) {
for (int i = 0; i < size; i++) {
cout << "Name: " << s[i].name << " Marks";
for (int j = 0; j < s[i].no_of_units && j < 5; j++) {
cout << " " << s[i].mark[j];
}
cout << endl;
}
return 0;
}

float calculateAverage(struct student s[], int size, string name) {
float average = -1;
for (int i = 0; i < size; i++) {
if (s[i].name == name) {
average = 0;
for (int j = 0; j < s[i].no_of_units; j++) {
average += s[i].mark[j];
}
average = average/s[i].no_of_units;
return average;
}
}
return average;
}

Sample run

1 to display data
2 to find average mark
-1 to exit.
1
Name: Ricky Marks 55 70
Name: Alex Marks 96 67 88 98
Name: Peter Marks 45 37 61
Name: Joseph Marks 98 56 100 87 77
Name: Tyler Marks 65 80 79 85
1 to display data
2 to find average mark
-1 to exit.
2
Enter name: Ricky
Average marks for Ricky are 62.5
1 to display data
2 to find average mark
-1 to exit.
-1