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

URGENT I forgot this one program and need it done for an assignment but don\'t h

ID: 3596339 • Letter: U

Question

URGENT

I forgot this one program and need it done for an assignment but don't have time.

create a text file with data include these functions
int menu();
int readFile(ifstream &infile, struct student s[]);
int displayStudents(struct student s[],
int size); float calculateAverage(struct student s[], int size);
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.

////////////////////
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>

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);

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 {
cout << calculateAverage(s, size);
}
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) {
float average = 0;
for (int i = 0; i < size; i++) {
float studentAvg = 0;
int j;
for (j = 0; j < s[i].no_of_units && j < 5; j++) {
studentAvg += s[i].mark[j];
}
average += (studentAvg/j);
}
return average/size;
}

data.txt

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

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
71.65331 to display data
2 to find average mark
-1 to exit.
-1