Thread: Converting Random Numbers into Chars

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    5

    Converting Random Numbers into Chars

    Hi,
    I am having difficulty completing 7th last line below I marked the line with an arrow. Please explain me what is required here.
    Thanks

    Code:
    int main(void) {
      int MaxNum,     /*number of random nos to generate     */
        i,          /*index                                */
        value,
        RandNo,     /*a random number                      */
        ExitCode = 0 ; /*value to exit program with           */
      //----------------Start below here. To do: approximate lines of code = 7
      // seed the random number generator with value 1 for repeatability
      srand(1);
      //set MaxNum to 3
      MaxNum=3;
      //for MaxNum times do the following
      for(i=1;i<=MaxNum;i++){
        //let RandNo be the next random number
        RandNo = rand();
        //print the random number in the format shown
        printf("Random No %d is %d",i,RandNo);
        //let value be the conversion of the random number to range 0 to 5, inclusive
        value = RandNo%6;
        printf("From zero to 5 that is %d\n", value);//
        //let value be the conversion of the random number to range 'a' to 'z', inclusive
    //--->    
        printf("From a to z that is %c\n", value);//
      //----------------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
        printf("-------------------------------\n") ;
      }
      exit(ExitCode);
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    ASCII values are numbers

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    For 'a', add 97 to 0.

    The most common way is to add 'a'

    Enjoy.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Sorry I don't understand can you explain in a bit more detail? please.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I'm having trouble explaining it without giving the answer away...

    Play around with this:

    Code:
    printf("\n\r>>%c", 0x61);
    
    printf("\n\r>>%c", 0x62);
    
    printf("\n\r>>%c", 0x63);
    You can see that the characters are created by the value entered. (Look at the ASCII table)

    If I wanted to get a random value between 5 and 15, I would get a random number mod 10 (that is 15-5), and then add 5 to the result.

    To get a random letter between 'a' (which has the hex value of 0x61) and 'z' (which has the hex value of 0x7A)...
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Ok I get it Thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. joining two chars together and converting to INT
    By tehprince in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 02:08 PM
  2. converting ints to chars
    By e66n06 in forum C Programming
    Replies: 4
    Last Post: 07-28-2007, 03:52 PM
  3. Converting Chars to Ints
    By sycorax in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2005, 10:40 PM
  4. Converting a Long to chars[4]
    By Russell in forum C++ Programming
    Replies: 15
    Last Post: 12-18-2002, 06:33 PM
  5. converting chars to ints
    By nebie in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2001, 11:33 AM