Download presentation
Presentation is loading. Please wait.
Published byEric Leonard Modified over 9 years ago
1
Required Functions for Program 3 int readUntilValidBaseRead( ); int readNumbersReturningValue( int base ); int decimalValueOf( char chDigit ); bool isValid( char chDigit, int base ); MUST NOT Change the Prototypes! You can have other functions. 1
2
Required Functions for Program 3 //--------------------------------------------------------------------- // This function reads bases until a valid base is read or eof occurs. // If an invalid base is read, an error message is displayed and the // rest of the line is ignored and another attempt to read a base value // will be attempted. // -1 is returned if eof occurs otherwise a valid base value is // returned. //--------------------------------------------------------------------- int readUntilValidBaseRead( ) Is base a char, int, float, or string? int! Input for Test Run 2: 1 hello 10 this is bad too -1 as well as this 2
3
Required Functions for Program 3 //--------------------------------------------------------------------- // This function reads in a sequence of characters that represent // a number in the given base. A valid sequence is given in a // "backwards" format such that the rightmost digit is given first, // the second to the rightmost digit is next, etc. // This function returns the value of this sequence of characters if // it is a valid sequence. If it is not valid it returns -1. // params: ( ) //--------------------------------------------------------------------- int readNumbersReturningValue( int base ) Do we know the base now? Yes! And it’s a valid base! Sample Input: 2 1101 3
4
Pseudocode for main() Set totalSum to zero Read until a valid base is read or end of file while not at end of file write "For the given base ", base Assign to numberValue the translated number from the input if numberValue is not valid Write " the number is NOT valid!" else Accumulate numberValue into totalSum Write numberValue appropriately labelled Read until another valid base is read or end of file Write totalSum appropriately labelled 4
5
Calling Functions from main() Set totalSum to zero Call function readUntilValidBaseRead while not at end of file write "For the given base ", base Call function readNumbersReturningValue to get numberValue if numberValue is not valid Write " the number is NOT valid!" else Accumulate numberValue into totalSum Write numberValue appropriately labelled Call readUntilValidBaseRead to get next base Write totalSum appropriately labelled 5
6
6 Functions //--------------------------------------------------------------------- // This function reads in a sequence of characters that represent // a number in the given base. A valid sequence is given in a // "backwards" format such that the rightmost digit is given first, // the second to the rightmost digit is next, etc. // This function returns the value of this sequence of characters if // it is a valid sequence. If it is not valid it returns -1. // params: ( ) //--------------------------------------------------------------------- int readNumbersReturningValue( int base ) How to read ‘\n’? cin.get(ch) How to read the first digit? cin >> ch; Input for Run 1: 2 1101 3 1212 5 66 2 1111
7
7 Functions int readNumbersReturningValue( int base ) { char ch; cin >> ch; while (ch != ‘\n’) { // process digit cin.get(ch) } } How to check range?
8
8 int readNumbersReturningValue( int base ) { char ch; bool valid; int total; cin >> ch; // call isValid() while (ch != ‘\n’ && valid) { // process digit // Call function decimalValueOf cin.get(ch); // call isValid() } if (! valid) { cin.ignore( MAX_LINE, '\n' ); // MAX_LINE: 256 return -1; } else return total; } How to check range? Call function! bool isValid( char chDigit, int base )
9
Required Functions for Program 3 //--------------------------------------------------------------------- // This function returns true if chDigit is a valid digit in the given // base, it returns false otherwise. // params: (in, in) //--------------------------------------------------------------------- bool isValid( char chDigit, int base ) Base 2: Valid digits: 0, 1 Base 5: Valid digits: 0, 1, 2, 3, 4 Any base: Valid digits: 0, 1,... (base – 1) 9
10
10 ASCII Code Table All digits 0 through 9 are together 0123456789 401 523456789 6ABCDE 7FGHIJKLMNO 8PQRSTUVWXY 9Zabc 10def
11
11 ASCII Code Char ASCII Code ‘0’: 48 ‘5’: ? Any base: Valid digits: 0, 1,... (base – 1) >= ‘0’ and < (‘0’ + base) 0, 1 and -1 are not magic numbers! 2 and 9 are!
12
Required Functions for Program 3 //--------------------------------------------------------------------- // This function returns the numeric value of the character digit that // is stored in chDigit. // params: (in) //--------------------------------------------------------------------- int decimalValueOf( char chDigit ) Assume chDigit is a digit Base 5: chDigit ‘3’ to 3 (int) chDigit – ‘0’ 12
13
13 int readNumbersReturningValue( int base ) { char ch; bool valid; int total; cin >> ch; // call isValid() while (ch != ‘\n’ && valid) { // process digit // Call function decimalValueOf cin.get(ch); // call isValid() } if (! valid) { cin.ignore( MAX_LINE, '\n' ); // MAX_LINE: 256 return -1; } else return total; }
14
14 Binary Numbers 2 7 + 2 3 + 2 2 + 2 1 + 2 0 128 + 8 + 4 + 2 + 1 Decimal Number: 143 10001111 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 Base 2 Two digits: 0, 1
15
15 Binary Numbers (may not be a byte) 2 3 2 2 2 1 2 0 1 1 0 1 = 2 3 + 2 2 + 0 + 2 0 = 8 + 4 + 0 + 1 = 13 Base 2 Two digits: 0, 1
16
16 Decimal Numbers Base 10 How many digits? Ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 143 = 1 * 10 2 + 4 * 10 1 + 3 * 10 0
17
17 Other Bases Base 5 How many digits? Five digits: 0, 1, 2, 3, 4 143 in base 5 1 * 5 2 + 4 * 5 1 + 3 * 5 0 = 25 + 20 + 3 = 48 Base 10 143 = 1 * 10 2 + 4 * 10 1 + 3 * 10 0
18
18 Backwards Why? Input one char at a time! 1 1 1 1 0 0 0 1 2 0 + 2 1 + 2 2 + 2 3 + 2 7 1 + 2 + 4 + 8 + 128 = 143 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7
19
19 Backwards What’s the value of the following number? 143 Base 5 1 * 5 0 + 4 * 5 1 + 3 * 5 2 = 1 + 20 + 75 = 96 Base 10 1 * 10 0 + 4 * 10 1 + 3 * 10 2 = 1 + 40 + 300 = 341
20
20 Backwards What’s the value of the following number? 4 1 3 0 2 Base 2 Invalid! Base 5 4 * 5 0 + 1 * 5 1 + 3 * 5 2 + 0 * 5 3 + 2 * 5 4 = 4 * 1 + 1 * 5 + 3 * 25 + 0 * 125 + 2 * 625 = 4 + 5 + 75 + 0 + 1250 = 1334 Base 10 20,314
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.