Chapter 5 (Part 1) - Practice
Q1: Population Density
- اعمل قاعدة بيانات للسكان والمساحة للدول دي:
| Country | Population (in million people) | Area (in millions of square miles) |
|---|---|---|
| Usa | 203 | 3 |
| India | 548 | 1 |
| China | 800 | 4 |
| Brazil | 108 | 3 |
-
وبعدين اعمل rule:
-
density is population divided by area.
-
واكتب الاستعلامات:
- Find the population of India.
- Find the area of Brazil.
- Find the population density of China.
Solution (Population Density)
Q2: Arithmetic Functions
- اعمل rules للدوال الرياضية دي:
- Sum: S = X + Y
- Calculate: y = (x + 3)²
- Polynomial: F = X⁴ + 2X + 5
- واكتب الاستعلامات:
- Find the sum of 5 and 4.
- Find y when x = 2 using calculate.
- Find F when X = 3 using the polynomial.
Solution (Arithmetic Functions)
Q3: Factorial
اعمل recursive rule لحساب factorial:
- Base: factorial of 1 is 1.
- Inductive: factorial of N is N × factorial of N-1.
واكتب الاستعلام:
- Find the factorial of 5.
Solution (Factorial)
Q4: GCD
- اعمل recursive rule لحساب greatest common divisor (GCD):
- If X and Y are equal, D = X.
- If X < Y, D = gcd(X, Y - X).
- If Y < X, D = gcd(Y, X).
- واكتب الاستعلامات:
- Find the GCD of 12 and 8.
- Find the GCD of 100 and 35.
Solution (GCD)
Q5: Logic Gates
- اعمل Facts للبوابات المنطقية الأساسية (not, and, or, xor):
| Inputs | and | or | xor |
|---|---|---|---|
| 0, 0 | 0 | 0 | 0 |
| 0, 1 | 0 | 1 | 1 |
| 1, 0 | 0 | 1 | 1 |
| 1, 1 | 1 | 1 | 0 |
| Inputs | not |
|---|---|
| 0 | 1 |
| 1 | 0 |
Solution (Logic Gates)
Q6: Half Adder
-
استخدم "and" و "xor" من Q5 عشان تبني half-adder:
-
Carry = A and B
-
Sum = A xor B
-
وجرب inputs (0,1) و (1,1).
Solution (Half Adder)
Q7: Boolean Function
-
استخدم "not", "and", "or" من Q5 عشان تعمل rule للدالة:
-
F = B' + AC
-
يعني: not B, then and(A, C), then or the results.
-
وجرب inputs:
-
A=1, B=0, C=1
-
A=1, B=1, C=1
-
A=0, B=1, C=1
-
A=0, B=0, C=0
Solution (Boolean Function)
Q8: Summation 1 to X
-
اعمل recursive rule تجمع الأرقام من 1 لأي رقم X:
-
F = 1 + 2 + 3 + ... + X
-
واكتب الاستعلامات:
-
Sum from 1 to 5.
-
Sum from 1 to 10.
-
Sum from 1 to 100.
Solution (Summation 1 to X)
Q9: Summation of Odd Numbers 1 to M
-
اعمل recursive rule تجمع الأرقام الفردية (odd) من 1 لأي رقم فردي M:
-
لو M زوجي: تخطاه
- Base: sum_odd(1, 1).
واكتب الاستعلامات:
- Sum of odd numbers from 1 to 5.
- Sum of odd numbers from 1 to 7.
- Sum of odd numbers from 1 to 10.
Solution (Summation of Odd Numbers)