Algonquin College Computer Studies CST 8110 Introduction to

  • Slides: 71
Download presentation
Algonquin College Computer Studies CST 8110 Introduction to Computing Ian D. Allen Rideau B

Algonquin College Computer Studies CST 8110 Introduction to Computing Ian D. Allen Rideau B 215 A alleni@algonquinc. on. ca 2/19/2021 12: 28 PM 2/19/2021 1

Welcome to Algonquin College • Focused on Your Career • Customized Training • Workplace

Welcome to Algonquin College • Focused on Your Career • Customized Training • Workplace Your Name Skills • Quality Instruction 2 2/19/2021 12: 28 PM 2/19/2021

Instructor Ian D. Allen • B. A. Honours Psychology University of Waterloo 1980 •

Instructor Ian D. Allen • B. A. Honours Psychology University of Waterloo 1980 • MMath Computer Science University of Waterloo 1985 3 2/19/2021 12: 28 PM 2/19/2021

Contact Information • Ian D. Allen • • alleni@algonquinc. on. ca Rideau B 215

Contact Information • Ian D. Allen • • alleni@algonquinc. on. ca Rideau B 215 A Telephone (727 -4723) x 5949 Office hours – see my office door • Notes and Course Outlines • online: http: //www. algonquinc. on. ca/~alleni/ http: //www. algonquinc. on. ca/cst/8110/ • in library, if needed 4 2/19/2021 12: 28 PM 2/19/2021

Things You Should Know • Your Student Number • marks are grouped by number

Things You Should Know • Your Student Number • marks are grouped by number • Your Course Number • CST 8110 section 010 - Ian Allen • Course Times • Lectures: two hours / week – 010 Mon 14: 00 -14: 50 RB 163 – Fri 11: 00 -11: 50 RB 163 • Labs: two hours / week – 011 Wed 14: 00 -15: 50 RC 205 – 012 Thu 11: 00 -12: 50 RC 204 – 013 Thu 14: 00 -15: 50 RC 205 5 2/19/2021 12: 28 PM 2/19/2021

CST 8110 Course Outline • course outlines are available online • please review academic

CST 8110 Course Outline • course outlines are available online • please review academic policies • withdrawing • repeating courses • probation • academic discipline 6 2/19/2021 12: 28 PM 2/19/2021

Course Learning Requirements • Lectures • 2 hours Class • 2 hours Lab (mandatory)

Course Learning Requirements • Lectures • 2 hours Class • 2 hours Lab (mandatory) • Evaluation / Earning Credit • 35% in-class tests & quizzes • 40% final examination • 25% assignments • Assignment Late Penalty • up to one week. . . • after one week. . . 7 2/19/2021 12: 28 PM – 20% – 100% 2/19/2021

Marking Scheme A: 80 -100 An outstanding level of achievement has been consistently demonstrated.

Marking Scheme A: 80 -100 An outstanding level of achievement has been consistently demonstrated. B: 70 -79 Achievement has exceeded the required level. C: 60 -69 Course requirements are met. D: 50 -59 Achievement is at a marginal level. Consistent, ongoing effort is required for continuing success in the program. 8 2/19/2021 12: 28 PM 2/19/2021

CST 8110 Major Academic Events • Midterm #1 • in class • Monday, June

CST 8110 Major Academic Events • Midterm #1 • in class • Monday, June 2, 1997 • Midterm #2 • in class • Friday, July 4, 1997 • Statutory Holidays • three • Final Exam Period • August 18 - 22 (inclusive) 9 2/19/2021 12: 28 PM 2/19/2021

How to Succeed in this Course • Do the assignments • understand theory •

How to Succeed in this Course • Do the assignments • understand theory • apply theory • practice • Attend Lectures and Labs • remember your diskettes – back up your diskettes! • be on time • ask questions • Learn the tools • memorization isn’t enough 10 2/19/2021 12: 28 PM 2/19/2021

Lab and Lecture Dialogue Protocol • There are no dumb questions • ask •

Lab and Lecture Dialogue Protocol • There are no dumb questions • ask • Stop me if I go too fast • assist me in knowing your learning styles • Do your homework • use class time to ask questions 11 2/19/2021 12: 29 PM 2/19/2021

Lab and Lecture Dialogue Protocol • Be here • Lab attendance is mandatory •

Lab and Lecture Dialogue Protocol • Be here • Lab attendance is mandatory • Be on time • use class time well • Listen • to me • to questions (and answers!) • Focus • don’t distract me or others • don’t distract yourself 12 2/19/2021 12: 29 PM 2/19/2021

Required for Laboratories • Diskettes • • 3 ½ inch high density (HD) disks

Required for Laboratories • Diskettes • • 3 ½ inch high density (HD) disks for assignments spare disks for back-up • You • sign in • please be on time • attendance is mandatory – missed labs result in no course credit: see the course outline 13 2/19/2021 12: 29 PM 2/19/2021

CST 8110 Assignment 0 • Generate your accounts • you must do this for

CST 8110 Assignment 0 • Generate your accounts • you must do this for e-mail • see the Student Monitors • Labs: C 204, C 205, C 207, +? • Choose a good password • you can remember it • other people can’t guess it • Use your account! • ask for assistance • find the course outline • send your neighbour some e-mail 14 2/19/2021 12: 29 PM 2/19/2021

How a Computer Works • Addressable Memory • composed of binary digits: bits •

How a Computer Works • Addressable Memory • composed of binary digits: bits • bits grouped by 8 into bytes • each byte has an address • bytes grouped into words • Central Processing Unit (CPU) • reads bytes and words from memory • operates on the bits (adds, shifts, etc. ) based on the numeric instruction code also read from memory • writes bytes and words into memory • Input and Output (I/O) • printers, terminals, modems, etc. 15 2/19/2021 12: 29 PM 2/19/2021

Computer Programs • are simply numbers stored in the main memory of the computer

Computer Programs • are simply numbers stored in the main memory of the computer • the numbers have special meaning when read by the Central Processing Unit (CPU) • the CPU reads the numbers as instruction codes that tell it to perform some actions • the action taken by the CPU upon reading a numeric instruction code from memory depends on the type of the CPU, e. g. PC, Macintosh, Atari 16 2/19/2021 12: 29 PM 2/19/2021

Problem Solving and Stepwise Refinement • Text: Section 1. 5 - 1. 6 •

Problem Solving and Stepwise Refinement • Text: Section 1. 5 - 1. 6 • Blue Book: Stepwise Refinement (p. 12 -24) • Problem Solving: • Define the problem • Determine the Outputs • Determine the Inputs • Derive the Algorithm • Stepwise Refinement of an Algorithm: • Steps are followed in order • Each stepwise refinement is indented • Each indentation explains how to do the higher-level step above it • No more than half a dozen steps at any level of indentation • Don’t focus on detail and refinement until all the higher-level steps are understood 17 2/19/2021 12: 29 PM 2/19/2021

Problem: Feed Me Get Food • • • Obtain Money – go outside and

Problem: Feed Me Get Food • • • Obtain Money – go outside and unlock bicycle – pedal over to bank machine and get cash Go Shopping – pedal to local market with cash – purchase macaroni and cheese dinner Come home – put cheese dinner ingredients in bicycle bag – cycle home (lock bike; go inside with food…) Prepare Food • • • Prepare Water – add salt to water – heat water on stove until boiling Prepare Macaroni – place macaroni in boiling water – wait until cooked just right – drain macaroni Add Magic Cheese Sauce – mix magic cheese powder into macaroni – mix butter and milk into macaroni Serve Food • • • Set table for one Turn on nice dinner music Light candles Place pot of food on dining room table Put some food from pot onto plate Eat Food • • • 18 Place fork into delicious food Lift fork with food into face Repeat until food is gone or face is full 2/19/2021 12: 29 PM 2/19/2021

Algonquin Pseudocode Basics • Blue Book: p. 10 -11, • • 19 Appendix A

Algonquin Pseudocode Basics • Blue Book: p. 10 -11, • • 19 Appendix A Use pseudocode to express the details of your Algorithms Pseudocode is English text combined with key programming words Less strict than programming languages More strict than English 2/19/2021 12: 29 PM 2/19/2021

Algonquin Pseudocode Rules • Pseudocode should be well • • • structured and pleasing

Algonquin Pseudocode Rules • Pseudocode should be well • • • structured and pleasing to the eye. Each statement must be concise. Omit words such as “the” and “and”. Start each line with a capital letter. Use lower case English with upper case programming keywords. Keywords are used for: • input and output: GET, PUT • decisions: IF, ELSE • looping: FOR, WHILE • comparison: EQUAL, LESS 20 2/19/2021 12: 29 PM 2/19/2021

Algonquin Pseudocode Keywords • Input: • GET • Output: • PUT • Decisions: •

Algonquin Pseudocode Keywords • Input: • GET • Output: • PUT • Decisions: • IF … ELSE … ENDIF • Looping: • FOR … ENDFOR • WHILE … ENDWHILE • DO … WHILE • Proper indentation must be used for each type of statement. • Indent three spaces for all compound statements and all statements within loops. 21 2/19/2021 12: 29 PM 2/19/2021

C Language Basics • Text: Chapter 2 22 2/19/2021 12: 29 PM 2/19/2021

C Language Basics • Text: Chapter 2 22 2/19/2021 12: 29 PM 2/19/2021

Number Systems • Natural Numbers 1, 2, 3, . . . • Whole Numbers

Number Systems • Natural Numbers 1, 2, 3, . . . • Whole Numbers 0, 1, 2, 3, . . . • Integers … , -2, -1, 0, 1, 2, … • Rational Numbers 22/7, 1/3, etc. • Irrational Numbers PI=3. 14159265358979323… 23 2/19/2021 12: 29 PM 2/19/2021

Positional Notation Base 10 • 4, 295 base 10 4 2 9 5 x

Positional Notation Base 10 • 4, 295 base 10 4 2 9 5 x x 10 10 to to the the power 3 2 1 0 = = 4, 000 + 200 + 90 + 5 -----as a base 10 number = 4, 295 • 3. 1415 base 10 3 1 4 1 5 24 x x x 3. 0 +. 1 +. 04 +. 001 +. 0005 ------as a base 10 number = 3. 1415 2/19/2021 12: 29 PM 10 10 10 to to to the the the power power 0 -1 -2 -3 -4 = = = 2/19/2021

Base 8 Octal • 7162 base 8 7 1 6 2 x x 8

Base 8 Octal • 7162 base 8 7 1 6 2 x x 8 8 to to the the power 3 2 1 0 = = 3, 584 + 64 + 48 + 2 -----as a base 10 number = 3, 698 • 2. 1730 base 8 2 1 7 3 0 25 x x x 2. 0 +. 125 +. 109375 +. 0058593 +. 0000000 -----as a base 10 number = 2. 2402343 2/19/2021 12: 29 PM 8 8 8 to to to the the the power power 0 -1 -2 -3 -4 = = = 2/19/2021

More Digits for Base 16 - Hexadecimal Base 2 digits: 0, 1 Base 8

More Digits for Base 16 - Hexadecimal Base 2 digits: 0, 1 Base 8 digits: 0, 1, 2, …, 6, 7 Base 10 digits: 0, 1, 2, …, 8, 9 Base 16 digits: 0, 1, 2, …, 8, 9, A, B, …, E, F A is a digit with value 10 B is a digit with value 11 C is a digit with value 12 D is a digit with value 13 E is a digit with value 14 F is a digit with value 15 F + 1 = 10 base 16 D + E = 1 B base 16 C - 7 = 5 base 16 F - E = 1 base 16 26 2/19/2021 12: 29 PM 2/19/2021

Base 16 Hexadecimal • 719 F base 16 7 1 9 F x x

Base 16 Hexadecimal • 719 F base 16 7 1 9 F x x 16 16 to to the the power 3 2 1 0 = = 28, 672 + 256 + 144 + 15 ------as a base 10 number = 29, 087 • F. 1 C 3 base 16 F 1 C 3 27 x x power 0 = 15. 0 power -1 = +. 0625 power -2 = +. 046875 power -3 = +. 000732 -----as a base 10 number = 15. 110107 2/19/2021 12: 29 PM 16 16 to to the the 2/19/2021

Any Base to Base 10 • Conversion of a whole number in any base

Any Base to Base 10 • Conversion of a whole number in any base to decimal (base 10): • Multiply each digit in the number by the base raised to the power corresponding to the position of the digit in the number • The rightmost digit is multiplied by the base raised to the power zero, next digit is multiplied by the base raised to the power 1, etc. • Sum the resulting products 28 2/19/2021 12: 29 PM 2/19/2021

Base 10 to Any Base • Left-to-Right: Start dividing by the largest power of

Base 10 to Any Base • Left-to-Right: Start dividing by the largest power of the base that is less than or equal to the value; continue with all the lesser powers of the base in order. Write down the quotient digits from top-tobottom and left-to-right. • Right-to-Left: Keep dividing by the base until the quotient is zero. Write down the remainder digits in order from top-to -bottom and right-to-left. 29 2/19/2021 12: 29 PM 2/19/2021

Base 10 to Binary: Left-to-Right Start dividing by the largest power of the base

Base 10 to Binary: Left-to-Right Start dividing by the largest power of the base that is less than or equal to the value; continue with all the lesser powers of the base in order: 1933 base 1933 div 909 div 397 div 141 div 13 div 5 div 10 to 2**10 2**9 2**8 2**7 2**6 2**5 2**4 2**3 2**2 2**1 1**0 binary (base 2): = 1 rem 909 = 1 rem 397 = 1 rem 141 = 1 rem 13 = 0 rem 13 = 1 rem 5 = 1 rem 1 = 0 rem 1 = 1 rem 0 = 11110001101 binary ( top-to-bottom and left-to-right ) 30 2/19/2021 12: 29 PM 2/19/2021

Base 10 to Hex: Left-to-Right Start dividing by the largest power of the base

Base 10 to Hex: Left-to-Right Start dividing by the largest power of the base that is less than or equal to the value; continue with all the lesser powers of the base in order: 1933 base 10 to hex: 1933 div 16 **2 = 141 div 16 **1 = 7 rem 141 8 rem 13 13 div 16 **0 = 13 rem 0 = 78 D hex (base 16) ( top-to-bottom and left-to-right ) • Note that 13 is written as the digit ‘D’ in hexadecimal notation. 31 2/19/2021 12: 29 PM 2/19/2021

Base 10 to Binary: Right-to-Left Keep dividing by the base until zero; write all

Base 10 to Binary: Right-to-Left Keep dividing by the base until zero; write all the remainder digits down from right-to-left: 1933 base 1933 div 966 div 483 div 241 div 120 div 60 div 30 div 15 div 7 div 3 div 10 to binary (base 2): 2 = 966 rem 1 2 = 483 rem 0 2 = 241 rem 1 2 = 120 rem 1 2 = 60 rem 0 2 = 30 rem 0 2 = 15 rem 0 2 = 7 rem 1 2 = 3 rem 1 2 = 1 rem 1 2 = 0 rem 1 = 11110001101 binary ( top-to-bottom and right-to-left ) 32 2/19/2021 12: 29 PM 2/19/2021

Base 10 to Hex: Right-to-Left • Keep dividing by the base until zero; write

Base 10 to Hex: Right-to-Left • Keep dividing by the base until zero; write all the remainder digits down in order from top-to-bottom and right-to-left: • 1933 base 10 to hex: 1933 div 16 = 120 rem 13 120 div 16 = 7 rem 8 7 div 16 = 0 rem 7 = 78 D hex (base 16) ( top-to-bottom and right-to-left ) • Note that 13 is written as the digit ‘D’ in hexadecimal notation. 33 2/19/2021 12: 29 PM 2/19/2021

Between Binary, Octal, and Hexadecimal • Bases 2, 8, and 16 are easily related

Between Binary, Octal, and Hexadecimal • Bases 2, 8, and 16 are easily related • Binary: Consider a two-byte value: 10110100 11000110 • Octal: group/ungroup by 8’s (3 bits) 1 010 011 000 110 1 3 2 3 0 6 => 132306 base 8 • Hex: group/ungroup by 16’s (4 bits) 1011 0100 1100 0110 B 4 C 6 => B 4 C 6 base 16 • 1323068 = B 4 C 616 = 46, 27810 34 2/19/2021 12: 29 PM 2/19/2021

Conversion Summary Sheet • Decimal to other bases • For positive or unsigned numbers:

Conversion Summary Sheet • Decimal to other bases • For positive or unsigned numbers: • Use the right-to-left or left-to-right dividing-by-the base method • Other bases to decimal • For positive or unsigned numbers: • Multiply each digit by its corresponding power of the base • Add up the products • Among binary, octal, and hex • For all bit patterns, whether signed or unsigned, just group and ungroup bits: • Octal: group/ungroup by 3 bits • Hex: group/ungroup by 4 bits 35 2/19/2021 12: 29 PM 2/19/2021

Short Cuts in Number Systems • Note that 0111111 base 2 is one less

Short Cuts in Number Systems • Note that 0111111 base 2 is one less than 1000000 base 2 • Note that 0777 base 8 is one less than 1000 base 8 • Note that 0999 base 10 is one less than 1000 base 10 • Note that 0 FFF base 16 is one less than 1000 base 16 36 2/19/2021 12: 29 PM 2/19/2021

Numbers to Characters 7 -Bit ASCII • American Standard Code for • • •

Numbers to Characters 7 -Bit ASCII • American Standard Code for • • • 37 Information Interchange An interpretation of 7 -bit numbers 7 bits means 128 values (characters) the value of ' ' (space) = 32 Upper-case: 'A' = 65 'Z' = 90 Lower-case: 'a' = 97 'z' = 122 ASCII characters are 7 -bit numbers: • 'A' + ' ' = 'a' (= 97) • 'B' + ' ' = 'b' (= 98) • 'a' + 2 = 'c' (= 99) 2/19/2021 12: 29 PM 2/19/2021

Unsigned 8 -bit Integers • Consider one-byte (8 -bit) integers: 2**8 = 256 possible

Unsigned 8 -bit Integers • Consider one-byte (8 -bit) integers: 2**8 = 256 possible numbers: 0000 0016 0 0001 0116 1 0000 0010 0216 2 0000 0011 0316 3. . . 0111 1110 7 E 16 126 0111 1111 7 F 16 127 1000 0000 8016 128 1000 0001 8116 129 1000 0010 8216 130 1000 0011 8316 131. . . 1111 1101 FD 16 253 1111 1110 FE 16 254 1111 FF 16 255 38 2/19/2021 12: 29 PM 2/19/2021

Signed (+/-) 8 -bit Integers • Consider one-byte (8 -bit) integers: + 39 2**8

Signed (+/-) 8 -bit Integers • Consider one-byte (8 -bit) integers: + 39 2**8 = 256 possible numbers: 0000 0016 0 0001 0116 1 128 0000 0010 0216 2 positive 0000 0011 0316 3 numbers. . . 0111 1110 7 E 16 126 0111 1111 7 F 16 127 1000 0000 8016 -128 1000 0001 8116 -127 1000 0010 8216 -126 128 1000 0011 8316 -125 negative. . . numbers 1111 1101 FD 16 -3 1111 1110 FE 16 -2 1111 FF 16 -1 2/19/2021 12: 29 PM 2/19/2021

Signed (+/-) 16 -bit Integers • Consider two-byte (16 -bit) integers: + 40 2**16

Signed (+/-) 16 -bit Integers • Consider two-byte (16 -bit) integers: + 40 2**16 = 65, 536 possible numbers: 000016 0 000116 1 000216 2 32, 768 positive 000316 3 numbers. . . 7 FFE 16 32, 766 7 FFF 16 32, 767 800016 -32, 768 800116 -32, 767 800216 -32, 766. . . 32, 768 negative numbers FFFC 16 -4 FFFD 16 -3 FFFE 16 -2 FFFF 16 -1 2/19/2021 12: 29 PM 2/19/2021

Observations on Signed Integers • Since half the bit values are used for negative

Observations on Signed Integers • Since half the bit values are used for negative numbers, signed integers have half the positive range of unsigned integers • 16 bit unsigned: 0 … 65, 535 • 16 bit signed: -32, 768 … 0 … +32, 767 • The most positive value and most negative value are adjacent bit patterns • adding one to the most positive value generates the bit pattern for the most negative value (“integer overflow”) • the bit pattern for the most positive value is the complement of the bit pattern for the most negative value: 7 FFF and 8000, 7 FFFFFFF and 80000000 • -1 is always “all bits turned on” • 8 bits: FF • 16 bits: FFFF • 32 bits: FFFF • adding 1 to -1 turns all bits off: zero 41 2/19/2021 12: 29 PM 2/19/2021

Observations on Signed Integers II • The same bit pattern may be interpreted as

Observations on Signed Integers II • The same bit pattern may be interpreted as either signed or unsigned. We say N is a signed integer if, when we interpret N, we interpret all bit patterns with the sign bit set as negative numbers. • If N is a signed integer, then its bit pattern is to be read as signed, and the leftmost (top) bit is known as the sign bit. If N is signed and the sign bit is set, the bit pattern will be interpreted as a negative number. • Note: If the sign bit is zero, the bit pattern has the same numeric value whether interpreted as signed or unsigned. • If the number is a signed number, zero is positive (the sign bit is not set). 42 2/19/2021 12: 29 PM 2/19/2021

Operations on Signed Integers • Negating signed binary, octal, hex • invert all the

Operations on Signed Integers • Negating signed binary, octal, hex • invert all the bits: 1 => 0, 0 => 1 (same as subtracting from -1) • then add 1 • i. e. -N = ((-1) - N) + 1 • e. g. to negate C 0 F 6 (negative): = ((-1) - C 0 F 6) + 1 = (FFFF - C 0 F 6) + 1 = 3 F 09 + 1 = 3 F 0 A (positive) • Converting negative decimal numbers • convert the positive equivalent • then negate it (using the above method) • e. g. to convert decimal -16, 138 = -(16, 138) - make positive = -(3 F 0 A) - convert = C 0 F 6 - negate 43 2/19/2021 12: 29 PM 2/19/2021

Operations on Signed Integers II • Numbers with the sign bit set • Only

Operations on Signed Integers II • Numbers with the sign bit set • Only signed numbers can be negative, and negative only if the sign bit is set e. g. C 0 F 6, 80 A 7, FFFF • To convert signed, negative numbers from binary, octal, and hex to decimal • negate the number (make positive) • then convert to decimal • then change the sign • e. g. to convert signed C 0 F 6 (negative) = -(3 F 0 A) - negate = -(16, 138) - convert = -16, 138 - change sign • e. g. to convert signed 1010 = -(01010110) - negate = -(86) - convert = -86 - change sign 44 2/19/2021 12: 29 PM 2/19/2021

Conversion of Signed Integers • From signed, negative bit patterns to negative decimal numbers

Conversion of Signed Integers • From signed, negative bit patterns to negative decimal numbers • negate the negative bit pattern to make it a positive bit pattern • convert the positive bit pattern to a positive decimal number • change the sign on the positive decimal number to make it negative again • From negative decimal numbers to signed, negative bit patterns • change the sign on the negative decimal number to make it positive • convert the positive decimal number to a positive bit pattern • negate the positive bit pattern to make it a negative bit pattern again 45 2/19/2021 12: 29 PM 2/19/2021

Floating-Point Numbers I • see “Scientific Notation” p. 5 in the Algonquin CST 8110

Floating-Point Numbers I • see “Scientific Notation” p. 5 in the Algonquin CST 8110 Blue Book • Floating-point numbers are stored as separate mantissa (fraction) and exponent. 31415926 E+1 • Limits on the size of the mantissa and exponent limit the precision (accuracy) and range of floating-point numbers • precision: # bits used in mantissa 1. 234567 E 1 1. 234567890123456789 E 1 • range: # bits used in exponent 1. 0 E 38 1. 0 E 300 46 2/19/2021 12: 29 PM 2/19/2021

Floating-Point Numbers II • Arithmetic Overflow: Some real numbers are “too big” • to

Floating-Point Numbers II • Arithmetic Overflow: Some real numbers are “too big” • to represent in a given number of bits of storage: • e. g. 1. 0 E+500 Arithmetic Underflow: Some real numbers are “too small” to represent using a fixed number of bits: • e. g. 1. 0 E-500 • Numbers nearer to zero are closer together than numbers with larger exponents: • because the mantissa multiplies the exponent; • successive numbers in the mantissa multiply the larger exponents, and the numbers space farther Cancellation Error: Adding a relatively small number to a large one may not change the large one if the difference in magnitude is greater than the precision: • e. g. 1. 0 E 30 + 1. 0 E 1 = 1. 0 E 30 • the number with the smaller exponent has no effect if the magnitude of the two numbers differs by more than the number of digits of precision • the mantissa may not have enough precision to reflect the addition of such a relatively small number 47 2/19/2021 12: 29 PM 2/19/2021

Floating Point Limitations • Binary values between 0 and 1 are sums of products

Floating Point Limitations • Binary values between 0 and 1 are sums of products of negative powers of two; they do not always accurately express sums of products of negative powers of ten. • Similar problem: 1/3 cannot be exactly expressed as a finite decimal number: 0. 333333333… • 1/10 (0. 1 decimal) cannot ever be accurately represented in base 2, 8, or 16: 0. 00011001100… base 2 0. 0631631… base 8 0. 199999… base 16 • a sum of ten binary “tenths” will not be exactly 1 due to this representation error 48 2/19/2021 12: 29 PM 2/19/2021

Using Floating Point • Be aware of round-off error occurring due to the limited

Using Floating Point • Be aware of round-off error occurring due to the limited number of bits available for each of the mantissa and exponent. • Make sure you pick a storage type with enough bits for precision and range to accurately represent the numbers you use. • Note that a four-byte integer has more bits of precision than a four-byte floating-point number. A four-byte integer has less range than a four-byte floating point #. • Never compare floating-point numbers for exact equality; test for “close enough” against a “reasonably” small value: • absolute_value(f 1 - f 2) < epsilon 49 2/19/2021 12: 29 PM 2/19/2021

Floating Point Implementations • For general use, the C language on most computers defines

Floating Point Implementations • For general use, the C language on most computers defines a floating-point storage type, 4 bytes long, that handles approximately 6 -7 decimal digits of mantissa and an exponent between -38 and +38 • The C language double floating-point storage type, 8 bytes long, handles approximately 17 -18 decimal digits of mantissa and an exponent between -308 and +308 • Remember: Bits are bits. The same bit pattern may be a signed or unsigned integer, a character, or a floating-point number, depending on how it is used. 50 2/19/2021 12: 29 PM 2/19/2021

Floating Point Imprecision main() { int i; float x; double y; printf("n. Floats: n");

Floating Point Imprecision main() { int i; float x; double y; printf("n. Floats: n"); for( i=0; i <= 9; i++ ){ x = 1. 0 + (i / 10. 0); printf("Expect 1. %d actual is %. 42 fn", i, x); } printf("n. Doubles: n"); for( i=0; i <= 9; i++ ){ y = 1. 0 + (i / 10. 0); printf("Expect 1. %d actual is %. 42 fn", i, y); } } 51 2/19/2021 12: 29 PM 2/19/2021

Output of Imprecise Floats 52 Floats: Expect 1. 0 Expect 1. 1 Expect 1.

Output of Imprecise Floats 52 Floats: Expect 1. 0 Expect 1. 1 Expect 1. 2 Expect 1. 3 Expect 1. 4 Expect 1. 5 Expect 1. 6 Expect 1. 7 Expect 1. 8 Expect 1. 9 - actual actual actual is is is 1. 000000000000000000000 1. 100000023841857910156250000000000 1. 20000004768371582031250000000000 1. 29999995231628417968750000000000 1. 399999976158142089843750000000000 1. 5000000000000000000000 1. 600000023841857910156250000000000 1. 70000004768371582031250000000000 1. 79999995231628417968750000000000 1. 899999976158142089843750000000000 Doubles: Expect 1. 0 Expect 1. 1 Expect 1. 2 Expect 1. 3 Expect 1. 4 Expect 1. 5 Expect 1. 6 Expect 1. 7 Expect 1. 8 Expect 1. 9 - actual actual actual is is is 1. 000000000000000000000 1. 10000000088817841970012523233890533 1. 19999999955591079014993738383054733 1. 30000000044408920985006261616945267 1. 39999999911182158029987476766109467 1. 5000000000000000000000 1. 60000000088817841970012523233890533 1. 69999999955591079014993738383054733 1. 80000000044408920985006261616945267 1. 89999999911182158029987476766109467 2/19/2021 12: 29 PM 2/19/2021

The simple IF statement makes a yes/no decision Any time execution encounters the simple

The simple IF statement makes a yes/no decision Any time execution encounters the simple IF statement, a choice is made whether or not to execute the enclosed set of statements: IF control_expression Statement 1 Statement 2 … ENDIF • If control_expression tests true, execute the set of statements between the IF and the ENDIF. • If control_expression tests false, skip all the statements and continue execution with whatever comes after the ENDIF. • IF is not a loop. When the simple IF statement is encountered, the set of enclosed statements is only executed once, or not at all. 53 2/19/2021 12: 29 PM 2/19/2021

Examples of the simple IF statement in C language #define LOW_LIMIT 0 if (

Examples of the simple IF statement in C language #define LOW_LIMIT 0 if ( num < LOW_LIMIT ) { printf(“Error: %d is smaller than %dn”, num, LOW_LIMIT); printf(“Resetting number to %d. n”, LOW_LIMIT); num = LOW_LIMIT; } #define MAXDAYS 10 if ( days > MAXDAYS ) { printf(“Warning: %d days > %dn”, days, MAXDAYS); } #define T_LETTER ‘T’ if ( letter != T_LETTER ) { printf(“You typed %c instead of %cn”, letter, T_LETTER); } 54 2/19/2021 12: 29 PM 2/19/2021

Why C has a compound IF statement Consider the following pair of of double-if

Why C has a compound IF statement Consider the following pair of of double-if statements, with the first IF being done if the control_expression is TRUE, and the second one being done if the expression is FALSE (NOT TRUE): IF control_expression A first set of one or more statements … ENDIF IF ! control_expression A second set of one or more statements … ENDIF this set if true this set if false This pairing happens often enough that C allows the above two IF statements to be written as one IF/ELSE statement: IF control_expression A first set of one or more statements … ELSE A second set of one or more statements … ENDIF this set if true this set if false The meaning is identical in both cases. The IF/ELSE version is more efficient, since it only has to test the condition once. If the condition is TRUE, the first set of statements is executed. If the condition is FALSE, the second set is executed instead. 55 2/19/2021 12: 29 PM 2/19/2021

The compound IF statement gives two choices (once) Any time execution encounters a compound

The compound IF statement gives two choices (once) Any time execution encounters a compound IF statement, one of two sets of statements are chosen: IF control_expression A first set of one or more statements … this set if ELSE true A second set of one or more statements … this set if ENDIF false • If control_expression tests true, execute the first set of statements, once. • If control_expression tests false, execute the second set of statements, once. • When this style IF statement is encountered, one or the other of the sets of statements is done. • Never both sets of statements; never no set • Always one set or the other 56 2/19/2021 12: 29 PM 2/19/2021

Examples of the compound IF statement in C language evencount = oddcount = 0;

Examples of the compound IF statement in C language evencount = oddcount = 0; if ( (num % 2) == 0 ) { printf(“%d is an even number. n”, num); ++evencount; } else { printf(“%d is an odd number. n”, num); ++oddcount; } #define FEB_NOLEAP_DAYS 28 #define FEB_LEAP_DAYS (FEB_NOLEAP_DAYS+1) if ( is. Leap. Year(year) ) { feb. Days = FEB_LEAP_DAYS; } else { feb. Days = FEB_NOLEAP_DAYS; } printf(“February has %d days in %dn”, feb. Days, year); 57 2/19/2021 12: 29 PM 2/19/2021

The nested IF statement A nested IF statement is an IF statement inside an

The nested IF statement A nested IF statement is an IF statement inside an IF statement. The nesting might occur in either part of the IF, and the nested IF may or may not itself be a compound IF statement. Nesting can go to any level. IF control_expression_1 IF control_expression_2 A nested set of one or more statements … ENDIF ELSE IF control_expression_3 A nested set of one or more statements … ELSE A nested set of one or more statements … ENDIF 58 2/19/2021 12: 29 PM 2/19/2021

Examples of the nested IF statement in C language evencount = oddcount = 0;

Examples of the nested IF statement in C language evencount = oddcount = 0; if ( (num % 2) == 0 ) { printf(“%d is an even number. n”, num); ++evencount; if( (num % 4) == 0 ) printf(“It is divisible by 4n”); if( num == 0 ) { printf(“It is zero. n”); printf(“That isn’t interesting. n”); } } else { printf(“%d is an odd number. n”, num); ++oddcount; if( (num % 9) == 0 ) printf(“It is divisible by 9n”); else printf(“It is not divisible by 9n”); } 59 2/19/2021 12: 29 PM 2/19/2021

The Decision Table IF statement A decision table IF statement selects among more than

The Decision Table IF statement A decision table IF statement selects among more than two alternatives. This form of IF statement avoids deep nesting. Usually, each control_expression tests the same variable against different constant values. IF control_expression_1 A first set of one or more statements … ELSE IF control_expression_2 A second set of one or more statements … ELSE IF control_expression_3 A third set of one or more statements … ELSE A final set of one or more statements … ENDIF 60 2/19/2021 12: 29 PM 2/19/2021

Example of the Decision Table IF statement in C language Note the use of

Example of the Decision Table IF statement in C language Note the use of brace brackets {} to enclose multiple statements, where needed. The order of the tests is very important. Testing for “less than” must proceed from smallest to largest. Testing for “greater than” must proceed from largest to smallest. void noise_print(int noise_db) { if( noise_db <= 50 ) printf(“quietn”); else if ( noise_db <= 70 ) printf(“intrusiven”); else if ( noise_db <= 90 ) printf(“annoyingn”); else if ( noise_db <= 110 ) printf(“very annoyingn”); else { printf(“n. Noise level %d “, noise_db); printf(“is uncomfortably loud!n”); } } 61 2/19/2021 12: 29 PM 2/19/2021

How to Construct a Loop • Initialization • What variables need starting values, once,

How to Construct a Loop • Initialization • What variables need starting values, once, before we begin the loop? • Variables used in the control_expression? • Variables used in the loop body? • Condition (control_expression) • Determine the terminating condition that makes the logical control_expression false and thus tells when to stop looping. • Increment / decrement • What variable or variables must change value during the looping, so that the terminating condition eventually happens? • Usually done at the end of the loop body. • Body • The statements that are repeated in the loop. 62 2/19/2021 12: 29 PM 2/19/2021

The WHILE statement loops zero, one, or many times The WHILE looping statement iterates

The WHILE statement loops zero, one, or many times The WHILE looping statement iterates zero or more times: WHILE control_expression Statement 1 Statement 2 … ENDWHILE • While the control_expression tests true at the start of an iteration of the loop, execute the set of statements between the WHILE and the ENDWHILE. • When the control_expression tests false at the beginning of an iteration of the loop, skip all the statements and continue execution after the ENDWHILE. • Some statement within the loop must cause some value in the control_expression to change from true to false, otherwise the control_expression will never test false and the looping will never stop. • If the control_expression is not true when the WHILE statement is first encountered, the set of statements is not executed even once. 63 2/19/2021 12: 29 PM 2/19/2021

The DO/WHILE statement loops one or many times The DO/WHILE looping statement iterates one

The DO/WHILE statement loops one or many times The DO/WHILE looping statement iterates one or more times: DO Statement 1 Statement 2 … WHILE control_expression • Execute the set of statements between the DO and the WHILE, and then repeat while the control_expression tests true at the end of an iteration of the loop. • When the control_expression tests false at the end of an iteration of the loop, continue execution after the line containing the WHILE; don’t do any more iterations. • Some statement within the loop must cause some value in the control_expression to change from true to false, otherwise the control_expression will never test false and the looping will never stop. • Because the control_expression is tested at the end of the loop, the set of statements is always executed at least once, even if the control_expression is false. 64 2/19/2021 12: 29 PM 2/19/2021

The FOR statement loops (zero, one, or many times) The FOR looping statement iterates

The FOR statement loops (zero, one, or many times) The FOR looping statement iterates zero or more times: FOR var from N 1 up/down to N 2 Statement 1 Statement 2 … ENDFOR • Execute the statements between FOR and ENDFOR with the var (variable) first set to the value N 1. Redo the same statements with var taking on the next value (up or down) toward N 2. Continue until var is equal to N 2. • If N 1 is already past N 2 when the FOR statement is first encountered, the set of statements is not executed even once. Execution continues after the ENDFOR. 65 2/19/2021 12: 29 PM 2/19/2021

Definitions • Variable: a named location in • • 66 memory Iterate: to loop;

Definitions • Variable: a named location in • • 66 memory Iterate: to loop; to do something more than once Increment: to increase the value in a variable, usually by 1 Decrement: to decrease the value in a variable, usually by 1 Termination condition: the condition that terminates a loop (the complement of the loop expression) 2/19/2021 12: 29 PM 2/19/2021

Problem 1 in English Problem: Numbers will be input from keyboard one at a

Problem 1 in English Problem: Numbers will be input from keyboard one at a time. Find the sum of the positive numbers and the sum of the negative numbers. Display both results. Use a loop controlled by a character from the keyboard. After each number, the user will be asked if he/she wishes to enter another number. If the response is 'Y', get the next number and sum it. Terminate input for any other response character. Semi-English solution: Initialize positive sum, negative sum DO Prompt user for a number Get and echo number IF number >= 0 Add to positive sum ELSE Add to negative sum ENDIF Prompt user for a Y response Get and echo response WHILE response is Y Display both sums 67 2/19/2021 12: 29 PM 2/19/2021

Problem 1 in Pseudocode Pos. Sum <-- 0 Neg. Sum <-- 0 DO PUT

Problem 1 in Pseudocode Pos. Sum <-- 0 Neg. Sum <-- 0 DO PUT “Enter a number: ” GET Number PUT “You entered: ” Number IF Number >= 0 Pos. Sum <-- Pos. Sum + Number ELSE Neg. Sum <-- Neg. Sum + Number ENDIF PUT “Type a ‘Y’ to continue” GET Response PUT “Your response was: ” Response WHILE Response EQUALS ‘Y’ PUT “The positive sum is: ” Pos. Sum PUT “The negative sum is: ” Neg. Sum 68 2/19/2021 12: 29 PM 2/19/2021

Problem 2 in English Problem: The marks for 70 students are to be entered.

Problem 2 in English Problem: The marks for 70 students are to be entered. Find and display the average mark. Use a loop controlled by a counter set to 70 when the program begins. Semi-English solution: Initialize counter, sum, maxmark WHILE counter <= maxmark Prompt for the mark GET mark Echo the mark Add mark to sum Increment counter ENDWHILE Calculate average using sum, maxmark PUT average 69 2/19/2021 12: 29 PM 2/19/2021

Problem 2 in Pseudocode Sum <-- 0 Counter <-- 1 Max. Mark <-- 70

Problem 2 in Pseudocode Sum <-- 0 Counter <-- 1 Max. Mark <-- 70 WHILE Counter <= Max. Mark PUT “Enter Mark #” Counter “: ” GET Mark PUT “You entered: ” Mark Sum <-- Sum + Mark Counter <-- Counter + 1 ENDWHILE Average <-- Sum / Max. Mark PUT “The average of “ Max. Mark PUT “ marks is “ Average 70 2/19/2021 12: 29 PM 2/19/2021

Pseudocode Exercises Write the pseudocode for the following problems: 1. Accept numbers from keyboard.

Pseudocode Exercises Write the pseudocode for the following problems: 1. Accept numbers from keyboard. Use 9999 to terminate. Find and display largest number (excluding 9999). 2. To count the number of times it takes player #2 to guess a number between 1 and 100 that is input by player #1. When the guess is too high or too low, display appropriate messages. When a correct guess is made, display an appropriate message and the number of guesses taken. 3. Accept positive numbers from keyboard. Terminate with a negative number. Determine the smallest and largest positive number that was entered. Display both. 4. Accept student test marks from the keyboard. Terminate with a -1. Display each mark. Count the number of marks that correspond to each letter grade: A, B, C, D, and F. Display the five counts. 71 2/19/2021 12: 29 PM 2/19/2021