National Yang Ming Chiao Tung University GECG10069 (561085) Laboratory Manual 02
Department of Electrical & Computer Engineering September 8, 2025
Computer Intelligence on Automation (C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
GECG10069 (561085) F25: Introduction to Programming (C++)
Lab 4 : Literals & Variables
What you will learn from Lab 4
In this laboratory, you will further explore special output symbols and practice using
variables in your programs.
TASK 4-1 : #DEFINE V.S. CONST NUMBER
✔ Figure out why coercion happened
✔ Try two different PI_1 & PI_2
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define PI 3.1415926
int main() {
int a = 5, b = 2;
cout << "a / b = " << a / b << endl;
double x = 5.5;
// cout << "x % 2 = " << x % 2 << endl;
int angle = 90;
cout << "sin(90) = " << sin(angle) << endl;
// 沒有 srand(),rand() 每次結果一樣
cout << "Random number (1~100): " << rand() % 100 << endl;
double y = 2.5;
cout << "floor(2.5) = " << floor(y) << endl;
cout << "ceil(2.5) = " << ceil(y) << endl;
cout << "round(2.5) = " << round(y) << endl;
return 0;
}
EXERCISE 4-1: RANDOM NUMBER
Write a C++ program that reads two integers a and b from the user, and then
generates a random integer between them (inclusive).
1/3
National Yang Ming Chiao Tung University GECG10069 (561085) Laboratory Manual 02
Department of Electrical & Computer Engineering September 8, 2025
Computer Intelligence on Automation (C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
Requirements
Your program should work no matter which number is larger.
● If the user enters 5 10, the random number should be between 5 and 10.
● If the user enters 10 5, the random number should still be between 5 and 10.
Sample Input - 1
2 10
Sample Output - 1
EXERCISE 4-2: HEIGHT OF THE LONGEST SIDE
You’re asked to write a C++ program that calculates the height of the longest side of
a triangle.
Hero's Formula
● a, b, c = the lengths of the sides of a triangle
● A = the area of the triangle
● s = the semiperimeter of the triangle
2/3
National Yang Ming Chiao Tung University GECG10069 (561085) Laboratory Manual 02
Department of Electrical & Computer Engineering September 8, 2025
Computer Intelligence on Automation (C.I.A.) Lab Prof. Hung-Pin(Charles) Wen
Sample Input
3
4
5
Sample Output
Hint
● Find the area first, then the height.
● You may use max({a, b, c}) in C++11 or later to directly find the largest side.
3/3