Faculty of Computers and Artificial Intelligence
Cairo University
CS111: Fundamentals of computer
science
Submitted to Dr. Mohammad El-Ramly
Assignment 2
Assignment Team Members:
-Mohamed Fouad Abdelmoniem -
ID:20240522
-Sondos Osama Mahmood -
ID:20240252
Part I – Flowcharts, Algorithms and Python:
Q1-
Q2: -
Q3: -
Q4: -
Q5: -
Q6: -
Q9: -
Yes, there is a difference between an algorithm and its
representation:
If an algorithm isn't represented in enough detail, it can lead to
confusion, errors, or inefficiencies because it's unclear how to
implement or understand it. Proper representation ensures that
the algorithm can be executed correctly, optimized, and
communicated effectively to others.
Q10: -
Using primitives in an algorithm removes
ambiguities of specification because the operations are standardized
and universally understood. Using primitives in a complex task breaks down
very intricate tasks into simple, well-defined steps. This brings clarity
and hence cannot be misinterpreted. It enhances debugging, and
consistency across all platforms, and translation to code is less
weighty. In this way, algorithms will be more exact, understandable, and
straightforward to implement, using only primitives.
Q11: -
Function Main
Declare Integer num
Assign num = 0
Input num
Do
(If num odd)
Print "num is odd"
Assign num = num+1
While
Num<50
End loop
End main
Q12: -
num = 100
while num > 0:
print(num)
num = num – 1
Q13: -
Q14: -
o S=9
o E=5
o N=6
o D=7
o M=1
o O=0
o R=8
o Y=2
Q15: -
The provided program segment has a few issues and logical gaps that need
to be addressed.
Incorrect use of else if:
In Python, the correct syntax for an "else if" statement is Elif, not else if.
So, the line else if (X % Y == 0): should be changed to Elif (X % Y == 0):
Corrected Version:
You should remove the conditional check if (X < Y) and simply check both
divisibility conditions for X and Y:
if (Y % X == 0):
print ('X is Divisor of Y')
Elif (X % Y == 0):
print ('Y is Divisor of X')
Q16: -
1. Start
2. input message
3. string encrypted message
4. For each character `char` in `message`:
a. If `char` is a letter:
i. Get the ASCII code of `char` using `Ord(char)`.
ii. If `char` is lowercase ('a' to 'z'):
- Calculate the new character by shifting to the next letter:
`new char = chr((Ord(char) - Ord('a') + 1) % 26 + Ord('a')) `
iii. If `char` is uppercase ('A' to 'Z'):
- Calculate the new character by shifting to the next letter:
`new char = chr((Ord(char) - Ord('A') + 1) % 26 + Ord('A')) `
iv. Append `new char` to `encrypted message`.
b. Else
- Append `char` unchanged to `encrypted message`.
5. Print an encrypted message
6. End
Q17: -
def to binary (input value):
Try:
Num = int (input value)
return bin(num) [2:]
except for Value Error:
binary’s = ‘. join (format (Ord(char), '08b') for char in input value)
return binary’s
user input = input ("Enter a number or a string to convert to binary: ")
print ("Binary representation:", to binary (user input))
Q18: -
Part II – Data Representation:
Q19: -
Floating Point Calculations:
a)
o Binary: 01011000
o Sign Bit: 0 (positive number)
o Exponent: 101 = 5
o Calculation: + 2^ (5-4) * 0.1000 = (1 )2 = , which converts to 1
in decimal.
b)
o Binary: 10101110
o Sign Bit: 1 (negative number)
o Exponent: 010 = 2
o Calculation: -2^ (2-4) * 0.1110 = - (0.00111)2, which converts -
0.21875 in decimal.
c)
o Binary: 11001111
o Sign Bit: 1 (negative number)
o Exponent: 100 = 4
o Calculation: 2^ (4-4) * 0.1111 = - (0.1111)2, which converts to
- 0.9375 in decimal.
Q20: -
a)
o Binary: 10111110101010000101000111101011
o Sign Bit: 1 (negative)
o Exponent: 01111101 (125 in decimal) → 125−127=−2125 - 127
= -2125−127=−2
o Mantissa: 1.01010000101000111101011
o Calculation: 2^ (125-127) * 1.01100001010001111010111= -
(0.01011000000000011111010111)2 = (-0.345) d
b)
o Binary: 01000010101111110000000000000000
o Sign Bit: 0 (positive)
o Exponent: 10000101 (133 in decimal) → 133−127=6133 - 127
= 6133−127=6
o Mantissa: 1.0111111
o Calculation: 2^ (132-127) * 1d.11111110000000000000000 =
(111111.110000000000000000)2 = (63.75) d
Q21: -
Explanation of Unexpected Result:
o The unexpected result of -2 is likely due to Excel’s internal
handling of division by zero in floating-point arithmetic, possibly
because of truncation or rounding behavior. Instead of producing
a #DIV/0! Error as expected, Excel might return an
approximation or a default value, like -2, because of internal
calculations involving floating-point representation or error
handling.
Q22: -
a)
o Binary Addition: 0011 (3) + 1010 (10) = 1101 (13)
o Overflow: No, the result fits within the 4-bit range
b)
o Binary Addition: 1101 (13) + 0100 (4) = 0001 (1)
o Overflow: Yes, the result does not fit within the 4-bit range
c)
o Binary Addition: 1001 (9) + 1100 (12) = 0101 (incorrect,
expected 10101)
o Overflow: Yes, the result does not fit within the 4-bit range
d)
o Binary Addition: 0100 (4) + 0100 (4) = 1000 (8)
o Overflow: No, the result fits within the 4-bit range
e)
o Binary Addition: 1011 (11) + 1100 (12) = 0111 (incorrect,
expected 10111)
o Overflow: Yes, the result does not fit within the 4-bit range
f)
o Binary Addition: 0100 (4) + 0101 (5) = 1001 (9)
o Overflow: No, the result fits within the 4-bit range
Q23: -
ASCII Table Message Decoding:
a)
o Decoding the ASCII values reveals the message "Computer
Science."
b)
o Decoding the ASCII values reveals the message "I Love
Computers."
c)
a) Sequence: 01001001 01110011 00100000 00110001 00100000
01100010 01111001 01110100 01100101 00100000 00111101
00100000 00111000 00100000 01100010 01101001 01110100
01110011 00111111
o Decodes to the message: "Is 1 byte = 8 bits?"
o 01011001 01100101 01110011 00101100 00100000 01100001
00100000 01100010 01111001 01110100 01100101 00100000
01100011 01101111 01101110 01110100 01100001 01101001
01101110 01110011 00100000 00111000 00100000 01100010
01101001 01110100 01110011 00100001
o Decodes to the message: " Yes, a byte contains 8 bits!"
Q24: -
a)
The development of the Unicode standard was imperative because
ASCII was limited in size with its 128 characters that could only
accommodate English letters and common symbols. Unicode on the other
hand, has the capacity of more than 1.1 million characters effectively
accommodating every language on the planet. ASCII character set utilizes a
static 1-byte fixed character size, thus, it becomes impractical for languages
other than English, whereas Unicode uses different character encodings such
as UTF-8 and UTF-16 which are of variable length, thereby saving space.
Furthermore, Unicode is constructed to be upward compatible with ASCII
which means this writing system is capable of supporting legacy as well as
contemporary multi-lingual texts without any difficulties.
b)
Encoding Name: محمد فؤاد عبدالمنعم فؤاد محمد
1) Unicode
o 0645 062D 0645 062F 0641 0624 0627 062F 0639 0628 062F
0627 0644 0645 0646 0639 0645 0641 0624 0627 062F 0645
062D 0645 062F
o Resource: https//[Link]/Unicode-converter
2) UTF-16 Encoding:
o Each Unicode code point is represented using 2 bytes (for Arabic
characters)
o Example: U+0645 ( → )م0x0645
o U+3000U+3600U+3400U+3500
U+3000U+3600U+3200U+4400
U+3000U+3600U+3400U+3500
U+3000U+3600U+3200U+4600
U+3000U+3600U+3400U+3100
U+3000U+3600U+3200U+3400
U+3000U+3600U+3200U+3700
U+3000U+3600U+3200U+4600
U+3000U+3600U+3300U+3900
U+3000U+3600U+3200U+3800
U+3000U+3600U+3200U+4600
U+3000U+3600U+3200U+3700
U+3000U+3600U+3400U+3400
U+3000U+3600U+3400U+3500
U+3000U+3600U+3400U+3600
U+3000U+3600U+3300U+3900
U+3000U+3600U+3400U+3500
U+3000U+3600U+3400U+3100
U+3000U+3600U+3200U+3400
U+3000U+3600U+3200U+3700
U+3000U+3600U+3200U+4600
U+3000U+3600U+3400U+3500
U+3000U+3600U+3200U+4400
U+3000U+3600U+3400U+3500
U+3000U+3600U+3200U+4600
3) UTF-16 Big Endian Encoding:
o Similar to UTF-16, but the byte order is reversed
o U+0030U+0036U+0034U+0035
U+0030U+0036U+0032U+0044
U+0030U+0036U+0034U+0035
U+0030U+0036U+0032U+0046
U+0030U+0036U+0034U+0031
U+0030U+0036U+0032U+0034
U+0030U+0036U+0032U+0037
U+0030U+0036U+0032U+0046
U+0030U+0036U+0033U+0039
U+0030U+0036U+0032U+0038
U+0030U+0036U+0032U+0046
U+0030U+0036U+0032U+0037
U+0030U+0036U+0034U+0034
U+0030U+0036U+0034U+0035
U+0030U+0036U+0034U+0036
U+0030U+0036U+0033U+0039
U+0030U+0036U+0034U+0035
U+0030U+0036U+0034U+0031
U+0030U+0036U+0032U+0034
U+0030U+0036U+0032U+0037
U+0030U+0036U+0032U+0046
U+0030U+0036U+0034U+0035
U+0030U+0036U+0032U+0044
U+0030U+0036U+0034U+0035
U+0030U+0036U+0032U+0046
4) UTF-8 Encoding:
o Each Unicode code point is represented using 1 to 4 bytes.
o Example: U+0645 ( → )م0xD9 0x85
o 0645 062D 0645 062F 0641 0624 0627 0627 0639 0628 062F
0627 0644 0645 0646 0639 0645 0641 0624 0627 062F 0645
062D 0645 062F
Q25: -
Floating Point Representation:
o 1)To represent -2.25:
o Decimal: 2.25 = 10.01 in binary
o Representation: 2^ (3 + 1) * 1.001
o Exponent Adjustment: 4 + 1 = 5, with exponent 101
o Final Representation: Sign bit as 1 (negative), exponent
101, mantissa 0010, corresponding to representation
(d).
o 2) 6.25 cannot be accurately represented in this format
because it equals 0110 1001, which does not exist
within this encoding system. (a)
Q26: -
o A. LZW → 5: An example of adaptive dictionary encoding
o B. flip-flop → 9: A digital circuit capable of holding a single digit
o C. JPEG → 8: A lossy compression technique for images
o D. VLSI → 6: A means of constructing complex electronic
circuits in a very small space
o E. Unicode → 4: A means to represent the world’s languages
using 16 bits or more
o F. MIDI → 1: A means of encoding music in terms of notes and
instruments rather than actual audio
Q27: -
Hexadecimal and Encoding Conversions:
o Binary to Hexadecimal: 1111 1110 1111 1011 translates to
hexadecimal FEFB. (e)
o Signed Two’s Complement: This converts to -261. (a)
o Excess Notation Interpretation: In this notation, the value
would represent a positive number. (d)
o UTF-16 Representation: Encodes as UTF-16 big Endian. (b)
o UTF-8 Representation: Requires Two bytes to encode. (b)
Part III-Python:
Q28: -
def swap pairs(A):
i=0
while i < len(A) - 1:
# Swap A[i] with A[i+1]
A[i], A[i+1] = A[i+1], A[i]
i += 2 # Move to the next pair
return A
# Test the function with the given input
A = [1, 2, 3, 4, 5, 6]
result = swap pairs(A)
print(result)
Q29: -
def find_factors_optimized(n):
factors = set ()
for i in range (1, int([Link](n)) + 1):
if n % i == 0:
factors. Add(i)
factors. Add (n // i) # Add both divisors
return sorted(factors)
# Example usage:
number = 12
factors = find_factors_optimized(number)
print (f"Factors of {number} are: {factors}")
Q30: -
n = int (input("Enter the number of terms: "))
sign = 1
sum = 0
i=0
while i < n:
term = sign / (2 * i + 1)
sum = sum + term
sign = sign * -1
i=i+1
p = sum * 4
print ("Approximation of Pi:", p)