1 CS 162 Introduction to Computer Science Chapter 5 ASCII to Integer Conversion Herbert G. Mayer, PSU Status 11/9/2014.

Slides:



Advertisements
Similar presentations
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
Advertisements

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 Lecture 7: Computer Arithmetic Today’s topics:  Chapter 2 wrap-up  Numerical representations  Addition and subtraction Reminder: Assignment 3 will.
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
CS150 Introduction to Computer Science 1
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Signed Numbers CS208. Signed Numbers Until now we've been concentrating on unsigned numbers. In real life we also need to be able represent signed numbers.
Number Systems Computer Science 210 Computer Organization.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
PSU CS 106 Computing Fundamentals II Bits, Binary Numbers & Binary Arithmetic HM 1/3/2009.
Simple Data Type Representation and conversion of numbers
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Computers Organization & Assembly Language
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Binary Arithmetic Stephen Boyd March 14, Two's Complement Most significant bit represents sign. 0 = positive 1 = negative Positive numbers behave.
1 Modified from  Modified from 1998 Morgan Kaufmann Publishers Chapter Three: Arithmetic for Computers citation and following credit line is included:
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014.
Number Systems Spring Semester 2013Programming and Data Structure1.
Chapter 8 Problems Prof. Sin-Min Lee Department of Mathematics and Computer Science.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
CSC 107 – Programming For Science. The Week’s Goal.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
Computer Math CPS120 Introduction to Computer Science Lecture 4.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
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=
Monday, January 14 Homework #1 is posted on the website Homework #1 is posted on the website Due before class, Jan. 16 Due before class, Jan. 16.
1 Lecture 7: MARS, Computer Arithmetic Today’s topics:  MARS intro  Numerical representations  Addition and subtraction.
1 CS 163 Data Structures Chapter 6 Function Calls and Recursion Herbert G. Mayer, PSU Status 6/1/2015.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic Herbert G. Mayer, PSU Status 1/14/2016 Copied with Permission from prof. Mark PSU ECE.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
William Stallings Computer Organization and Architecture 8th Edition
CHAPTER 5: Representing Numerical Data
Data Representation COE 308 Computer Architecture
Data Representation ICS 233
Lec 3: Data Representation
Chapter 4 C Program Control Part I
CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION
Tokens in C Keywords Identifiers Constants
Herbert G. Mayer, PSU CS status 7/29/2013
Bit Operations Horton pp
CHAPTER 4: Representing Integer Data
CS1010 Programming Methodology
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSC 370 (Blum)
Number Representation
Chapter 3: Input/Output
Differences between Java and C
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Homework Homework Continue Reading K&R Chapter 2 Questions?
Data Representation ICS 233
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Bit Operations Horton pp
Data Representation COE 308 Computer Architecture
Chapter 1 Introduction.
Presentation transcript:

1 CS 162 Introduction to Computer Science Chapter 5 ASCII to Integer Conversion Herbert G. Mayer, PSU Status 11/9/2014

2 Syllabus #include for getchar() #include for getchar() Convert Decimal Digits to int Convert Decimal Digits to int Account for Sign Account for Sign Check integer Overflow on 16-Bit Check integer Overflow on 16-Bit Maxint on 32-Bit Maxint on 32-Bit

3 Specify a2i() Conversion Reading one input char at a time, assuming the character is a decimal digit ‘0’..’9’ Reading one input char at a time, assuming the character is a decimal digit ‘0’..’9’ Convert this ASCII string of decimal digits to its corresponding integral value Convert this ASCII string of decimal digits to its corresponding integral value For example, the incoming string “123” is converted to the integer hundred-and-twenty-three, i.e For example, the incoming string “123” is converted to the integer hundred-and-twenty-three, i.e Be aware that this input is indeed a sequence of ASCII character, not an integer value Be aware that this input is indeed a sequence of ASCII character, not an integer value So we have to convert the digit ‘5’ for example to the integer value 5, by subtracting from character ‘5’ the value character ‘0’, and the difference is 5 So we have to convert the digit ‘5’ for example to the integer value 5, by subtracting from character ‘5’ the value character ‘0’, and the difference is 5 Int value of ‘5’ is: ‘5’ – ‘0’ Int value of ‘5’ is: ‘5’ – ‘0’

4 Implement a2i() Conversion int a2i( void ) { // a2i int number = 0; int number = 0; while( ( ( c = getchar() ) >= '0' ) && ( c = '0' ) && ( c <= '9' ) ) { number = number * 10 + ( c - '0' ); } //end while } //end while return number; return number; } //end a2i int main() { // main cout << “Enter a decimal number: ” << endl; cout << "The number was:” << a2i() << endl; return 0; } //end main

5 Discuss Code for a2i() Conversion The assumption in the code is that a sequence of decimal digits can be read The assumption in the code is that a sequence of decimal digits can be read However, if not a single digit is entered, the correct result of 0 is generated, due to initialization of number; see “int number = 0;” However, if not a single digit is entered, the correct result of 0 is generated, due to initialization of number; see “int number = 0;” A crucial test is: ( c = getchar() ) >= '0'... A crucial test is: ( c = getchar() ) >= '0'... Here a side-effect happens: A character is read from standard in, and it is tested. Test becomes more complex, but no further input happens Here a side-effect happens: A character is read from standard in, and it is tested. Test becomes more complex, but no further input happens The current number is shifted left by 1 decimal position (multiply by 10) and the digit value is added The current number is shifted left by 1 decimal position (multiply by 10) and the digit value is added

6 Account for Sign in a2i() Before reading one decimal digit at a time, check for optional negative sign ‘-’ or a redundant positive sign ‘+’ Before reading one decimal digit at a time, check for optional negative sign ‘-’ or a redundant positive sign ‘+’ Former ‘-’ will change the resulting value, by inverting the sign Former ‘-’ will change the resulting value, by inverting the sign State of ‘-’ has to be remembered; see variable neg State of ‘-’ has to be remembered; see variable neg The latter ‘+’ is silently skipped The latter ‘+’ is silently skipped Once the sign is handled, proceed as before with the decimal digits that make the number Once the sign is handled, proceed as before with the decimal digits that make the number

7 Implement Sign in a2i() int is_digit( char c ) { // is_digit return ( c >= '0' ) && ( c = '0' ) && ( c <= '9' ); } //end is_digit int a2i0( void ) { // a2i0 int number = 0; int number = 0; int neg = 0; int neg = 0; if ( c == '-' ) { if ( c == '-' ) { neg = 1; neg = 1; c = getchar(); c = getchar(); }else if ( c == '+' ) { }else if ( c == '+' ) { c = getchar(); c = getchar(); } //end if } //end if while ( is_digit( c ) ) { while ( is_digit( c ) ) { number = number * 10 + ( c - '0' ); c = getchar(); } //end while } //end while return neg ? -number : number; return neg ? -number : number; } //end a2i0

8 Discuss Sign in a2i State variable neg is solely used to determine at the end, whether sign inversion happens State variable neg is solely used to determine at the end, whether sign inversion happens Here we us a conditional expression: Here we us a conditional expression: return neg ? -number : number; For the initial state of neg = 0, the decimal computed so far is returned For the initial state of neg = 0, the decimal computed so far is returned But if neg is set to a non-zero state, the sign is inverted, all accomplished in a single conditional expression But if neg is set to a non-zero state, the sign is inverted, all accomplished in a single conditional expression Note separation of the 2 options via the : and ? operators! Note separation of the 2 options via the : and ? operators!

9 Specify Overflow in a2i() on 16-Bit Delicate programming matter, analyzed by sample of a fictitious 16-bit architecture Delicate programming matter, analyzed by sample of a fictitious 16-bit architecture On 16-bit computer the largest possible integer = On 16-bit computer the largest possible integer = On a two’s-complement architecture, the largest negative value = On a two’s-complement architecture, the largest negative value = Both cases have to be handled, without the overflow actually occurring, so we probe the growing integer value, before the next multiply by 10, MAX10 = 3276 Both cases have to be handled, without the overflow actually occurring, so we probe the growing integer value, before the next multiply by 10, MAX10 = 3276 Also track, whether scanning a negative int literal, in which case the largest next decimal digit for 3276 is 8, versus the positive literal, for which largest next decimal digit fir 3276 is 7 Also track, whether scanning a negative int literal, in which case the largest next decimal digit for 3276 is 8, versus the positive literal, for which largest next decimal digit fir 3276 is 7

10 Specify Overflow in a2i() on 16-Bit Scanning a positive decimal int literal: Scanning a positive decimal int literal: If number scanned so far is less than 3276, which is max / 10, then scanning 1 more digit is safe, do number = number * 10 + ( c-'0’ ); But if number scanned so far is in critical range 3276, then only some digits are safe, namely ‘0’..’7’ Else there will be positive overflow Scanning a negative decimal int literal: Scanning a negative decimal int literal: If number scanned so far is less than 3276, which is max / 10, then scanning 1 more digit is safe, do number = number * 10 + ( c-'0’ ); But if number scanned so far is in critical range 3276, then only some digits are safe, namely ‘0’..’8’ Else there will be negative overflow

11 Check for Overflow in a2i() #define MAX #define MAX // i.e. integer divide / 10 = int a2i1( void ) { // a2i int number = 0; int number = 0; int neg = 0; int neg = 0; if ( c == '-' ) { if ( c == '-' ) { neg = 1; neg = 1; c = getchar(); c = getchar(); }else if ( c == '+' ) { }else if ( c == '+' ) { c = getchar();// keep neg = 0 c = getchar();// keep neg = 0 } //end if } //end if while ( is_digit( c ) ) { while ( is_digit( c ) ) { if ( number > MAX10 ) { printf( "Overflow 1. max = %d\n", MAX ); printf( "Overflow 1. max = %d\n", MAX ); }else if ( number == MAX10 ) { if ( c == '9' ) { if ( c == '9' ) { printf( "Overflow 2. max = %d\n", MAX ); }else if ( c == '8' ) { }else if ( c == '8' ) { if ( neg ) { number = ; number = ;}else{ printf( "Overflow 3. max = %d\n", MAX ); printf( "Overflow 3. max = %d\n", MAX ); } //end if }else{ }else{ number = number * 10 + ( c - '0' ); } //end if } //end if}else{ number = number * 10 + ( c - '0' ); number = number * 10 + ( c - '0' ); } //end if } //end if c = getchar(); } //end while } //end while return neg ? -number : number; return neg ? -number : number; } //end a2i

12 Discuss Overflow in a2i The critical number = number * 10 + next digit is performed only in safe state: The critical number = number * 10 + next digit is performed only in safe state: For positive state, if the next digit after 3276 is ‘0’..’7’ one more iteration of the multiply is safe; yielding <= For positive state, if the next digit after 3276 is ‘0’..’7’ one more iteration of the multiply is safe; yielding <= For negative state, if the next digit after 3276 is ‘0’..’8’ one more iteration of the multiply is safe; yielding >= For negative state, if the next digit after 3276 is ‘0’..’8’ one more iteration of the multiply is safe; yielding >= Similarly on a 32-bit or today’s 64-bit computer; the scanner for integer literals needs to be safe, and detect integer overflow before it happens Similarly on a 32-bit or today’s 64-bit computer; the scanner for integer literals needs to be safe, and detect integer overflow before it happens Common to have computers ignore integer overflow, so it may be safe to simply experience it, but the numeric value has to be safely scanned Common to have computers ignore integer overflow, so it may be safe to simply experience it, but the numeric value has to be safely scanned

13 Maxint on 32-Bit #include #include main() { // main int expo;// shift 1 32 times int expo;// shift 1 32 times unsigned long int pow = 1;// multiply repeatedly by 2 unsigned long int pow = 1;// multiply repeatedly by 2 for ( expo = 1; expo <= 32; expo++ ) { for ( expo = 1; expo <= 32; expo++ ) { pow = pow * 2; pow = pow * 2; cout << " 2**" << expo << " = " << pow-1 << endl; cout << " 2**" << expo << " = " << pow-1 << endl; } // end for } // end for cout << << endl; cout << << endl; cout << << endl; cout << << endl; } //end main

14 Maxint Output 2**1 = 1 2**1 = 1 2**2 = 3 2**2 = 3 2**3 = 7 2**3 = **14 = **14 = **15 = **15 = **16 = **16 = **17 = **17 = **18 = **18 = **19 = **19 = **20 = **20 = **21 = **21 = **22 = **22 = **23 = **23 = **24 = **24 = **25 = **25 = **26 = **26 = **27 = **27 = **28 = **28 = **29 = **29 = **30 = **30 = **31 = **31 = **32 = **32 =