Practical 10 Part B Input

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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