CS1001 Programming Fundamentals 3(3-0) Lecture 2

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 2.
Advertisements

1 CS101 Introduction to Computing Lecture 17 Algorithms II.
Chapter 1 - An Introduction to Computers and Problem Solving
Chapter 2 - Problem Solving
Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK
Chapter 2 - Problem Solving
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Algorithm Design CS105. Problem Solving Algorithm: set of unambiguous instructions to solve a problem – Breaking down a problem into a set of sub- problems.
Chapter Chapter Goals Know the different types of numbers Describe positional notation.
Programming Fundamentals (750113) Ch1. Problem Solving
Number Systems and Arithmetic
CS105 INTRODUCTION TO COMPUTER CONCEPTS BINARY VALUES & NUMBER SYSTEMS Instructor: Cuong (Charlie) Pham.
EKT 121 / 4 ELEKTRONIK DIGIT 1 CHAPTER 1 : INTRODUCTION.
How Computers Work Dr. John P. Abraham Professor UTPA.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
Number systems, Operations, and Codes
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '081 Chapter 2 - Problem Solving 2.1 Program Development Cycle 2.2 Programming Tools.
Concepts of Algorithms CSC-244 Unit Zero Pseudo code, Flowchart and Algorithm Master Prince Computer College Qassim University K.S.A.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Program Program is a collection of instructions that will perform some task.
How Computers Solve Problems Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step.
 Problem Analysis  Coding  Debugging  Testing.
PROGRAMMING: What’s It All About?
Number Systems & Binary Arithmetic
Unit 1 Introduction Number Systems and Conversion.
Lecturer: Santokh Singh
Introduction To Number Systems
Digital Logic and Computer Organization
Number Systems and Binary Arithmetic
Integer Real Numbers Character Boolean Memory Address CPU Data Types
CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION
CHAPTER 1 : INTRODUCTION
Introduction to Computing
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Algorithms and Flowcharts
Introduction The term digital is derived from the way computers perform operation, by counting digits. Application of digital technology: television, communication.
Pseudocode Upsorn Praphamontripong CS 1110 Introduction to Programming
CSE 102 Introduction to Computer Engineering
Chapter 3 Data Representation
The Selection Structure
Lecture 2 Introduction to Programming
Number System conversions
Number Systems.
March 2006 Saeid Nooshabadi
University of Gujrat Department of Computer Science
Algorithm and Ambiguity
ALGORITHMS AND FLOWCHARTS
Number Systems and Binary Arithmetic
Data Representation in Computer Systems
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Teaching Computing to GCSE
ALGORITHMS AND FLOWCHARTS
Digital Logic Design (ECEg3141) 2. Number systems, operations & codes 1.
Introduction to Algorithms and Programming
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
CPS120: Introduction to Computer Science
Algorithm and Ambiguity
Understanding Problems and how to Solve them by using Computers
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
ICT Gaming Lesson 2.
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 3: Selection Structures: Making Decisions
Basic Concepts of Algorithm
COMS 161 Introduction to Computing
Presentation transcript:

CS1001 Programming Fundamentals 3(3-0) Lecture 2 Binary Arithmetic and Over View of Software Development Lecture: Narong Wesnarat

Binary Arithmetic The individual digits of a binary number are referred to as bits Each bit represents a power of two Examples 01011 = 0 • 24 + 1 • 23 + 0 • 22 + 1 • 21 + 1 • 20 = 11 00010 = 0 • 24 + 0 • 23 + 0 • 22 + 1 • 21 + 0 • 20 = 2 00010 + 01011 01101 Binary addition 2 + 11 13 Equivalent decimal addition

Binary Arithmetic 0101 Binary Equivalent decimal multiplication × 0011 0000 0001111 5 × 3 15

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

Two's Complement Example Performing two's complement on the decimal 7 to get -7 Using a five-bit representation 7 = 00111 Convert to binary 11000 Complement the bits 11000 Add 1 to the complement + 00001 11001 Is -7 in two's complement (Answer)

Two's Complement Arithmetic Computing 8 - 7 using a two's complement representation with five-bit numbers In base 10 (Decimal System) 8 - 7 = 8 + (-7) = 1 Using Two’s Complement Arithmetic 01000 Two's complement of 8 11001 Two's complement of -7 01000 Add 8 and -7 + 11001 100001 00001 Is the five-bit result Throw away the high-order carry as we are using a five bit representation

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

Hexadecimal Constants Letters represent the hex digits a or A - 10 d or D - 13 b or B - 11 e or E - 14 c or C - 12 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

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

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 = 1.609 kilometers

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 1.609 times the distance in miles

1.5 Applying the software Development Method IMPLEMENTATION Figure 1.14 Miles-to-Kilometers Conversion Program

Basic Software Design Tools Simple program design tools Flowchart Pseuso-code Visit a link for complete Flowchart Symbols at the instructor’s Web site

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

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.

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

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

Introduction Pseudo-Code Case statement (multiple decision) CASE condition is : value-1 : statement another statement value-2: statement value-3: statement ENDCASE

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.

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)

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!”

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

  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  

Flowchart & Pseudo-code comparison Read N I = 0 REPEAT Print I I = I + 1 UNTIL I = N