Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 either positive or negative. 1)TASK: Output minus sign if necessary 1)IF: (x LT 0) 1)OUT: ‘-’ 2)x = -x 2)TASK: Output an unsigned integer

2 Screen output goes from left to right, so much identify which digits to output in that order and output them to the screen one at a time. First, make sure we can output a single digit to the screen. Unsigned Integer Output 1)TASK: Output single digit (assume i < 10) 1)PUT: i + ‘0’ int PutD(int i) { PutC( i + ‘0’ ); }

3 Give v = 1234, how do we figure out we need to print a ‘1’ first? Weighting of largest digit 1)TASK: Determine the weighting of the largest digit. SET: weight = 1 1)WHILE: v > 10 * weight ( > or >= ??? ) 1)weight = 10 * weight Watch out for overflow! 10*weight might not be representable.... but v/10 and v/weight are!

4 Weighting of largest digit 1)TASK: Determine how many digits there are. 1)SET: weight = 1 2)WHILE: weight <= ( v / 10 ) (integer division okay?) 1)weight = 10 * weight for (weight = 1; weight <= v / 10; weight *= 10) /* EMPTY LOOP */;

5 Does it work? for (weight = 1; weight <= x / 10; weight *= 10) /* EMPTY LOOP */; if x is 1234, then weight will end up being 1000: weight weight < (1234)/10 ? 1T 10T 100T 1000F

6 What is the largest digit? for (digit = 0; v >= weight; digit++) v - = 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 count the number of times we can subtract N. 1)TASK: Determine the left-most digit. 1)SET: digit = 0 2)WHILE: v > weight ( > or >= ??? ) 1)v = v - weight 2)digit = digit + 1

7 Does it work? if x is 3234, then weight will be 1000: v > weight ? vdigit ---32340 T22341 T12342 T 234 2343 F for (digit = 0; v >= weight; digit++) v - = weight;

8 How to get the next digit? After putting out one digit (we know how to do that!) then we can simply repeat the process with the weighting reduced by a factor of 10. We stop after we have printed out the units digit (when weight = 1) so after dividing by ten we will have zero. 1)WHILE: weight > 0 ( > or >= ??? ) 1)TASK: Determine leftmost digit (see prior slides) 2)TASK: Print leftmost digit (see prior slides) 3)SET: weight = weight / 10

9 Bringing it all together - top level PRINTING INTEGERS: 1)TASK: Print sign and take absolute value. 2)TASK: Print out digits in |v| 1)TASK: Find weight of leftmost digit. 2)WHILE: weight > 0 1)TASK: Determine leftmost digit 2)TASK: Print leftmost digit 3)SET: weight = weight / 10

10 Bringing it all together - expanded 1)TASK: Print sign and take absolute value. 1)IF: (v) LT (0) 1)OUT: ‘-’ 2)SET: v = -v 2)TASK: Print out |v| 1)Find weight of leftmost digit 1)SET: weight = 1 2)WHILE: (v / 10) GE (weight) 1)SET: weight = weight * 10 2)WHILE: (weight) GT (0) 1)TASK: Determine leftmost digit 1)SET: digit = 0 2)WHILE: (v) GE (weight) 1)SET: v = v / 10 2)SET: digit = digit + 1 2)TASK: Print leftmost digit (see prior slides) 1)PutD(digit) 3)SET: weight = weight / 10 /* TASK: Print sign, v = |v| if ( v < 0 ) {PutC(‘-’); v = -v; } /* TASK: Print out |v| */ weight = 1; while ( (v / 10) >= weight ) weight *= 10; while (weight > 0) { digit = 0; while (v >= weight) { v /= 10; digit ++; }PutD(digit); weight /= 10; }

11 Using for() loops to tidy up the code /* TASK: Print sign, v = |v| if ( v < 0 ) {PutC(‘-’); v = -v; } /* TASK: Print out |v| */ weight = 1; while ( (v / 10) >= weight ) weight *= 10; while (weight > 0) { digit = 0; while (v >= weight) { v /= 10; digit ++; }PutD(digit); weight /= 10; } /* TASK: Print sign, v = |v| if ( v < 0 ) {PutC(‘-’); v = -v; } /* TASK: Print out |v| */ for ( weight = 1; (v / 10) >= weight ; weight *= 10 ) /* EMPTY LOOP */; while (weight > 0) { for (digit = 0; v >= weight ; digit ++ ) v /= 10; PutD(digit); weight /= 10; }

12 The final function also counts characters int Put_i( int v ) { int weight; int count, digit; count = 0; /* TASK: Print sign, v = |v| if ( v < 0 ) {PutC(‘-’);count++; v = -v; } /* TASK: Print out |v| */ for ( weight = 1; (v / 10) >= weight ; weight *= 10 ) /* EMPTY LOOP */; while (weight > 0) { for (digit = 0; v >= weight ; digit ++ ) v /= 10; PutD(digit);count++; weight /= 10; } return count; }


Download ppt "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."

Similar presentations


Ads by Google