multidimentional character arrays

This is a discussion on multidimentional character arrays within the C++ Programming forums, part of the General Programming Boards category; im working on a program that has me stuck. im trying to form a grid thats 10 by 10. i ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    12

    multidimentional character arrays

    im working on a program that has me stuck. im trying to form a grid thats 10 by 10. i want it to display the numbers from one to 100. i want to b able to insert a letter in place of a number later and move it around the board. i cant figure out how to get a character matrix. any ideas?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    char matrix[10][10];
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    12
    i have that part, that wasnt the problem, how do i go about filling it?

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    How about a loop of assignment statements?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    12
    Code:
    for (r=1; r<=10; r++)
    {
       for (c=1; c<=10; c++)
       {
          board[ROWS][COLS];
       }
       for (c=10; c>=1; c--)
      {
        board[ROWS][COLS];
    						
    				
       }			
    }
    this is the code that i have so far. i have a separate set of loops to display it. i know im not filling it right. thats what i cant get. i need to fill my matrix with the numbers 1-100 that reverses ever other line like this:

    100 99 98 97 96 95 94 93 92 91

    81 82 83 84 85 86 87 88 89 90

    80 79 78 77 76 75 74 73 72 71

    61 62 63 64 65 66 67 68 69 70

    60 59 58 57 56 55 54 53 52 51

    41 42 43 44 45 46 47 48 49 50

    40 39 38 37 36 35 34 33 32 31

    21 22 23 24 25 26 27 28 29 30

    20 19 18 17 16 15 14 13 12 11

    1 2 3 4 5 6 7 8 9 10
    i noticed that the numbers come up all weird, i wanted them lined up in a box. i can fill the board if its an int but i cant switch over to a char. i thank you for any help you can give me
    Last edited by ckeener; 03-11-2005 at 06:58 AM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you want to be able to do something like this:1 2 c 4 5

    and change it to something like this

    1 2 3 4 c

    then you will probably want to consider a multidimensional array (or multidimensional vector) of strings because digits can be ints or char but ints can be a single digit or a series of digits (a series of digits would be equivalent to a string rather than a single char). Using either a three dimensional array (vector) of char (using C style strings) or a two dimensional array (vector) of C++ STL strings would be possible if you wanted to do this. You might be able to use a multidimensional array of single chars if you display the character set integer representation of the char at a given point rather than display the pictoral representation of the char itself (using a cast to int????), but to me that's even more confusing. A third alternative might be to have an array of unions rather than an array of simple int or char or strings, but I've never done that, so I don't even know if it's possible or not. A fourth alternative, and the approach I'd probably consider, would be to use a multidimensional array of struct/classes that contain two data members, one an int and the a char. If the instance being displayed has a valid int but not a valid char, then display the int and if it doesn't have an valid int, then it must have a valid char, so display the char.

    Given different widths of char/ints in different fonts it's not a trivial task to get perfect alignment of tables on a visual display device, but it's not rocket science either. It involves a lot of tinkering with formatting output (with C style format strings or C++ ostream manipulators/etc.) to achieve the visual effect desired. For example, 100 has three digits, but 9 has only one. Therefore to line them up you would need to have each int consist of three char digits (char?) and pad the output of 9 with a leading space (or two?) and then fill the remaining (if any) digits with spaces. I have included a simple sample using * instead of spaces that I hope will allow you to better visualize the approach:

    100 *99 *88
    *9* *8* *7*
    You're only born perfect.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > for (r=1; r<=10; r++)
    Arrays start at 0, not 1
    So it would be for (r=0; r<10; r++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    12
    thanks for all the input, ill have to give it try

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 06:54 PM
  4. Spaces in Character Arrays
    By ADLOTS in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 03:24 AM
  5. help with character arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21