Name: Luong Phuong Anh ID: IELSIU18241 Introduction To Programming C++ Lab 6 Programming Using Structure Data Type
Name: Luong Phuong Anh ID: IELSIU18241 Introduction To Programming C++ Lab 6 Programming Using Structure Data Type
ID: IELSIU18241
LAB 6
- 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>
struct Rectangle
double length;
double width;
};
int main()
Rectangle rect;
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>
struct Fraction
int tuSo;
int mauSo;
};
int ucln(int a, int b)
if (a > b)
swap(a, b);
if (a % i == 0 && b % i == 0)
return i;
void simpleFraction(Fraction& f)
f.tuSo /= u;
f.mauSo /= u;
Fraction c;
simpleFraction(c);
return c;
}
Fraction sub(Fraction a, Fraction b)
b.tuSo *= -1;
Fraction c;
simpleFraction(c);
return c;
Fraction c;
c.tuSo = b.mauSo;
c.mauSo = b.tuSo;
void Input(Fraction& a)
{
cout << "Input numerator: ";
do
void Output(Fraction a)
int main()
Fraction a, b, c;
Input(a);
simpleFraction(a);
Output(a);
Input(b);
simpleFraction(b);
cout << "Simple Fraction b : ";
Output(b);
c = addition(a, b);
Output(c);
c = sub(a, b);
Output(c);
c = mul(a, b);
Output(c);
c = divide(a, b);
Output(c);
return 0;
- Output:
3/ Using structure method to write a C program that ask user to choose 3 options, in which:
- 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>
struct time
int h;
int m;
int s;
};
int opt1(time t)
time opt2(int t)
time a;
a.h = t / 3600;
return a;
time t = opt2(temp);
return t;
}
void Input(time& t)
void Output(time t)
cout << t.h << ":" << t.m << ":" << t.s << endl;
int main()
Input(t1);
Input(t2);
cout << "Opt1: " << endl;
int t;
cin >> t;
Output(opt2(t));
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>
struct student
string name;
string id;
double grade;
};
swap(a[i], a[j]);
{
for (int i = 0; i < n - 1; i++)
swap(a[i], a[j]);
int main()
int n;
student a[100];
cin >> n;
Input(a, n);
Output(a, n);
sortGPA(a, n);
Output(a, n);
sortID(a, n);
Output(a, n);
return 0;
- Output: