Results 1 to 10 of 12

Thread: Ascii help / column width - new to this.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The Program statement parameters are optional in most modern Pascal compilers. They are vestigial holdovers from the mainframe days where you specified the input/output "files" that were used for read/readln and write/writeln. You can include them, but modern compilers just ignore them. If your professor grades you for strict adherence to syntax, you might want to keep them. In that case, the proper way is like so:

    Code:
    program ASCIITABLE(input,output);
    As for the formatting of your output, you can space it a number of ways. The easiest and quickest is to use the field options for each data parameter you pass to write/writeln. Just put a colon and a number after it specifying the field size, like so:

    Code:
    writeln(i:3,chr(i):2);
    That will right-justify the character index number (ordinal) in a 3-space field, and the actual character right-justified in a 2-space field.

    As for doing them in more than one column, you have to do a little math in the loop, depending on whether you want the numbers incrementing horizontally (row ordering) or vertically (column ordering). Most ASCII tables are done in column ordering, so you'll probably want to do that. Since you can't "go back up" and write them out on the same lines (well, not with basic text streams, anyway), you have to write out several characters at once, which means you'll have to alter your loop a little bit. The first thing you'll have to do is figure out how many rows you will need. Since you said you wanted six columns, you take the number of characters you are writing out and divide it by 6. 127 / 6 = 21.166667. Thus, to keep all the characters in six columns, you will need 22 rows (have to round any fraction up to the next whole). So your ASCII chart will look something like this:

    Code:
      1 .  23 .  45 -  67 C  89 Y 111 o
      2 .  24 .  46 .  68 D  90 Z 112 p
      3 .  25 .  47 /  69 E  91 [ 113 q
    ... (13 more rows here)
     17 .  39 '  60 <  83 S 105 i 127 .
     18 .  40 (  61 =  84 T 106 j
     19 .  41 )  62 >  85 U 107 k
     20 .  42 *  64 @  86 V 108 l
     21 .  43 +  65 A  87 W 109 m
     22 .  44 ,  66 B  88 X 110 n
    Now, the trick is to figure out the right loop structures and calculations to generate the table this way. There are actually several ways you can do it. The most obvious and straightforward is to use two loops (the outer one from 1 to 22, the inner one 1 to 6), but it is possible to still just use one loop. Do note that there are a couple issues you will have to deal with:

    1) due to the fact that there are not 6 full columns (since 6 does not divide into 127 equally), you will have to use some kind of condition to suppress output for the sixth column after you get past 127 in it.
    2) Many of the characters below 32 are "unprintable" control characters. Some may print, depending on how you view the output, but some will break the formatting of your table. You may need to detect these and place an "unprintable placeholder" character instead. In the example above, I used the period "." character to represent the unprintables. What character you choose and how you do it is entirely up to you.

    Since this is a class project, I'll leave it to you to decide the logic you want to use, and then we can talk about how to implement it in Pascal.

  2. #2

    You guys are awesome!

    Not sure what to say!

    Theres so much useful information in there its unreal.

    This is my first attempt at programmingin so no prior knowledge of C etc, I think the last time I tried anythignw as when BASIC was about and then I was a bit too young to knwo what I was doing.

    The best thing to come out of what you guys have said is the explanations that go with it. It helps me understand so much more.

    Thanks!

    I'm going to take a little bit of everything and see what happens :d

    Many thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •