Thread: While loop

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    5

    Question While loop

    this might be a lengthy question but I would appreciate any help.

    the criteria seems simple and I know how to print the ASCII table. Although how would one space 20 characters on a line using modular division?

    this is the question:

    " Write a program to print out as many characters as possible in the ASCII table. A while(..) Loop must be used for this purpose. Use Modular Division to ensure that 20 characters are printed per row with separation between each character of two white spaces."

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Simply put, this exercise demands that only a single while loop is used. The ASCII table is larger than 20 characters, therefore you need modulo to know when to break to the next line. I would do it at (i+1) % 20 == 0, but that's not the only way.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    I see how you are using modulo to get a value up until 20, although how would you use 'printf' to print 20 characters and only after 20 characters instruct it to go to the next line using \n ?

    Thank you for you reply.
    I will

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    how does one print out one char at a time, then when count reached issue a new line command? food for thought.
    Code:
    count = 0;
    while (true){
    
    print one char at a time
    cont++;
    if count == 20
    print new line
    }
    repeat

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Milo7 View Post
    I see how you are using modulo to get a value up until 20, although how would you use 'printf' to print 20 characters and only after 20 characters instruct it to go to the next line using \n ?
    I see you are confused so let me just say that you don't have to add a newline every time you use printf. When you don't, the next character will still get printed on the same line as the last. For example, the following code will print all (printable) ascii characters on the same line. What do you need to add to make it print only 20 characters per line?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i;
        for (i = 0; i < 94; i++) {
            // Don't mind the way I print the characters here if you can't understand it, did it this way to make it easier adding your solution
            putchar(i+33); // Same as 'printf("%c", i+33);'
        }
    
        return 0;
    }
    Last edited by GReaper; 09-22-2017 at 07:02 AM.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    I think I've got it.
    thank you very much.

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    5
    this would be the final code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
      int  count = 33;
    while(count<255){
    
    
    printf("%c ",count);
    
    
    count++;
    if ((count-33) % 20 == 0){
    
    
    printf("\n");
    
    
    }
    //count++;
    
    
    }
    return 0;
    }

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Milo7 View Post
    this would be the final code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
      int  count = 33;
    while(count<255){
    
    
    printf("%c ",count);
    
    
    count++;
    if ((count-33) % 20 == 0){
    
    
    printf("\n");
    
    
    }
    //count++;
    
    
    }
    return 0;
    }
    what are you printing out again? number off of your count or ASCII table?

    %c prints out what data type, or what is printf looking for when using %c
    and how is 'int count' the ASCII table?
    that count looks funny too.

    you're mixing between me and GReaper . where GReaper did the same thing but used a different method. where neither did the complete code for you. only showed you examples to keep and/or check count to get a new line, and that was it.

    I think, you need to learn the basics a little more before trying hairball stuff like that, But, sometimes trying hairball stuff like that helps to learn the basics better, so it's give and take,or live and learn.

    OOPs, I stand corrected: I just look up how to print out ascii
    The %c is the format string for a single character, and %d for a digit/integer. By casting the char to an integer, you'll get the ascii value. To print all the ascii values from 0 to 255 using while loop. Chars within single quote ('XXXXXX'), when printed as decimal should output its ASCII value.
    so I'd be something like this;
    Code:
    int count = 0, check = 0;
    char something;
    while ( count < 255)
    {
    
    printf("%d ", (int)something);
    
    if ( check == 20)
    {
         printf("\n");
        check = 0;
    }
    check++;
    count++;
    }
    BUT : BIG BUT, that is NOT using the criteria for the assignment, which is using modular division

    and you need to make that something chars within the ASCII table so it can print out its numeric values.
    Last edited by userxbw; 09-22-2017 at 10:12 AM.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I think Milo7 nailed it( ignoring their non-existent indentation ). The solution they implemented works just fine, although I would go for "count < 127" because official ascii values are from 0 to 128, not 256.
    Last edited by GReaper; 09-22-2017 at 10:16 AM.
    Devoted my life to programming...

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GReaper View Post
    I think Milo7 nailed it( ignoring their non-existent indentation ). The solution they implemented works just fine, although I would go for "count < 127" because official ascii values are from 0 to 128, not 256.
    his code gets this:
    Code:
    userx@~/bin <> ./a.out
    ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 
    5 6 7 8 9 : ; < = > ? @ A B C D E F G H 
    I J K L M N O P Q R S T U V W X Y Z [ \ 
    ] ^ _ ` a b c d e f g h i j k l m n o p 
    q r s t u v w x y z { | } ~  €  ‚ ƒ „ 
    … † ‡ ˆ ‰ Š ‹ Œ  Ž   ‘ ’ “ ” • – — ˜ 
    ™ š › œ  ž Ÿ               
    ..                    
                        
                        
                        
      userx@~/bin <>
    as I thought it was the other way around, using the alphabet to get the numeric values printed out not using he numeric values to get the chars printed out.
    where this
    Code:
    int c = 0;
     char ascc [6] = { 'a' , 'A', 'b', 'B', 'c', 'C'};
    
    while ( c < 6)
    {
        printf("%d ", (int)ascc[c]);
        c++;
    }
    prints this:
    Code:
    97 65 98 66 99 67
    lower 'a' is 97
    Cap 'A' is 65

    table reference
    Reference: Nonprintable and Printable ASCII Characters - Technical Documentation - Support - Juniper Networks
    Last edited by userxbw; 09-22-2017 at 10:41 AM.

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    ASCII is defined for characters 0 through 127. ASCII is a 7-bit standard, so no characters exist outside of that range. Anything or anyone that claims otherwise is uniformed.

    How characters outside of that range are displayed depend on the character encoding setting of your system. My system is configured as UTF-8, and each byte value above 127 is printed as a question mark inside a box. Your system appears to be configured to use the ISO-8859-1 character encoding or a similar encoding. That is an 8-bit character encoding that defines codes 128 through 255. The lower 128 characters are identical to ASCII (same as UTF-8), so it's "backward compatible" with ASCII.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by christop View Post
    ASCII is defined for characters 0 through 127. ASCII is a 7-bit standard, so no characters exist outside of that range. Anything or anyone that claims otherwise is uniformed.

    How characters outside of that range are displayed depend on the character encoding setting of your system. My system is configured as UTF-8, and each byte value above 127 is printed as a question mark inside a box. Your system appears to be configured to use the ISO-8859-1 character encoding or a similar encoding. That is an 8-bit character encoding that defines codes 128 through 255. The lower 128 characters are identical to ASCII (same as UTF-8), so it's "backward compatible" with ASCII.
    so that is why I get them little '?' sometimes, and yes I just reinstalled (Linux) and have not changed it to UTF-8 (yet) ...it is still set to just en_US. I might as well do it right now, Now that you bring it up. because I have ran into a few probs and had to have it UTF-8 ... so thanks for that sharing.
    Last edited by userxbw; 09-22-2017 at 12:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2017, 08:05 PM
  2. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  3. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  4. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  5. Replies: 23
    Last Post: 04-05-2011, 03:40 PM

Tags for this Thread