Thread: Character out of range

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Character out of range

    Hi guys,

    I am having headache over this problem. I needed to randomise the position of different pairs of characters. The 2D array I chose to use is 6 x 6 which is 36 in total. This means that i only needed to use 18 pairs of characters. I chose to use character 'a' to 'r', between interger value of 97 and 114, which the range of them is 18. But when I print the result characters such as 'z' or 'y' which should'nt be in the range came out as well. By the way, when I print the result nomatter what type of variable I set it as it still came out as characters. I doesn't understand quite much about these ASCII codes.

    Any help or explaination is appreciated.
    Thanks
    Last edited by Brian90; 11-15-2010 at 07:09 AM. Reason: Prevent plagiarism

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Is arr[] static or initialized to zero somewhere before this snippet? Otherwise it contains garbage that you shouldn't be comparing against.

    Furthermore, this code will only set values for 18 out of 36 elements in arr[].

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Sorry I forgot to mention that. Yes I did initialised the whole array to 0 before comparing it. I had put a for loop before the randomisation of position which should be running it the second time.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    My mistake, I missed the top for loop.

    Could you show us how you print out the values? I put your code in a complete program, and the output appears correct to me.
    Code:
    f  o  b  n  q  p
    j  k  k  r  g  i
    r  l  h  l  j  a
    h  c  f  a  q  m
    m  n  p  b  d  c
    i  g  d  e  e  o

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Its ok. Here is how I print it :

    Code:
    for(i=0; i<6; i++)
        {
           for(j=0; j<6; j++)
                printf("%c ", arr[i][j]);
                printf("\n");
         }
    Btw, even if I put %i, it still shows the result as characters.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Btw, even if I put %i, it still shows the result as characters.
    Maybe you forgot to compile after making changes.?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    I did compliled it before running it..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  5. Character problem!!
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 11:51 PM