1 / 13

Number systems and review of C++ syntax

Number systems and review of C++ syntax. In base 10 all digits are between 0 and 9 and can be interpreted as 419 = 9 x 1 + 1 x 10 +4 x 100 = 9 x 10 0 +1 x 10 1 + 4 x 10 2 23= 2 x 1 + 3 x 10 = 3 x 10 0 +2 x 10 1

isolde
Download Presentation

Number systems and review of C++ syntax

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Number systems and review of C++ syntax In base 10 all digits are between 0 and 9 and can be interpreted as 419 = 9 x 1 + 1 x 10 +4 x 100 = 9 x 100 +1 x 101 + 4 x 102 23= 2 x 1 + 3 x 10 = 3 x 100 +2 x 101 In base 2 all digits are either 0 or 1 and can be interpreted as 10111 = 1 x 1 + 1 x 2 + 1x 4 + 0 x 8 + 1 x 16 =1 x 20 +1 x 21 + 1 x 22 +0 x 23 +1 x 24 = 23( base 10) In base 16 we have the digits 0 though 9 and 10 is A, 11 is B , 12 is C, 13 is D, 14 is E and 15 is F and can be interpreted as 1A3= 3 x 16 0 +10 x 161 + 1 x 162 = 3 + 10 x 16 + 256 = 419(base10) 17 = 7 x 16 0 +1 x 161 = 7 + 16 = 23 (base 10)

  2. Base 10 Base 2 Base 16 • 1 1 • 10 2 • 11 3 • 100 4 • 101 5 • 110 6 • 111 7 • 1000 8 • 1001 9 • 1010 A • 1011 B • 1100 C • 1101 D • 1110 E • 1111 F • 1000 10

  3. What would be the output from #include <iostream.h> #include <iomanip.h> int main() { cout << 23 << " in base 16 is " << setiosflags(ios::hex) << 23 << endl; return 0; } ______________________________________ 23 in base 16 is 17

  4. Can we do our own conversions? 419%10 = 9 419% 16 = 3 419/ 10 = 41 419/16 = 26 41%10 = 1 26%16 = 10 = A(base 16) 41/10 = 4 26/16 = 1 4%10 = 4 1%16 = 1 4/10 = 0 1/16 = 0 23%10 = 3 23%16 = 7 23%2 = 1 23/10 = 2 23/16 = 1 23/2 = 11 2%10 = 2 1%16 = 1 11%2 =1 2/10 = 0 1/16 = 0 11/2 = 5 5%2 = 1 5/2 = 2 2%2 = 0 2/2 = 1 1%2 =1

  5. Program fragment to do binary conversions- first cut int dec; dec=23; while (dec >0) { cout << dec%2; dec= dec/2; } What is produced by the code above? What’s wrong? How do we fix it?

  6. Binary conversion in correct order int dec, i=0, bin[100]; dec=23; while (dec >0) { bin[i]=dec%2; //store stuff in array bin i++; dec= dec/2; } for ( int j =i-1; j>=0; j--) cout << bin[j]; //print bin in reverse order cout << endl;

  7. Hex conversion- first cut int dec, i=0, hex[100]; dec=23; while (dec >0) { hex[i]=dec%16; //store stuff in array hex i++; dec= dec/16; } for ( int j =i-1; j>=0; j--) cout << hex[j]; //print hex in reverse order cout << endl; What is the output? What would the output be if dec=26?

  8. Hex conversion- version with array int dec, i=0,hex[100]; char hexchar[16]={'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F'}; dec=26; while (dec >0) { hex[i]=dec%16; //store stuff in array hex i++; dec= dec/16; } for ( int j =i-1; j>=0; j--) cout << hexchar[hex[j]]; //print hex in reverse order cout << endl;

  9. int dec=27, i=0,hex[100]; char let; while (dec >0) { hex[i]=dec%16; //store stuff in array hex i++; dec= dec/16; } for ( int j =i-1; j>=0; j--) if (hex[j]<10) cout <<hex[j]; //these are numbers else { let='A'+hex[j]-10; //use fact that lettershave cout <<let; //consecutive rep. } cout << endl;

  10. Hex conversion with switch int dec=26, i=0,hex[100]; while (dec >0) { hex[i]=dec%16; //store stuff in array hex i++; dec= dec/16; } for ( int j =i -1; j>=0; j--) if (hex[j] <10) cout <<hex[j]; else switch(hex[j]) { case 10: cout <<'A'; break; case 11: cout <<'B'; break; case 12: cout <<'C'; break; case 13: cout <<'D'; break; case 14: cout <<'E'; break; case 15: cout <<'F'; break; }

  11. Binary conversion in function void dectobin( int dec); int main() { dectobin(23); return 0;} void dectobin( int dec) {int i=0, bin[100]; while (dec >0) { bin[i]=dec%2; //store stuff in array bin i++; dec= dec/2; } for ( int j =i-1; j>=0; j--) cout << bin[j]; //print bin in reverse order cout << endl; }

  12. Binary conversion with recursion void dectobin( int dec); int main() { int dec=6; dectobin(dec); return 0; } void dectobin(int dec) { if (dec==0) //base case return; else { dectobin(dec/2); //calling self cout <<dec%2; //putting cout after call gets reverse order } }

  13. Homework Fri: Review chapter 2 in Deitel for assessment test to determine who should not be in this class and how much to review. Tues. Read Appendix C and hand in 23, 24, 26, 29 Wednesday: Write a recursive function that receives a decimal input parameter and prints out its hexadecimal equivalent. Try out your function on the decimal number 27.

More Related