Transcript 0, 1 and

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
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
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
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
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
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
6
Functions
int readNumbersReturningValue( int base )
{
char ch;
cin >> ch;
while (ch != ‘\n’)
{
// process digit
cin.get(ch)
}
}
How to check range?
7
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' );
return -1;
}
else
return total;
// MAX_LINE: 256
}
How to check range?
Call function!
bool isValid( char chDigit, int base )
8
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
ASCII Code Table
0
1
2
3
4
5
6
7
4
5
2
3
4
5
6
6
8
9
0
1
7
8
9
A
B
C
D
E
7
F
G
H
I
J
K
L
M
N
O
8
P
Q
R
S
T
U
V
W
X
Y
9
Z
a
b
c
10
d
e
f
All digits 0 through 9 are together
10
ASCII Code
Char
‘0’:
‘5’:
ASCII Code
48
?
Any base:
Valid digits: 0, 1, . . . (base – 1)
>= ‘0’ and
< (‘0’ + base)
0, 1 and -1 are not magic numbers!
2 and 9 are!
11
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
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' );
return -1;
}
else
return total;
// MAX_LINE: 256
}
13
Binary Numbers
Base 2
Two digits: 0, 1
27 26 25 24 23 22 21 20
1
27
128
0
0
0
1
1
1
1
+ 23 + 22 + 21 + 20
+8 +4 +2 +1
Decimal Number: 143
14
Binary Numbers
(may not be a byte)
Base 2
Two digits: 0, 1
23 22 21 20
1
1
0
1
=
23 + 22 + 0 + 20
=
8 + 4 +0 +1
= 13
15
Decimal Numbers
Base 10
How many digits?
Ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
143 =
1 * 102 + 4 * 101 + 3 * 100
16
Other Bases
Base 5
How many digits?
Five digits: 0, 1, 2, 3, 4
143 in base 5
1 * 52 + 4 * 51 + 3 * 50
= 25 + 20 + 3 = 48
Base 10
143 =
1 * 102 + 4 * 101 + 3 * 100
17
Backwards
27 26 25 24 23 22 21 20
20 21 22 23 24 25 26 27
Why?
Input one char at a time!
11110001
20 +
21 +
22 +
23 +
27
1 + 2 + 4 + 8 + 128
= 143
18
Backwards
What’s the value of the following number?
143
Base 5
1 * 50 +
4 * 51 +
= 1
= 96
3 * 52
+ 20 + 75
Base 10
1 * 100 +
4 * 101 +
3 * 102
= 1 + 40 + 300
= 341
19
Backwards
What’s the value of the following number?
41302
Base 2
Invalid!
Base 5
4 * 50 +
1 * 51 +
3 * 52 +
0 * 53 +
2 * 54
= 4 * 1 + 1 * 5 + 3 * 25 + 0 * 125 + 2 * 625
= 4 + 5 + 75 + 0 + 1250
= 1334
Base 10
20,314
20