1 / 12

Integer Output

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

tracey
Download Presentation

Integer Output

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

  2. Unsigned Integer Output 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. • TASK: Output single digit (assume i < 10) • PUT: i + ‘0’ int PutD(int i) { PutC( i + ‘0’ ); }

  3. Weighting of largest digit Give v = 1234, how do we figure out we need to print a ‘1’ first? • TASK: Determine the weighting of the largest digit. SET: weight = 1 • WHILE: v > 10 * weight ( > or >= ??? ) • 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 • TASK: Determine how many digits there are. • SET: weight = 1 • WHILE: weight <= ( v / 10 ) (integer division okay?) • 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:

  6. What is the largest digit? 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. • TASK: Determine the left-most digit. • SET: digit = 0 • WHILE: v > weight ( > or >= ??? ) • v = v - weight • digit = digit + 1 for (digit = 0; v >= weight; digit++) v - = weight;

  7. Does it work? for (digit = 0; v >= weight; digit++) v - = weight; if x is 3234, then weight will be 1000:

  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. • WHILE: weight > 0 ( > or >= ??? ) • TASK: Determine leftmost digit (see prior slides) • TASK: Print leftmost digit (see prior slides) • SET: weight = weight / 10

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

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

More Related