kent logo

CO538 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for conversion


Question 7 (2003):

In the method `tabulate.int' from q2.occ, how does the sequence:

      in ? n
      out.int (n, 15, out)
      out.string ("*c*n", 0, out)

work ?

I'm curious because I'd like to make a:

    PROC testing (CHAN OF BYTE in, CHAN OF INT out)

procedure/method for testing purposes. In other words, how does the conversion work and is it basically the same to convert the other way around?

Answer 7:

The conversion from an `INT' value to the sequence of decimal digits that represents it is performed inside `out.int'. Essentially it involves a sequence of `modulo 10' then `divide-by 10', until the value is zero. It's mildly more complicated since the digits are generated in the reverse order.

The code for `out.int' can be found on raptor in:


Converting the other way is, as you suspect, largely the reverse. However, it's already been programmed for you (also in `utils.occ'):

    PROC ask.int (VAL []BYTE prompt, INT n, VAL INT max, CHAN OF BYTE in, out)

As an example, your `testing' PROC can be implemented with:

    PROC testing (CHAN OF BYTE in, out, CHAN OF INT vals.out)
        INT n:
          ask.int ("Enter a number: ", n, 11, in, out)
          vals.out ! n

The `in' and `out' channels should be wired to the keyboard and screen respectively. The screen channel is needed so that characters can be echo'd as typed, in addition to displaying the prompt. `ask.int' handles all the unpleasantness of keyboard input, such as rejecting invalid characters and handling backspace properly.

The `max' parameter specifies the maximum number of digits that may be entered, inclusive of any leading `+' or `-'.

Keywords: q2 , output , conversion

Valid CSS!

Valid XHTML 1.0!

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Mon May 20 13:50:26 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.