12
Given an integer value stored in a Given an integer value stored in a variable, develop an algorithm to print the variable, develop an algorithm to print the value to the display device. value to the display device. Integer Output Note that the value could be either Note that the value could be either positive or negative. positive or negative. 1) 1) TASK: Output minus sign if necessary TASK: Output minus sign if necessary 1) 1) IF: (x LT 0) IF: (x LT 0) 1) 1) OUT: ‘-’ OUT: ‘-’ 2) 2) x = -x x = -x 2) 2) TASK: Output an unsigned integer TASK: Output an unsigned integer

Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Embed Size (px)

Citation preview

Page 1: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Given an integer value stored in a variable, develop an Given an integer value stored in a variable, develop an algorithm to print the value to the display device.algorithm to print the value to the display device.

Integer OutputInteger Output

Note that the value could be either positive or negative.Note that the value could be either positive or negative.

1)1) TASK: Output minus sign if necessaryTASK: Output minus sign if necessary

1)1) IF: (x LT 0)IF: (x LT 0)

1)1) OUT: ‘-’OUT: ‘-’

2)2) x = -xx = -x

2)2) TASK: Output an unsigned integerTASK: Output an unsigned integer

Page 2: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Screen output goes from left to right, so much identify Screen output goes from left to right, so much identify which digits to output in that order and output them to the which digits to output in that order and output them to the screen one at a time.screen one at a time.

First, make sure we can output a single digit to the screen.First, make sure we can output a single digit to the screen.

Unsigned Integer OutputUnsigned Integer Output

1)1) TASK: Output single digit (assume i < 10)TASK: Output single digit (assume i < 10)

1)1) PUT: i + ‘0’ PUT: i + ‘0’

int PutD(int i)int PutD(int i)

{{

PutC( i + ‘0’ );PutC( i + ‘0’ );

}}

Page 3: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Give v = 1234, how do we figure out we need to print a ‘1’ Give v = 1234, how do we figure out we need to print a ‘1’ first?first?

Weighting of largest digitWeighting of largest digit

1)1) TASK: Determine the weighting of the largest digit. TASK: Determine the weighting of the largest digit. SET: weight = 1SET: weight = 1

1)1) WHILE: v > 10 * weight ( WHILE: v > 10 * weight ( > or >= ???> or >= ??? ) )

1)1) weight = 10 * weightweight = 10 * weight

Watch out for overflow!Watch out for overflow!

10*weight might not be representable10*weight might not be representable

.... but v/10 and v/weight are!.... but v/10 and v/weight are!

Page 4: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Weighting of largest digitWeighting of largest digit

1)1) TASK: Determine how many digits there are.TASK: Determine how many digits there are.

1)1) SET: weight = 1SET: weight = 1

2)2) WHILE: weight <= ( v / 10 ) (integer division okay?)WHILE: weight <= ( v / 10 ) (integer division okay?)

1)1) weight = 10 * weightweight = 10 * weight

for (weight = 1; weight <= v / 10; weight *= 10)for (weight = 1; weight <= v / 10; weight *= 10)

/* EMPTY LOOP */;/* EMPTY LOOP */;

Page 5: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Does it work?Does it work?

for (weight = 1; weight <= x / 10; weight *= 10)for (weight = 1; weight <= x / 10; weight *= 10)

/* EMPTY LOOP */;/* EMPTY LOOP */;

if x is 1234, then weight will end up being 1000:if x is 1234, then weight will end up being 1000:

weightweight weight < (1234)/10 ?weight < (1234)/10 ?

11 TT

1010 TT

100100 TT

10001000 FF

Page 6: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

What is the largest digit?What is the largest digit?

for (digit = 0; v >= weight; digit++)for (digit = 0; v >= weight; digit++)

v - = weight;v - = weight;

If the largest digit is N, then we can only subtract off weight If the largest digit is N, then we can only subtract off weight N times before we end up with a negative number. So N times before we end up with a negative number. So count the number of times we can subtract N.count the number of times we can subtract N.

1)1) TASK: Determine the left-most digit.TASK: Determine the left-most digit.

1)1) SET: digit = 0SET: digit = 0

2)2) WHILE: v > weight ( WHILE: v > weight ( > or >= ???> or >= ??? ) )

1)1) v = v - weightv = v - weight

2)2) digit = digit + 1digit = digit + 1

Page 7: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Does it work?Does it work?

if x is 3234, then weight will be 1000:if x is 3234, then weight will be 1000:

v > weight ?v > weight ? vv digitdigit

------ 32343234 00

TT 22342234 11

TT 12341234 22

TT 234234 33

FF

for (digit = 0; v >= weight; digit++)for (digit = 0; v >= weight; digit++)

v - = weight;v - = weight;

Page 8: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

How to get the next digit?How to get the next digit?

After putting out one digit (we know how to do that!) then After putting out one digit (we know how to do that!) then we can simply repeat the process with the weighting we can simply repeat the process with the weighting reduced by a factor of 10. We stop after we have printed reduced by a factor of 10. We stop after we have printed out the units digit (when weight = 1) so after dividing by ten out the units digit (when weight = 1) so after dividing by ten we will have zero.we will have zero.

1)1) WHILE: weight > 0 ( WHILE: weight > 0 ( > or >= ???> or >= ??? ) )

1)1) TASK: Determine leftmost digit (see prior slides)TASK: Determine leftmost digit (see prior slides)

2)2) TASK: Print leftmost digit (see prior slides)TASK: Print leftmost digit (see prior slides)

3)3) SET: weight = weight / 10SET: weight = weight / 10

Page 9: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Bringing it all together - top levelBringing it all together - top level

PRINTING INTEGERS:PRINTING INTEGERS:

1)1) TASK: Print sign and take absolute value.TASK: Print sign and take absolute value.

2)2) TASK: Print out digits in |v|TASK: Print out digits in |v|

1)1) TASK: Find weight of leftmost digit.TASK: Find weight of leftmost digit.

2)2) WHILE: weight > 0WHILE: weight > 0

1)1) TASK: Determine leftmost digit TASK: Determine leftmost digit

2)2) TASK: Print leftmost digitTASK: Print leftmost digit

3)3) SET: weight = weight / 10SET: weight = weight / 10

Page 10: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Bringing it all together - expandedBringing it all together - expanded

1)1) TASK: Print sign and take absolute value.TASK: Print sign and take absolute value.1)1) IF: (v) LT (0)IF: (v) LT (0)

1)1) OUT: ‘-’OUT: ‘-’2)2) SET: v = -vSET: v = -v

2)2) TASK: Print out |v|TASK: Print out |v|1)1) Find weight of leftmost digitFind weight of leftmost digit

1)1) SET: weight = 1SET: weight = 12)2) WHILE: (v / 10) GE (weight)WHILE: (v / 10) GE (weight)

1)1) SET: weight = weight * 10SET: weight = weight * 102)2) WHILE: (weight) GT (0)WHILE: (weight) GT (0)

1)1) TASK: Determine leftmost digitTASK: Determine leftmost digit1)1) SET: digit = 0SET: digit = 02)2) WHILE: (v) GE (weight)WHILE: (v) GE (weight)

1)1) SET: v = v / 10SET: v = v / 102)2) SET: digit = digit + 1SET: digit = digit + 1

2)2) TASK: Print leftmost digit (see prior slides)TASK: Print leftmost digit (see prior slides)1)1) PutD(digit)PutD(digit)

3)3) SET: weight = weight / 10SET: weight = weight / 10

/* TASK: Print sign, v = |v|/* TASK: Print sign, v = |v|if ( v < 0 )if ( v < 0 ){{

PutC(‘-’);PutC(‘-’);v = -v;v = -v;

} } /* TASK: Print out |v| *//* TASK: Print out |v| */weight = 1;weight = 1;while ( (v / 10) >= weight ) while ( (v / 10) >= weight )

weight *= 10;weight *= 10;while (weight > 0)while (weight > 0){{

digit = 0;digit = 0;while (v >= weight)while (v >= weight){{

v /= 10;v /= 10;digit ++;digit ++;

}}PutD(digit);PutD(digit);weight /= 10;weight /= 10;

}}

Page 11: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

Using for() loops to tidy up the codeUsing for() loops to tidy up the code

/* TASK: Print sign, v = |v|/* TASK: Print sign, v = |v|if ( v < 0 )if ( v < 0 ){{

PutC(‘-’);PutC(‘-’);v = -v;v = -v;

} } /* TASK: Print out |v| *//* TASK: Print out |v| */weight = 1;weight = 1;while ( (v / 10) >= weight ) while ( (v / 10) >= weight )

weight *= 10;weight *= 10;while (weight > 0)while (weight > 0){{

digit = 0;digit = 0;while (v >= weight)while (v >= weight){{

v /= 10;v /= 10;digit ++;digit ++;

}}PutD(digit);PutD(digit);weight /= 10;weight /= 10;

}}

/* TASK: Print sign, v = |v|/* TASK: Print sign, v = |v|if ( v < 0 )if ( v < 0 ){{

PutC(‘-’);PutC(‘-’);v = -v;v = -v;

} }

/* TASK: Print out |v| *//* TASK: Print out |v| */for ( weight = 1; (v / 10) >= weight ; weight *= 10 )for ( weight = 1; (v / 10) >= weight ; weight *= 10 )

/* EMPTY LOOP */;/* EMPTY LOOP */;while (weight > 0)while (weight > 0){{

for (digit = 0; v >= weight ; digit ++ )for (digit = 0; v >= weight ; digit ++ )v /= 10;v /= 10;

PutD(digit);PutD(digit);weight /= 10;weight /= 10;

}}

Page 12: Given an integer value stored in a variable, develop an algorithm to print the value to the display device. Integer Output Note that the value could be

The final function also counts charactersThe final function also counts characters

int Put_i( int v )int Put_i( int v ){{

int weight;int weight;int count, digit;int count, digit;

count = 0;count = 0;/* TASK: Print sign, v = |v|/* TASK: Print sign, v = |v|if ( v < 0 )if ( v < 0 ){{

PutC(‘-’);PutC(‘-’);count++;count++;v = -v;v = -v;

} } /* TASK: Print out |v| *//* TASK: Print out |v| */for ( weight = 1; (v / 10) >= weight ; weight *= 10 )for ( weight = 1; (v / 10) >= weight ; weight *= 10 )

/* EMPTY LOOP */;/* EMPTY LOOP */;while (weight > 0)while (weight > 0){{

for (digit = 0; v >= weight ; digit ++ )for (digit = 0; v >= weight ; digit ++ )v /= 10;v /= 10;

PutD(digit);PutD(digit);count++;count++;weight /= 10;weight /= 10;

}}return count;return count;

}}