Download presentation
Presentation is loading. Please wait.
Published byMeredith Grace Davis Modified over 6 years ago
1
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Binary Arithmetic and Over View of Software Development Lecture: Narong Wesnarat
2
Binary Arithmetic The individual digits of a binary number are referred to as bits Each bit represents a power of two Examples = 0 • • • • • 20 = 11 = 0 • • • • • 20 = 2 00010 01101 Binary addition 2 + 11 13 Equivalent decimal addition
3
Binary Arithmetic 0101 Binary Equivalent decimal multiplication
× 0011 0000 5 × 3 15
4
Two’s Complement Convention for handling signed numbers in binary representation The leading bit is a sign bit Binary number with leading 0 is positive Binary number with leading 1 is negative Magnitude of positive numbers is just the binary representation Magnitude of negative numbers is found by performing the two’s complement Complement the bits Replace all the 1's with 0's, and all the 0's with 1's Add one to the complemented number Carry in most significant bit position is thrown away when performing arithmetic
5
Two's Complement Example
Performing two's complement on the decimal 7 to get -7 Using a five-bit representation 7 = Convert to binary Complement the bits Add 1 to the complement Is -7 in two's complement (Answer)
6
Two's Complement Arithmetic
Computing using a two's complement representation with five-bit numbers In base 10 (Decimal System) = 8 + (-7) = 1 Using Two’s Complement Arithmetic Two's complement of 8 Two's complement of -7 Add 8 and -7 100001 Is the five-bit result Throw away the high-order carry as we are using a five bit representation
7
Integer Constants Integer constants are positive or negative whole numbers Integer constant forms Decimal (base 10) Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Octal (base 8) Digits 0, 1, 2, 3, 4, 5, 6, 7 Hexadecimal (base 16) Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a , b, c, d, e, f Consider 31 oct and 25 dec
8
Hexadecimal Constants
Letters represent the hex digits a or A d or D - 13 b or B e or E - 14 c or C f or F - 15 Examples (in C language) 0x2C 0XAC12EL The type of the constant depends on its size, unless the type specifier is used
9
1.4 The Software Development Method (Hanly’s Text book)
Specify the problem requirements Analyse the problem Design the algorithm to solve the problem Implement the algorithm Test and verify the completed program Maintain and update the program
10
1.5 Applying the software Development Method
CASE STUDY Converting Miles to Kilometers Problem Convert distances from miles into kilometers. Analysis DATA REQUIREMENTS Problem Inputs: miles /* the distance in miles */ Problem Output: kms /*the distance in kilomieters*/ Relevant Formula: 1 mile = kilometers
11
1.5 Applying the software Development Method
Design ALGORITHM Get the distance in miles Convert the distance to kilometers Display the distance in kilometers Altorithm with Refinements 2.1 The distance in kilometers is times the distance in miles
12
1.5 Applying the software Development Method
IMPLEMENTATION Figure Miles-to-Kilometers Conversion Program
13
Basic Software Design Tools
Simple program design tools Flowchart Pseuso-code Visit a link for complete Flowchart Symbols at the instructor’s Web site
14
Flowchart Analyzing the Flowchart (Checking your algorithm)
Suppose we Enter N = 3, The process of Printing 1 -3 will be: Step 1 2 3 4 5 6 N I Printed on Paper 12 123 Decision No Yes Stop
15
Introduction Pseudo-Code
Pseudo-code is a way to express an algorithm ... ... using a structured form of language ... ... that is (hopefully) unambiguous ... ... and resembles a programming language. There are no rules ... ... but there might be standards.
16
Introduction Pseudo-Code
The Sequence (instructions in series) some statement another statement If-Then-Else statement (decision) If some condition is TRUE Then some statements Else another statement Else (maybe) no statement
17
Introduction Pseudo-Code
While-Do statement (loop) WHILE the condition is TRUE do some statement another statement ENDWHILE or WEND Repeat-Until statement (loop) REPEAT UNTIL the condition is TRUE
18
Introduction Pseudo-Code
Case statement (multiple decision) CASE condition is : value-1 : statement another statement value-2: statement value-3: statement ENDCASE
19
Introduction Pseudo-Code
(more) Keywords: Read , Write used for input description (also: Input, Ouput) true , false used to state a logical result Add , Subtract Multiply , Divide used for calculations AND , OR , NOT used for combined Conditions Hints and Tips: In principal there are 3 Pseudocode structures: Sequence, Loop and Decision. With those three, you can describe any Algorithm (solution to a problem), using Pseudo-code.
20
Introduction Pseudo-Code
Some examples If-Then_Else (decision) Problem: Read one number and check if it is positive! Pseudo-Code: Read number If number > zero Then Write “Number is Positive!” Else Write “Number is Negative!” This simple algorithm will read one Number as Input (from the keyboard if nothing else is specified), will check if that Number is Positive or not and will print the result (on the Monitor, if nothing else is specified)
21
Introduction Pseudo-Code
The solution above will not solve all cases though! That’s because the number could also be zero, and then you cannot tell whether it is positive or negative! So we improve our solution to the problem by changing the PSEUDOCODE to: Read number If number > zero Then Write “Number is Positive!” Else If number = zero Then Write “Number is Zero!” Else Write “Number is Negative!”
22
Introduction Pseudo-Code
Suppose we would like to use the CASE-statement here, then it would look like following: Read Number CASE Number is : > zero : Write “Number is Positive!” = Zero : Write “Number is zero!” < Zero : Write “Number is Negative!” ENDCASE
23
Structured Flow chart Flowchart should be structured
These two flowchart gives the same result but the right one is better. Flowchart should be structured easier to implement in high level language easier to find errors Use only standard structures sequence IF… THEN … ELSE … REPEAT…. UNTIL ….. WHILE …. DO ….. ….. WEND
24
Flowchart & Pseudo-code comparison
Read N I = 0 REPEAT Print I I = I + 1 UNTIL I = N
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.