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
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    Hi Steven and welcome to PGD.

    Not too bad on your first run through. I'm guessing that you have gotten yourself familiar with C already? I have a few small tips that might help clean up your code a little.

    ...but first, have you tried to compile your program yet?

    You can remove "(output)" from the first line. Pascal does not return values this way, I don't think, and you don't need to return anything anyhow, not for this program at least.

    Also the way your program will write each ascii character, you'll have one character per line, is this intended? If not the remove the line in your for loop with "writeln" in it. Instead you might want to try this...

    Code:
    for (i := 1 to 127) do
      write (chr(i) + '');
    I've saved you a few lines by using proper string manipulation and eliminated a reason to code block your for loop. Now the down side to this is that your next writeln call will print the text right after your last ascii character instead of on the next line or so.

    To fix this you could call a couple of writeln(''); procedures right after the for loop OR instead you can simply add in "\n" inside your string at the beginning. What this little guy will do is simply carry your text onto the next line. Put in a couple like this...

    Code:
    writeln('\n\nPress any key to continue');
    ...and you'll have your cursor skip down to lines before printing the desired text.

    One last thing, you can do away with those extra parameters hanging off the end of your write and writeln procedures, they don't really do anything for you in this case and it's more of a C-type construct anyhow.

    Hope that helps.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  2. #2
    Quote Originally Posted by WILL View Post
    One last thing, you can do away with those extra parameters hanging off the end of your write and writeln procedures, they don't really do anything for you in this case and it's more of a C-type construct anyhow.
    Actually...
    I prefer this:
    Code:
    writeln ('  ', i);
    ...rather than:
    Code:
    writeln ('  '+inttostr(i));
    They do the same thing. Writeln lets you do things like
    Code:
    writeln (3, ' divided by ', 2); // Will print "3 divided by 2"
    you can use word "mod" to do the line change every 6 columns. For example "i mod 6=0" will return true on 0, 6, 12, 18...

  3. #3
    Here's how I would do it:
    Code:
    program ASCIITABLE;
    
    const
        ColumnCount = 6;
        ColumnWidth = 8;
    
    var i: longint;
        ch: Char;
    
    begin
       writeln ('A to Z in ASCII');
       
       for i := 0 to 127 do
       begin
          write(chr(i):ColumnWidth);
          
          // Each column start at index 0, and ends at ColumnCount-1
          if (((i+1) mod ColumnCount) = 0) then
             Writeln;
       end;
       
       Writeln;
       writeln('Press any key to continue' ,ch);
       read(ch);
    end.
    The magic happens in a special syntax for the write/writeln function

    If you call Write('a':4); then it'll write " a". Write(1:5); will produce " 1". This is some bad syntactic sugar that's always existed in Pascal
    Last edited by JSoftware; 20-02-2011 at 10:14 PM.
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  4. #4

    Awesome!!!

    Quote Originally Posted by JSoftware View Post
    Here's how I would do it:
    Code:
    program ASCIITABLE;
    
    const
        ColumnCount = 6;
        ColumnWidth = 8;
    
    var i: longint;
        ch: Char;
    
    begin
       writeln ('A to Z in ASCII');
       
       for i := 0 to 127 do
       begin
          write(chr(i):ColumnWidth);
          
          // Each column start at index 0, and ends at ColumnCount-1
          if (((i+1) mod ColumnCount) = 0) then
             Writeln;
       end;
       
       Writeln;
       writeln('Press any key to continue' ,ch);
       read(ch);
    end.
    The magic happens in a special syntax for the write/writeln function

    If you call Write('a':4); then it'll write " a". Write(1:5); will produce " 1". This is some bad syntactic sugar that's always existed in Pascal



    -----------
    // Each column start at index 0, and ends at ColumnCount-1
    if (((i+1) mod ColumnCount) = 0) then
    Writeln;

    Can you explain how this creates a new column? we're div the ascci numeral by 6 and if no remainder then a new line is created?

    Sorry if this sounds retarded, I can "see" how it works, I just can't explain it to myself....

    (not the best sentence I've ever written....)

  5. #5
    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.

  6. #6

    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
  •