Lecture+12 - Engineering 101 Engineering 101 Lecture 12...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Engineering 101 Lecture 12 10/11/07 Selection Quote of the Day Quote of the Day We could never learn to be brave and patient, if there were only joy in the world. ­ Helen Keller Project 4 Project 4 Selection Selection So far we have discussed sequence and iteration. Selection allows the execution of a portion of your code depending on some condition. Selection Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: if (condition) statement_t else statement_f Selection Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: The condition if (condition) statement_t else statement_f is similar to the while condition. It evaluates to true or false. Selection Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: If the condition if (condition) statement_t else statement_f evaluates to true then statement_t is executed. Selection Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: If the condition if (condition) statement_t else statement_f evaluates to false then statement_f is executed. Selection Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: if (condition) statement_t else statement_f The statements can either be single line expressions or multiple expressions enclosed in braces. Selection Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: if (condition) statement_t else statement_f The else can be entirely removed. Selection Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: if (condition1) statement_t1 else if (condition2) statement_t2 else statement_f Or you can add more else conditions in the form of ‘else if’. Simple Example Simple Example int x; cin >> x; if (x % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example Simple Example int x; x 14 cin >> x; if (x % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example Simple Example int x; x 14 cin >> x; if (14 % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example Simple Example int x; x 14 cin >> x; if (0 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example Simple Example int x; x 14 cin >> x; if (x % 2 == 0) 14 is an even number cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example Simple Example int x; x 27 cin >> x; if (x % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example Simple Example int x; x 27 cin >> x; if (27 % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example Simple Example int x; x 27 cin >> x; if (1 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example Simple Example int x; x 27 cin >> x; if (x % 2 == 0) 27 is an odd number cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Multiple Expressions Multiple Expressions int x; cin >> x; if (x % 2 == 0){ cout << x << “is an even number” << endl; x = x / 2; } else cout << x << “is an odd number” << endl; More complex example More complex example Computing your taxes But not over Your tax is Plus Of the amount over If your taxable income is over $0 $21,450 $51,900 $21,450 $51,900 $0 $3,217.50 $11,734.50 15% 28% 31% $0 $21,450 $51,900 Let’s translate this into a program. double taxes(double income){ double tax; if (income<=21450.0) tax = 0.15*income; return tax; } If your taxable income is over But not over Your tax is Plus Of the amount over Taxes Taxes $0 $21,450 $51,900 $21,450 $51,900 $0 $3,217.50 $11,734.50 15% 28% 31% $0 $21,450 $51,900 double taxes(double income){ double tax; if (income<=21450.0) tax = 0.15*income; else { if (income <= 51900.0) tax = 3217.5 + 0.28*(income­21450.0); } return tax; If your taxable But not over Your tax is Plus Of the amount income is over over } $0 $21,450 $51,900 $0 15% 28% 31% $0 $21,450 $51,900 $3,217.50 $11,734.50 $21,450 $51,900 Taxes Taxes double taxes(double income){ double tax; if (income<=21450.0) tax = 0.15*income; else { if (income <= 51900.0) tax = 3217.5 + 0.28*(income­21450.0); else tax = 11734.5 + 0.31*(income­51900.0); } return tax; } Taxes Taxes Practice with Selection Practice with Selection You are at a movie theater: Regular price is $8 Senior Citizens, age 65 or older, $5.50 Students (student == true) $6 Write a program that will assign to an identifier called price the correct ticket price given a customer’s age and student status. Regular price is $8 Senior Citizens, age 65 or older, $5.50 Students (student == true) $6 if (age < 65) price = 8.00; else if (student == true) price = 6.00; else price = 5.50; if (age >= 65) if (age >= 65) price = 5.50; else if (student == true) price = 6.00; else price = 8.00; Write a program that will assign to an identifier called price the correct ticket price given a customer’s age and student status. if (age < 65) price = 8.00; if (student == true) price = 6.00; else price = 5.50; if (age >= 65) price = 5.50; if (student == true) price = 6.00; else price = 8.00; Regular price is $8 Senior Citizens, age 65 or older, $5.50 Students (student == true) $6 if (age < 65) price = 8.00; else if (student == true) price = 6.00; else price = 5.50; if (age >= 65) if (age >= 65) price = 5.50; else if (student == true) price = 6.00; else price = 8.00; Write a program that will assign to an identifier called price the correct ticket price given a customer’s age and student status. if (age < 65) price = 8.00; if (student == true) price = 6.00; else price = 5.50; if (age >= 65) price = 5.50; if (student == true) price = 6.00; else price = 8.00; Something is wrong with this code Something is wrong with this code int rock=1, scissor=2, paper=3; int int a, b; int cin >> a >> b; cin if (a == b) cout << “tie” << endl; if else if (a == rock) else if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if (a == scissor) else if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if (a == paper) else if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; else What Result Do You Get When… What Result Do You Get When… a = 1 (rock) b = 2 (scissor) 1­ a wins 2­ b wins 3­ neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if (a == scissor) if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if (a == paper) if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What Result Do You Get When… What Result Do You Get When… a = 1 (rock) b = 2 (scissor) 1­ a wins 2­ b wins 3­ neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if (a == scissor) if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if (a == paper) if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What Result Do You Get When… What Result Do You Get When… a = 3 (paper) b = 1 (rock) 1­ a wins 2­ b wins 3­ neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if (a == scissor) if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if (a == paper) if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What Result Do You Get When… What Result Do You Get When… a = 3 (paper) b = 1 (rock) 1­ a wins 2­ b wins 3­ neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if (a == scissor) if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if (a == paper) if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What is wrong with the code? What is wrong with the code? int rock=1, scissor=2, paper=3; int int a, b; int cin >> a >> b; cin if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if else if (a == scissor) else if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if else if (a == paper) else if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; else • The “else” is always associated with the most recent “if” What is wrong with the code? What is wrong with the code? int rock=1, scissor=2, paper=3; int int a, b; int cin >> a >> b; cin if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if • The “else” is always associated with the most recent unpaired “if” else if (a == scissor) else if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if else if (a == paper) else if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What is wrong with the code? What is wrong with the code? int rock=1, scissor=2, paper=3; int int a, b; int cin >> a >> b; cin if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if • a = 1 (rock) • b = 2 (scissor) • a wins else if (a == scissor) else if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if else if (a == paper) else if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What is wrong with the code? What is wrong with the code? int rock=1, scissor=2, paper=3; int int a, b; int cin >> a >> b; cin if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if • a = 3 (paper) • b = 1 (rock) • neither else if (a == scissor) else if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if else if (a == paper) else if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What Result Do You Get When… What Result Do You Get When… a = 2 (scissor) b = 3 (paper) 1­ a wins 2­ b wins 3­ neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if (a == scissor) if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if (a == paper) if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What Result Do You Get When… What Result Do You Get When… a = 2 (scissor) b = 3 (paper) 1­ a wins 2­ b wins 3­ neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if (a == scissor) if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if (a == paper) if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What is wrong with the code? What is wrong with the code? int rock=1, scissor=2, paper=3; int int a, b; int • How do we fix this? cin >> a >> b; cin if (a == b) cout << “tie” << endl; else if (a == rock) if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; else if else if (a == scissor) else if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; else if else if (a == paper) else if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; What is wrong with the code? What is wrong with the code? int rock=1, scissor=2, paper=3; int int a, b; int • How do we fix this? cin >> a >> b; cin • Idea 1: braces if (a == b) cout << “tie” << endl; if else if (a == rock) { else if (b == scissor) cout << “a wins” << endl; else if (b == paper) cout << “b wins” << endl; } else if (a == scissor) { else if (b == paper) cout << “a wins” << endl; else if (b == rock) cout << “b wins” << endl; } else if (a == paper) { if (b == rock) cout << “a wins” << endl; else if (b == scissor) cout << “b wins” << endl; else } What is wrong with the code? What is wrong with the code? int rock=1, scissor=2, paper=3; int int a, b; int • How do we fix this? cin >> a >> b; cin • Idea 2: remove if (a == b) cout << “tie” << endl; if unnecessary conditions else if (a == rock) else if (b == scissor) cout << “a wins” << endl; if else cout << “b wins” << endl; else if (a == scissor) else if (b == paper) cout << “a wins” << endl; if else cout << “b wins” << endl; else if (a == paper) if (b == rock) cout << “a wins” << endl; if else cout << “b wins” << endl; else What is wrong with the code? What is wrong with the code? int rock=1, scissor=2, paper=3; int int a, b; int • How do we fix this? • Idea 3: think of a simpler expression cin >> a >> b; cin if (a == b) cout << “tie” << endl; if else if ((b – a) == 1 or (b – a) == -2) cout << “a wins” << endl; else cout else cout << “b wins” << endl; else a rock (1) scissor (2) paper (3) b scissor(2) paper (3) rock (1) b­a 1 1 ­2 Selection plus Iteration Selection plus Iteration Often solving a problem involves using a combination of selection plus iteration. What does this procedure do? void thisproc(int N){ int j; int for (j=1; j <= sqrt(static_cast<double>(N)); j++) for if (N%j == 0) cout << j << “x” << N/j << “ “; if cout << endl; cout return; return; } What is the result of calling What is the result of calling thisproc(8) ? 1­ 3­ 1x8 2x4 4x2 8x1 8x1 4x2 4­ 2­ 1x8 2x4 8x1 void thisproc(int N){ int j; for (j=1; j <= sqrt(static_cast<double>(N)); j++) if (N%j == 0) cout << j << “x” << N/j << “ “; cout << endl; return; } What is the result of calling What is the result of calling thisproc(8) ? 1­ 3­ 1x8 2x4 4x2 8x1 8x1 4x2 4­ 2­ 1x8 2x4 8x1 void thisproc(int N){ int j; for (j=1; j <= sqrt(static_cast<double>(N)); j++) if (N%j == 0) cout << j << “x” << N/j << “ “; cout << endl; return; } Selection plus Iteration Selection plus Iteration N8 void thisproc(int N){ int j; int for (j=1; j <= sqrt(static_cast<double>(N)); j++) for if (N%j == 0) cout << j << “x” << N/j << “ “; if cout << endl; cout return; return; } thisproc(8) Selection plus Iteration Selection plus Iteration N8 void thisproc(int N){ j1 int j; int for (j=1; j <= sqrt(static_cast<double>(N)); j++) for if (N%j == 0) cout << j << “x” << N/j << “ “; if cout << endl; cout return; return; } thisproc(8) Selection plus Iteration Selection plus Iteration N8 void thisproc(int N){ j1 int j; int for (j=1; j <= sqrt(8.0); j++) for if (N%j == 0) cout << j << “x” << N/j << “ “; if cout << endl; cout return; return; } thisproc(8) Selection plus Iteration Selection plus Iteration N8 void thisproc(int N){ j1 int j; int for (j=1; j <= 2.828; j++) 2.828 if (N%j == 0) cout << j << “x” << N/j << “ “; if cout << endl; cout return; return; } thisproc(8) Selection plus Iteration Selection plus Iteration N8 void thisproc(int N){ j1 int j; int for (j=1; j <= 2.828; j++) 2.828 if (8%1 == 0) cout << j << “x” << N/j << “ “; if cout << endl; cout return; return; } thisproc(8) Selection plus Iteration Selection plus Iteration N8 void thisproc(int N){ j1 int j; int for (j=1; j <= 2.828; j++) 2.828 if (8%1 == 0) cout << j << “x” << N/j << “ “; if cout cout << endl; cout return; return; } 1x8 thisproc(8) Selection plus Iteration Selection plus Iteration N8 void thisproc(int N){ j1 int j; int for (j=1; j <= 2.828; j++) 2.828 if (8%2 == 0) cout << j << “x” << N/j << “ “; if cout cout << endl; cout return; return; } 1x8 2x4 thisproc(8) What is the result of calling What is the result of calling thisproc(9) ? 1­ 3­ 1x9 2x4.5 3x3 2­ 9x1 4.5x2 3x3 1x9 3x3 4­ 9x1 3x3 void thisproc(int N){ int j; for (j=1; j <= sqrt(static_cast<double>(N)); j++) if (N%j == 0) cout << j << “x” << N/j << “ “; cout << endl; return; } What is the result of calling What is the result of calling thisproc(9) ? 1­ 3­ 1x9 2x4.5 3x3 2­ 9x1 4.5x2 3x3 1x9 3x3 4­ 9x1 3x3 void thisproc(int N){ int j; for (j=1; j <= sqrt(static_cast<double>(N)); j++) if (N%j == 0) cout << j << “x” << N/j << “ “; cout << endl; return; } Selection plus Iteration Selection plus Iteration It prints all the pairs that can be multiplied to make the integer N void thisproc(int N){ int j; int for (j=1; j <= sqrt(static_cast<double>(N)); j++) for if (N%j == 0) cout << j << “x” << N/j << “ “; if cout << endl; cout return; return; } News Flash! (Thanks Adam) Radioactive decay in your pocket! Yummy… Powered by the decay of protons “If all goes well” they will be here in 2­3 years… There is, of course, a LOT of skepticism. So do your homework first. http://community.zdnet.co.uk/blog/0,1000000567,10006069o­2000331777b,00.htm Which procedure Which procedure will…. Request a single integer number. It then asks the user to input a number. If the number guessed is too high print out “guess lower” and get another number If the number guessed is too low print out “guess higher” and get another number If the number guessed is correct let the user know this is the number and tell how many times the user had to guess. 1 2 3 Which procedure Which procedure will…. Request a single integer number. It then asks the user to input a number. If the number guessed is too high print out “guess lower” and get another number If the number guessed is too low print out “guess higher” and get another number If the number guessed is correct let the user know this is the number and tell how many times the user had to guess. 1 2 3 void GuessIt (int n){ void int guess, num=1; int cout << “guess a number” << endl; cin >> guess; while ( guess != n ){ while != if ( guess > n ) cout << “Guess lower” << endl; else cout << “Guess higher” << endl; cin >> guess; num = num + 1; } cout << “ You guessed it in “ << num << “ tries“ << endl; return; } Guessing Game Guessing Game Using Selection To Check Using Selection To Check Program Status One of the most important uses of selection is to check the status of a command or action cout << “Enter a positive number” << endl; cin >> n; exit() is if (n <= 0){ defined in the cout << n << “ is not positive” << endl; cstdlib library. exit(EXIT_FAILURE); It terminates } the program. cout << “The inverse square root is “ << 1.0/sqrt(static_cast<double>(n))<<endl; Streams are a particular case in point since whenever you try to open or read from a file you don’t know if the file might not exist or be finished. ifstream infile(“myfile.data”); if( infile.fail() ){ cerr << “Problem with file” << endl; stream.fail() can be exit(EXIT_FAILURE); used with any stream } and returns true if infile >> x >> y >> z; the last action failed. Using Selection To Check Using Selection To Check Program Status int x, n=0; ifstream infile(“myfile.data”); infile >> x; while (! infile.fail() ){ cout << x; n = n + 1; infile >> x; } cout << “read in “ << n << “ integers.” << endl; fail() can also be used to read fail() can also be used to read files of arbitrary length Switch Statements Switch Statements A switch statement is used to decide between a number of cases switch (integer_expression){ case value: expression case value: expression default: expression } The integer expression is evaluated and the thread of execution starts from the matching case. If none matches it starts from the default. Switch Statements Switch Statements switch (weekday){ case 1: cout << “Monday”; breaks are break; needed. case 2: cout << “Tuesday”; Otherwise break; switch drops case 3: cout << “Wednesday”; through to break; next expression. case 4: cout << “Thursday”; break; case 5: cout << “Friday”; break; default: cout << “Weekend”; } Execution Control Sequence Selection Iteration Two Important Halves of Two Important Halves of Computer Programming Data Representation How do we structure the data that we are acting on? Data Representation Data Representation All data inside the computer is represented by bits. Bits can have only two values 0 or 1. 4 bits make a nibble 8 bits make a byte 11001010 10101011 Data Encoding Data Encoding Bytes 11001110 10101010 11110000 10100101 11111111 11001110 11001110 Meaning Hail! to the victors valiant Hail! Hail! Each bit allows two possibilities. 1 bit: 0 1 (2 things) 2 bits: 00 01 10 11 (4 things) 3 bits: 000 001 010 011 100 101 110 111 (8 things) n bits: 2n things 8 bits: 256 things How many things can you encode How many things can you encode with a byte (8 bits)? Base 2 is a number system like Base 2 is a number system like base 10 456,347 Base 2 is a number system like Base 2 is a number system like base 10 456,347 100000 105 4 10000 104 5 1000 103 6 100 102 3 10 101 4 1 100 7 Base 2 is a number system like Base 2 is a number system like base 10 456,347 100000 105 4 10000 104 5 1000 103 6 100 102 3 10 101 4 1 100 7 4 x 105 + 5 x 104 + 6 x 103 + 3 x 102 + 4 x 101 + 7 x 100 Base 2 is a number system like Base 2 is a number system like base 10 110001 32 25 1 16 24 1 8 23 0 4 22 0 2 21 0 1 20 1 1 x 25 + 1 x 24 + 0 x 23 + 0 x 22 + 0 x 21 + 1 x 20 Base 2 is a number system like Base 2 is a number system like base 10 110001 32 25 1 16 24 1 8 23 0 4 22 0 2 21 0 1 20 1 1 x 25 + 1 x 24 + 0 x 23 + 0 x 22 + 0 x 21 + 1 x 20 110001(base 2) = 32+16+1 = 49(base 10) 128 27 Convert the Binary Number to Convert the Binary Number to Decimal 64 26 32 25 16 24 8 23 4 22 2 21 1 20 01000001 1- 27 2- 65 3- 67 4- 79 128 27 Convert the Binary Number to Convert the Binary Number to Decimal 64 26 32 25 16 24 8 23 4 22 2 21 1 20 01000001 1- 27 2- 65 3- 67 4- 79 64 + 1 = 65 128 27 Convert the Binary Number to Convert the Binary Number to Decimal 64 26 32 25 16 24 8 23 4 22 2 21 1 20 01101011 1- 85 2- 101 3- 97 4- 107 128 27 Convert the Binary Number to Convert the Binary Number to Decimal 64 26 32 25 16 24 8 23 4 22 2 21 1 20 01101011 1- 85 2- 101 3- 97 4- 107 64 + 32 + 8 + 2 + 1 = 107 Adding Binary Numbers Adding Binary Numbers 0 1 1 0 0 0 1 1 1 0 + Adding Binary Numbers Adding Binary Numbers 0 1 1 0 0 + 0 1 1 1 0 0 numbers are added column by column Adding Binary Numbers Adding Binary Numbers 0 1 1 0 0 + 0 1 1 1 0 1 0 numbers are added column by column Adding Binary Numbers Adding Binary Numbers 0 1 1 0 0 + 0 1 1 1 0 2 1 0 2 is not a valid binary digit. In this case recall that 10 in binary equals 2 in decimal Adding Binary Numbers Adding Binary Numbers 1 0 1 1 0 0 + 0 1 1 1 0 0 1 0 So we replace the 2 by a 0 and carry the extra 1. Adding Binary Numbers Adding Binary Numbers 1 0 1 1 0 0 + 0 1 1 1 0 0 1 0 Now what do we do with 1 + 1 + 1? Recall that 11 in binary equals 3 in decimal Adding Binary Numbers Adding Binary Numbers 1 1 0 1 1 0 0 + 0 1 1 1 0 1 0 1 0 So we output a 1 and carry the extra 1. Adding Binary Numbers Adding Binary Numbers 1 1 0 1 1 0 0 + 0 1 1 1 0 1 1 0 1 0 (8 + 4 = (8 + 4 + 2 = (16 + 8 + 2 = 12) 16) 28) Next Lecture Next Lecture More on Data Types! Yes! ...
View Full Document

Ask a homework question - tutors are online