0% found this document useful (0 votes)
58 views

UECS1643 Fundamentals of Programming: Practical 10 Answer Guidelines Part D (Practice Exercises)

This document provides code guidelines for Part D of a practical assignment. It defines a student record struct with name, marks, and average. It includes functions to read student marks into an array, and list the student records with formatting. The main function implements a menu to read marks, list records, or quit, calling the respective functions.

Uploaded by

Jing Ze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

UECS1643 Fundamentals of Programming: Practical 10 Answer Guidelines Part D (Practice Exercises)

This document provides code guidelines for Part D of a practical assignment. It defines a student record struct with name, marks, and average. It includes functions to read student marks into an array, and list the student records with formatting. The main function implements a menu to read marks, list records, or quit, calling the respective functions.

Uploaded by

Jing Ze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

UECS1643 Fundamentals Of Programming

Practical 10 Answer Guidelines

Part D (Practice Exercises)

#include <iostream>
#include <iomanip>
#include <cstring>
#define SIZE 30
using namespace std;

typedef struct
{
char name[31];
int mark1;
int mark2;
double average;
} STUDENTREC;

int read_mark(STUDENTREC stud_arr[], int index);


void list(const STUDENTREC stud_arr[], int size);

int main(void)
{
int action, index = 0;
bool quit = false;
STUDENTREC students[SIZE];

while(!quit)
{
cout <<"\n\t\t******************************************";
cout <<"\n\t\t**\tStudent Records Menu\t\t**";
cout <<"\n\t\t******************************************\n\n";
cout <<"\t\t<1> Read Marks.\n";
cout <<"\t\t<2> List.\n";
cout <<"\t\t<3> Quit.\n\n";
cout <<"\tPlease enter your option --> ";
cin >> action;
cout <<"\n\n";

if(action == 1)
index = read_mark(students, index);
else if(action == 2)
list(students, index);
else if(action == 3)
quit = true;
else
cout <<"\tWrong selection.\n";
}
}

int read_mark(STUDENTREC stud_arr[], int index)


{
if(index == SIZE)
cout << "Array is full.\n";
else
{
cout <<"\tPlease enter the student name : ";
cin.ignore();
cin.getline(stud_arr[index].name, 31);
cout <<"\tPlease enter the student mark 1: ";
cin >> stud_arr[index].mark1;

1
UECS1643 Fundamentals Of Programming

cout <<"\tPlease enter the student mark 2: ";


cin >> stud_arr[index].mark2;
stud_arr[index].average = (stud_arr[index].mark1 +
stud_arr[index].mark2) / 2.0;
cout <<"\tThe average of the student marks is "
<< fixed << setprecision(2) << stud_arr[index].average << endl;
index++;
}

return index;
}

void list(const STUDENTREC stud_arr[], int size)


{
if(size == 0)
{
cout << "\tNo record.\n";
return;
}

cout <<"Name\t\t\tMark1\t\tMark2\t\tAverage\n";
for(int i = 0; i < size; i++)
{
if (strlen(stud_arr[i].name) < 16)
cout << stud_arr[i].name << "\t\t"
<< setw(3) << stud_arr[i].mark1 << "\t\t"
<< setw(3) << stud_arr[i].mark2 << "\t\t"
<< setw(6) << fixed << setprecision(2)
<< stud_arr[i].average << endl;
else
cout << stud_arr[i].name << "\t"
<< setw(3) << stud_arr[i].mark1 << "\t\t"
<< setw(3) << stud_arr[i].mark2 << "\t\t"
<< setw(6) << fixed << setprecision(2)
<< stud_arr[i].average << endl;
}
}

You might also like