Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be.

Slides:



Advertisements
Similar presentations
Data Representation COE 202 Digital Logic Design Dr. Aiman El-Maleh
Advertisements

Division Algorithms By: Jessica Nastasi.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
Data Representation Computer Organization &
Data Representation COE 205
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
2-1 Computer Organization Part Fixed Point Numbers Using only two digits of precision for signed base 10 numbers, the range (interval between lowest.
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Data Representation ICS 233
Bell Work Explain why the location of point A(1, -2) is different than the location of point B(-2, 1). **Answer in complete thought sentences.
Professor Jennifer Rexford COS 217
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Fractions and Decimals
Number Systems Computer Science 210 Computer Organization.
Binary and Hexadecimal Numbers
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Simple Data Type Representation and conversion of numbers
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Numbering Systems CS208.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Computer Science 111 Fundamentals of Programming I Number Systems.
IT253: Computer Organization
Whole Number Operations Addition Rules - Line up numbers by place value - Number with most digits should be on top - Order doesn't matter - Can add more.
Signed Rationals. Place Value Let’s look at position after the decimal to help us do some rounding!
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
When we add or subtract integers we can use a number line to help us see what is happening with the numbers.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
Exponents & Scientific Notation MATH 102 Contemporary Math S. Rook.
Chapter 2.1 Rational Numbers and Chapter 2.2 Adding and Subtracting Rational Numbers.
Data Representation – Chapter 3 Section 3-1. Terminology “Digital” –Discrete, well defined values/steps –Opposite of analog –Analogy: digital is to analog.
Introduction to Integers On the number line the Numbers have a greater value As you go to the right. -6 –5 –4 –3 –2 – Review.
Negative Bit Representation Lesson CS1313 Spring Negative Bit Representation Outline 1.Negative Bit Representation Outline 2.Negative Integers 3.Representing.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Engineering Notation: Similar to scientific notation. Similar to scientific notation. Exponent is evenly divisible by 3 Exponent is evenly divisible by.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Cougar Time. Adding Negative Numbers  What are the two rules for adding integers?  Same Signs = Add and keep the sign  Different Signs = Find the absolute.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Introduction to Microprocessors Chapter 2. Decimal or Base 10 Numbers  Have ten different digits (0-9)  It is a weighted number system. Each position.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
CEC 220 Digital Circuit Design Binary Arithmetic & Negative Numbers Monday, January 13 CEC 220 Digital Circuit Design Slide 1 of 14.
Adding, Subtracting, Multiplying, and Diving Integers!!!
CEC 220 Digital Circuit Design Binary Arithmetic & Negative Numbers Fri, Aug 28 CEC 220 Digital Circuit Design Slide 1 of 14.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Objectives The student will be able to: 1. State the coordinate of a point on a number line. 2. Graph integers on a number line. 3. Add and subtract integers.
Data Representation COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum.
Data Representation COE 308 Computer Architecture
Data Representation ICS 233
Data Representation.
Chapter 4 Operations on Bits.
3.1 Denary, Binary and Hexadecimal Number Systems
Fractions and Decimals
Fundamentals of Programming I Number Systems
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings.
Digital Electronics and Microprocessors
Fractions and Decimals
CPS120: Introduction to Computer Science
Chapter Four Data Representation in Computers By Bezawit E.
Data Representation ICS 233
Fractions and Decimals
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Data Representation COE 308 Computer Architecture
Presentation transcript:

Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be either positive or negative. 1)TASK: Output minus sign if necessary 1)IF: (x LT 0) 1)OUT: ‘-’ 2)x = -x 2)TASK: Output an unsigned integer

Screen output goes from left to right, so much identify which digits to output in that order and output them to the screen one at a time. First, make sure we can output a single digit to the screen. Unsigned Integer Output 1)TASK: Output single digit (assume i < 10) 1)PUT: i + ‘0’ int PutD(int i) { PutC( i + ‘0’ ); }

Give v = 1234, how do we figure out we need to print a ‘1’ first? Weighting of largest digit 1)TASK: Determine the weighting of the largest digit. SET: weight = 1 1)WHILE: v > 10 * weight ( > or >= ??? ) 1)weight = 10 * weight Watch out for overflow! 10*weight might not be representable.... but v/10 and v/weight are!

Weighting of largest digit 1)TASK: Determine how many digits there are. 1)SET: weight = 1 2)WHILE: weight <= ( v / 10 ) (integer division okay?) 1)weight = 10 * weight for (weight = 1; weight <= v / 10; weight *= 10) /* EMPTY LOOP */;

Does it work? for (weight = 1; weight <= x / 10; weight *= 10) /* EMPTY LOOP */; if x is 1234, then weight will end up being 1000: weight weight < (1234)/10 ? 1T 10T 100T 1000F

What is the largest digit? for (digit = 0; v >= weight; digit++) v - = weight; If the largest digit is N, then we can only subtract off weight N times before we end up with a negative number. So count the number of times we can subtract N. 1)TASK: Determine the left-most digit. 1)SET: digit = 0 2)WHILE: v > weight ( > or >= ??? ) 1)v = v - weight 2)digit = digit + 1

Does it work? if x is 3234, then weight will be 1000: v > weight ? vdigit T22341 T12342 T F for (digit = 0; v >= weight; digit++) v - = weight;

How to get the next digit? After putting out one digit (we know how to do that!) then we can simply repeat the process with the weighting reduced by a factor of 10. We stop after we have printed out the units digit (when weight = 1) so after dividing by ten we will have zero. 1)WHILE: weight > 0 ( > or >= ??? ) 1)TASK: Determine leftmost digit (see prior slides) 2)TASK: Print leftmost digit (see prior slides) 3)SET: weight = weight / 10

Bringing it all together - top level PRINTING INTEGERS: 1)TASK: Print sign and take absolute value. 2)TASK: Print out digits in |v| 1)TASK: Find weight of leftmost digit. 2)WHILE: weight > 0 1)TASK: Determine leftmost digit 2)TASK: Print leftmost digit 3)SET: weight = weight / 10

Bringing it all together - expanded 1)TASK: Print sign and take absolute value. 1)IF: (v) LT (0) 1)OUT: ‘-’ 2)SET: v = -v 2)TASK: Print out |v| 1)Find weight of leftmost digit 1)SET: weight = 1 2)WHILE: (v / 10) GE (weight) 1)SET: weight = weight * 10 2)WHILE: (weight) GT (0) 1)TASK: Determine leftmost digit 1)SET: digit = 0 2)WHILE: (v) GE (weight) 1)SET: v = v / 10 2)SET: digit = digit + 1 2)TASK: Print leftmost digit (see prior slides) 1)PutD(digit) 3)SET: weight = weight / 10 /* TASK: Print sign, v = |v| if ( v < 0 ) {PutC(‘-’); v = -v; } /* TASK: Print out |v| */ weight = 1; while ( (v / 10) >= weight ) weight *= 10; while (weight > 0) { digit = 0; while (v >= weight) { v /= 10; digit ++; }PutD(digit); weight /= 10; }

Using for() loops to tidy up the code /* TASK: Print sign, v = |v| if ( v < 0 ) {PutC(‘-’); v = -v; } /* TASK: Print out |v| */ weight = 1; while ( (v / 10) >= weight ) weight *= 10; while (weight > 0) { digit = 0; while (v >= weight) { v /= 10; digit ++; }PutD(digit); weight /= 10; } /* TASK: Print sign, v = |v| if ( v < 0 ) {PutC(‘-’); v = -v; } /* TASK: Print out |v| */ for ( weight = 1; (v / 10) >= weight ; weight *= 10 ) /* EMPTY LOOP */; while (weight > 0) { for (digit = 0; v >= weight ; digit ++ ) v /= 10; PutD(digit); weight /= 10; }

The final function also counts characters int Put_i( int v ) { int weight; int count, digit; count = 0; /* TASK: Print sign, v = |v| if ( v < 0 ) {PutC(‘-’);count++; v = -v; } /* TASK: Print out |v| */ for ( weight = 1; (v / 10) >= weight ; weight *= 10 ) /* EMPTY LOOP */; while (weight > 0) { for (digit = 0; v >= weight ; digit ++ ) v /= 10; PutD(digit);count++; weight /= 10; } return count; }