0% found this document useful (0 votes)
48 views16 pages

Name: Luong Phuong Anh ID: IELSIU18241 Introduction To Programming C++ Lab 6 Programming Using Structure Data Type

The document describes 4 programs using C++ structures: 1. A program to calculate the perimeter and area of a rectangle using a Rectangle structure. 2. A program that allows a user to enter two fractions, perform operations on them (sum, difference, product, quotient), using a Fraction structure. 3. A program with time conversion options between hours:minutes:seconds and seconds, and time addition, using a Time structure. 4. A program to store and sort student data by name, ID, and GPA, using a Student structure. The program allows input, output, and sorting of student records.

Uploaded by

Phuong Anh Luong
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)
48 views16 pages

Name: Luong Phuong Anh ID: IELSIU18241 Introduction To Programming C++ Lab 6 Programming Using Structure Data Type

The document describes 4 programs using C++ structures: 1. A program to calculate the perimeter and area of a rectangle using a Rectangle structure. 2. A program that allows a user to enter two fractions, perform operations on them (sum, difference, product, quotient), using a Fraction structure. 3. A program with time conversion options between hours:minutes:seconds and seconds, and time addition, using a Time structure. 4. A program to store and sort student data by name, ID, and GPA, using a Student structure. The program allows input, output, and sorting of student records.

Uploaded by

Phuong Anh Luong
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/ 16

Name: Luong Phuong Anh

ID: IELSIU18241

INTRODUCTION TO PROGRAMMING C++

LAB 6

Programming Using Structure Data Type


1/ Using structure method to write a C program that calculate the perimeter and area of a

rectangle with length and width entered by user.

- Algorithm
1. Declare struct named Rectangle including 2 double fields named ‘width’ and ‘length’.
2. Declare a variable 'rect' of struct Rectangle type.
3. Enter the length via rect.length.
4. Enter the width via rect.width.
5. Print the circumference to the screen: (rect.length + rect.width) * 2.
6. Print the area to the screen: rect.length * rect.width.
- Code:
#include <iostream>

using namespace std;

struct Rectangle

double length;

double width;

};

int main()

Rectangle rect;

cout << "Enter the length of rectangle: ";

cin >> rect.length;


cout << "Enter the width of rectangle: ";

cin >> rect.width;

cout << "Perimeter of rectangle: " << (rect.length + rect.width) * 2 << endl;

cout << "Area of rectangle: " << rect.length * rect.width << endl;

return 0;

- Output

2/ Using structure method to write a C program that user will enter 2 fraction number, simple them
then calculate their sum, difference, product and quotient.

Output:

- Algorithm:

1. The struct declaration name Fraction consists of two fields of type int named ‘tuSo’ and ‘mauSo’.
2. Declare 3 variables ‘a’, ‘b’, ‘c’ of struct Fraction type.
3. Call the Input (a) function (refer to the reference type).
3.1. Enter the numerator via a.tuSo
3.2. Enter the denominator via a.mauSo (enter until the denominator is nonzero then stop typing)
4. Call the fraction reduction function simpleFraction (a) (a referenced by reference type).
4.1. For i from a.tuSo downto 1
4.2. If both a.tuSo and a.mauSo are divisible by i, then i is the greatest common divisor of both
the death and the sample, stopping the loop
4.3. a.tuSo = a.tuSo / i
4.3. a.mauSo = a.mauSo / i
5. Print the fraction ‘a’ to the screen.
6. Follow steps 3, 4 and 5 for the variable ‘b’.
7. Call the function to sum 2 addition fractions (a, b) and store the result in the variable ‘c’.
7.1. Declaring a variable ‘temp’ of struct type Fraction;
7.2. temp.tuSo = a.tuSo * b.mauSo + b.tuSo * a.mauSo
7.3. temp.mauSo = a.mauSo * b.mauSo
7.4. Call the simpleFraction (temp) function to shorten ‘temp’
7.5. Returns the value of the variable ‘temp’
8. Print fractions ‘c’ is the sum of 2 fractions ‘a’ and ‘b’ on the screen.
9. Call the function to calculate the 2 sub fractions (a, b) and save the result in the variable ‘c’.
9.1. b.tuSo = b.tuSo * (-1);
9.2. Call and return the value of the function to sum 2 fractions addition (a, b)
10. In fractions ‘c’ is the difference between 2 fractions ‘a’ and ‘b’ on the screen.
11. Call the function to calculate the 2 mul fractions (a, b) and store the result in the variable ‘c’.
11.1. Declaring a variable ‘temp’ of struct type Fraction;
11.2. temp.tuSo = a.tuSo * b.tuSo
11.3. temp.mauSo = a.mauSo * b.mauSo
11.4. Call the simpleFraction (temp) function to shorten ‘temp’
11.5. Returns the value of the variable ‘temp’
12. Print fractions ‘c’ is product of 2 fractions ‘a’ and ‘b’ on the screen.
13. Call the quotient function to divide 2 fractions divide (a, b) and save the result in the variable ‘c’.
13.1. Declare a variable ‘temp’ of struct type Fraction
13.2. temp.tuSo = b.mauSo
13.3. temp.mauSo = b.tuSo
13.4. Call and return the value of the integral function 2 mul fractions (a, b)
14. Print fractions ‘c’ is the quotient of 2 fractions ‘a’ and ‘b’ on the screen.
- Code:
#include <iostream>

using namespace std;

struct Fraction

int tuSo;

int mauSo;

};
int ucln(int a, int b)

if (a > b)

swap(a, b);

for (int i = a; i > 0; i--)

if (a % i == 0 && b % i == 0)

return i;

void simpleFraction(Fraction& f)

int u = ucln(f.tuSo, f.mauSo);

f.tuSo /= u;

f.mauSo /= u;

Fraction addition(Fraction a, Fraction b)

Fraction c;

c.tuSo = a.tuSo * b.mauSo + b.tuSo * a.mauSo;

c.mauSo = a.mauSo * b.mauSo;

simpleFraction(c);

return c;

}
Fraction sub(Fraction a, Fraction b)

b.tuSo *= -1;

return addition(a, b);

Fraction mul(Fraction a, Fraction b)

Fraction c;

c.tuSo = a.tuSo * b.tuSo;

c.mauSo = a.mauSo * b.mauSo;

simpleFraction(c);

return c;

Fraction divide(Fraction a, Fraction b)

Fraction c;

c.tuSo = b.mauSo;

c.mauSo = b.tuSo;

return mul(a, c);

void Input(Fraction& a)

{
cout << "Input numerator: ";

cin >> a.tuSo;

do

cout << "Input denominator: ";

cin >> a.mauSo;

} while (a.mauSo == 0);

void Output(Fraction a)

cout << a.tuSo << "/" << a.mauSo << endl;

int main()

Fraction a, b, c;

Input(a);

simpleFraction(a);

cout << "Simple Fraction a : ";

Output(a);

Input(b);

simpleFraction(b);
cout << "Simple Fraction b : ";

Output(b);

c = addition(a, b);

cout << "The sum of Fraction a and Fraction b: ";

Output(c);

c = sub(a, b);

cout << "The difference of Fraction a and Fraction b: ";

Output(c);

c = mul(a, b);

cout << "The product of Fraction a and Fraction b: ";

Output(c);

c = divide(a, b);

cout << "The quotient of Fraction a and Fraction b: ";

Output(c);

return 0;

- Output:
3/ Using structure method to write a C program that ask user to choose 3 options, in which:

- Option 1: convert a hour format from hh.mm.ss to second

- Option 2: convert second to a hour format hh:mm:ss

- Option 3: add two times (hh:mm:ss)

- Algorithm:

1. The struct declaration Time has three int fields named ‘h’, ‘m’ and ‘s’.
2. Declare 3 variables ‘t1’, ‘t2’, ‘t3’ of struct Time type.
3. Call the Input (t1), Input (t2) (t1, t2 functions) as a reference.
3.1. Enter hours through time
3.2. Enter the time via t.m
3.3. Enter hours via t.s
4. Convert hours t1, t2 as hh: mm: ss hours into seconds by calling opt1 (t1) and opt1 (t2).
4.1. Calculate and return value: t.h * 3600 + t.m * 60 + t.s
5. Enter the variable t as time in seconds.
6. Convert t to hh: mm: ss form by calling opt2 (t).
6.1. Declare a ‘Time’ variable type struct Time
6.2. a.h = t / 3600;
6.3. a.m = (t - a.h * 3600) / 60;
6.4. a.s = t - a.h * 3600 - a.m * 60;
6.5. Returns the value of the variable ‘a’
7. Sum the 2 variables ‘t1’ and ‘t2’ by calling opt3 (t1, t2) and saving the result to the variable ‘t3’.
7.1. Declare and initialize the temp variable of type int: temp = opt1 (t1) + opt1 (t2)
7.2. Declaring and initializing the variable of type Time: t = opt2 (temp)
7.3. Returns the value of the variable ‘t’

- Code:

#include <iostream>

using namespace std;

struct time

int h;

int m;

int s;
};

int opt1(time t)

return t.h * 3600 + t.m * 60 + t.s;

time opt2(int t)

time a;

a.h = t / 3600;

a.m = (t - a.h * 3600) / 60;

a.s = t - a.h * 3600 - a.m * 60;

return a;

time opt3(time t1, time t2)

int temp = opt1(t1) + opt1(t2);

time t = opt2(temp);

return t;
}

void Input(time& t)

cout << "H: ";

cin >> t.h;

cout << "M: ";

cin >> t.m;

cout << "S: ";

cin >> t.s;

void Output(time t)

cout << t.h << ":" << t.m << ":" << t.s << endl;

int main()

time t1, t2;

Input(t1);

Input(t2);
cout << "Opt1: " << endl;

cout << "Time 1: " << opt1(t1) << endl;

cout << "Time 2: " << opt1(t2) << endl;

int t;

cout << "Opt2: " << endl;

cout << "Input: ";

cin >> t;

Output(opt2(t));

cout << "Opt3: " << endl;

time t3 = opt3(t1, t2);

Output(t3);

return 0;

- Output:
4/ Using structure method to write a C program that enter N student with Name, ID and

GPA. Print list of students in original form, sorted form by GPA and ID number.

Output:

- Algorithm:
1. Declare struct name Student includes 2 string fields named ‘name’, ‘id’ and 1 double type field is
‘grade’.
2. Declare array a with 100 elements of type struct Student.
3. Call the Input array function (a, n) (n is passed in reference style).
3.1. For i from 0 to n - 1
3.2. Alternately enter a [i] .name, a [i] .id, a [i] .grade
4. Call the function to print the original array Output (a, n).
4.1. For i from 0 to n - 1
4.2. In turn print a [i] .name, a [i] .id, a [i] .grade on the screen
5. Call the ascending array sort function by GPA sortGPA (a, n).
5.1. For i from 0 to n - 2
5.2. For j from i + 1 to n - 1
5.3. If a [i] .grade> a [j] .grade then swap the values of a [i] and a [j]
6. Call the function in array Output (a, n) after sorting in ascending order according to GPA.
7. Call the ascending array sort function by ID sortID (a, n).
7.1. For i from 0 to n - 2
7.2. For j from i + 1 to n - 1
7.3. If a [i] .id> a [j] .id then swap the values of a [i] and a [j]
8. Call the function in array Output (a, n) after sorting ascending by ID.
- Code:
#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

struct student

string name;

string id;

double grade;

};

void Input(student a[], int n)

for (int i = 0; i < n; i++)

cout << "Student " << i + 1 << endl;

cout << "Name: ";

cin >> a[i].name;

cout << "ID: ";

cin >> a[i].id;

cout << "Grade: ";

cin >> a[i].grade;


}

void Output(student a[], int n)

for (int i = 0; i < n; i++)

cout << "Student " << i + 1 << " information: ";

cout << setw(10) << left << a[i].name;

cout << setw(15) << left << a[i].id;

cout << setw(10) << left << a[i].grade << endl;

void sortGPA(student a[], int n)

for (int i = 0; i < n - 1; i++)

for (int j = i + 1; j < n; j++)

if (a[i].grade > a[j].grade)

swap(a[i], a[j]);

void sortID(student a[], int n)

{
for (int i = 0; i < n - 1; i++)

for (int j = i + 1; j < n; j++)

if (a[i].id > a[j].id)

swap(a[i], a[j]);

int main()

int n;

student a[100];

cout << "N = ";

cin >> n;

Input(a, n);

cout << "Original" << endl;

Output(a, n);

sortGPA(a, n);

cout << "Sort by GPA" << endl;

Output(a, n);

sortID(a, n);

cout << "Sort by ID" << endl;

Output(a, n);

return 0;

- Output:

You might also like