0% found this document useful (0 votes)
49 views4 pages

Practical 10 Part B Input

The document describes a C++ program that implements a student record system. The program uses a struct to define a student record with name, mark1, mark2, and average fields. It contains functions to read student marks into an array, calculate the average, and list the student records. The main function implements a menu to allow the user to read marks, list records, or quit. It demonstrates using structs, arrays, and functions to organize and process related data in C++.

Uploaded by

Ng Siewmin
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)
49 views4 pages

Practical 10 Part B Input

The document describes a C++ program that implements a student record system. The program uses a struct to define a student record with name, mark1, mark2, and average fields. It contains functions to read student marks into an array, calculate the average, and list the student records. The main function implements a menu to allow the user to read marks, list records, or quit. It demonstrates using structs, arrays, and functions to organize and process related data in C++.

Uploaded by

Ng Siewmin
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/ 4

PRACTICAL 10

PART B
INPUT
#include <iostream>
#include <cmath>
using namespace std;

typedef struct
{
int numerator;
int denominator;
} FRACTION;

//can be using called function also


FRACTION multiply(FRACTION f1, int num);

int main(void)
{
int num;
FRACTION f1, f2;

cout << "Please enter the integer: ";


cin >> num;
cout << "Please enter the numerator of fraction: ";
cin >> f1.numerator;
cout << "Please enter the denomenator of fraction: ";
cin >> f1.denominator;

f2 = multiply(f1, num);
cout << "The resulting fraction is " << f2.numerator << "/" << f2.denominator <<
endl;
return 0;

FRACTION multiply(FRACTION f1, int num)


{
FRACTION f2;

f2.numerator = f1.numerator*num;
f2.denominator = f1.denominator;

return f2;
}
OUTPUT

PART D
INPUT

#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;
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;
}}
OUTPUT

PART C
QUESTION 1
The typedef creates a new type by giving a name to a data type.
QUESTION 2
A structure is a collection of elements of possibly different data types.
QUESTION 3
Using keyword struct or keywords typedefstruct
QUESTION 4
Using variable-name.member-name (where the dot is the selection operator).

You might also like