CS111 - Lecture 9 - Fall 2023 2024
CS111 - Lecture 9 - Fall 2023 2024
INTRODUCTION TO CS
3
Unsigned integer means that the number may be positive
or zero only
Signed integer means that the number may be positive, or
negative, or zero also
If we don’t write the type it means that it’s signed
If we define the variable x as:
unsigned char x ; then the range for the variable x is
0 < x < 255 ( it contains numbers equal to 28 -1= 255)
main()
{
char x;
for(x=-127 ; x<127; x++)
printf(" %d ",x);
}
it will prints the numbers from -127 to 126 but if we write
for(x=-127 ; x<=127; x++)
We will have an infinite loop; to terminate the loop it must reach the
value 128 and it can’t be done
4
Note that the compiler don’t send an error message
main()
{
unsigned char x;
for(x=0;x<=255;x++)
printf(" %d ",x);
}
Appendix B
5
printf( "The character (%c) has the value %d\n", 'a', 'a' );
The character (a) has the value 97
TYPE MODIFIERS
(a) i + n
(b) i / n
(c) x + n
(d) x* y
(e) n *y
(f) (float) y
(g) (double) y
(r) (float) i / n
COMPOUND INTEREST CALCULATIONS
Consider:
A person invests $1000.00 in a savings account
yielding 5% interest. Assuming that all interest is
left on deposit in the account, calculate and print
the amount of money in the account at the end of
each year for 10 years. Use the following formula
for determining these amounts:
a = p(1 + r)n
Section 4.6
where
p is the original amount invested (i.e., the principal)
r is the annual interest rate
n is the number of years
a is the amount on deposit at the end of the nth year.
14
COMPOUND INTEREST CALCULATIONS
(CONT.)
function prototype:
unsigned int year;
double pow (double, double)
Section 4.6
Implicitly
converted to double
15
RECURSION
16
RECURSION
In C, a function that calls itself directly or
indirectly is called recursive function.
Recursion is useful for problems that can be
represented by a simpler version of the same
problem.
Example: the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!
24
THANKS
25
COUNTING SYSTEMS
COUNTING SYSTEM
There are three kinds of people in the world:
those who can count, and those who can not.
- Unknown Wisdom
BASE 10 COUNTING SYSTEM
We happened to use the
current counting system,
because we happened to have
ten fingers.
If dinosaurs had ruled the
earth, they would be happy
to use a 8-based counting
system.
NUMBERS
Ancient Africa
Notches on a bone.
Egyptians/Roman
Each magnitude is represented by a symbol.
Indian/Arabian (Modern numbering system)
1,475,268
BASE 10 (DECIMAL NUMBERS)
Base 2
1011 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20
1011 = 1 x 8 + 0 x 4 + 1 x 2 + 1 x 1
= 11 in decimal system
WHAT ARE BINARY NUMBERS?
Decimal system uses ten symbols to
represent numeric information
0 1 2 3 4 5 6 7 8 9
Each position within a number represents
10 raised to a power
Binary system uses two symbols to
represent numeric information
0 1
Each position within a number represents
2 raised to a power
Binary numbers can be converted to
decimal numbers and vice versa
USING BINARY NUMBERS
Binary numbers can be used for mathematical
operations
101 + 110 = 1011
0 1
On Off
True False
Yes No
Open Closed
BINARY BITS AND BYTES
0+0=0
1+0=1
1+1=10
BINARY FRACTIONS
Just as we multiply each place to the left of the
decimal by 2 when we’re working with integers,
we divide by 2 for each place to the right of the
decimal
0 0 0 1 1 1.0 1 1
0 0 0 1 1 1.0 1 1
= 4 + 2 + 1 + ¼ + 1/8
=7.375
ASSIGNMENT #1
Convert the following numbers into decimal:
10001010.1010
00000011.0110
ANSWERS
138.625
3.375
CONVERTING DECIMAL FRACTIONS INTO
BINARY
Convert the part to the left of the decimal as
before.
Take the fraction and multiply it by two.
The whole number part is the value you enter.
Discard the whole number part
Repeat the preceding steps until you have the
number of digits you need.
AN EXAMPLE
13.625 into binary (4 binary decimals)
13 = 00001101
0.625 * 2 = 1.25
0.25 * 2 = 0.5
0.5 * 2 = 1.0
0 * 2 = 0.0
13.625 = 00001101.1010
ASSIGNMENT #2
Convert the following decimal fractions into
binary (4 decimal places)
12.5
15.875
ANSWER
1100.1000
1111.1110
HEXADECIMAL (BASE 16)
DECIMAL TO OCTAL
CONVERSION OF 12510 TO OCTAL
8 125
8 15 - 5
1 -7
12510=1758
4
175
5X80 = 5
7X81 = 56
1X82 = 64
125
1758 = 12510
QUIZ
No Calculators!!!!
Convert binary code to Decimal number.
10100101 (Bin)
Convert Decimal number to binary code
176 (Dec)
Convert Hexadecimal number to binary
BADDEF
Add these two binary numbers
10001101+11011100=?
ANSWER
10100101 (Binary) = 165 (Decimal)
176 (Decimal)= 10110000 (Binary)
BADDEF=1011,1010,1101,1101,1110,1111