nebie simply needs help creating 2 dimensional char array

This is a discussion on nebie simply needs help creating 2 dimensional char array within the C++ Programming forums, part of the General Programming Boards category; i assumed in c++ it was supposed to be typed; Code: char table[3][3][3] = {'a','b','c'}{'d','e','f'}{'g','h','i'}; i looked for examples online ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    nebie simply needs help creating 2 dimensional char array

    i assumed in c++ it was supposed to be typed;


    Code:
    char table[3][3][3] = {'a','b','c'}{'d','e','f'}{'g','h','i'};

    i looked for examples online but couldn't find any. thanks for any help given.
    also how would i type it if i wanted all the var in the array to be spaces. ex: " "

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    char table[3][3] = {{'a','b','c'},{'d','e','f'},{'g','h','i'}};
    char table[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Two problems. First, you want a two dimensional array, but you specified three dimensions. Second, you need to add curly braces around the outer dimension and commas between each element of the outer dimension. The elements of the outer dimension are the braced set of data for the inner dimension.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    thanks you!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,932
    You might also see such aggregates being initialised with the inner braces removed:
    Code:
    char table[3][3] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
    In this case it has the same effect as in Daved's example, but I would suggest following Daved's example to make things clearer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    thanks for the help guys.. some simple things are really hard for me to find online. How would i go about printing the whole array? would i have to use a loop? or could i do it with one line of code? sorry for being such a newbie

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,932
    How would i go about printing the whole array? would i have to use a loop?
    Yes.

    or could i do it with one line of code?
    Also yes, but try using a loop first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Usually two dimensional array's are printed using a nested for loop
    I'm just trying to be a better person - My Name Is Earl

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    my regular way of printing two dimensional array is as follows

    Code:
    main()
    {
    
    int example[10][10];
    int down = 0, across = 0;
    
    while(down < 10)
    {
         
         while(across < 10)
          {
               example[down][across] = across;
               printf("%d ", example[down][across]);  
               across++;
           }
    across = 0;                                                        /*set across back to start */
    down++;                                                            /* down a row */
    printf("\n");                                                       /* newline for neat grid printout*/
    }
    
    down = 0;
    
    return (0);
    }
    the WHILE loop is useful here rather than FOR loop as it lets you control when the counters are incremented, you could have a condition in the loop that only increases 'across' when the condition is met, if not it will just loop until the right condition occurs thus ensuring you only collect and print 10 of the values you want.

  10. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by rogster001 View Post
    my regular way of printing two dimensional array is as follows

    Code:
    main()
    {
    
    int example[10][10];
    int down = 0, across = 0;
    
    while(down < 10)
    {
         
         while(across < 10)
          {
               example[down][across] = across;
               printf("&#37;d ", example[down][across]);  
               across++;
           }
    across = 0;                                                        /*set across back to start */
    down++;                                                            /* down a row */
    printf("\n");                                                       /* newline for neat grid printout*/
    }
    
    down = 0;
    
    return (0);
    }
    the WHILE loop is useful here rather than FOR loop as it lets you control when the counters are incremented, you could have a condition in the loop that only increases 'across' when the condition is met, if not it will just loop until the right condition occurs thus ensuring you only collect and print 10 of the values you want.
    Why use a while loop? A for loop gets the job done and makes the code look alot more tidy.

    Code:
    const unsigned int TABSIZE = 10;
    char tab[TABSIZE][TABSIZE];
    unsigned int across = 0, down = 0;
    
    for(;down < TABSIZE; down++)
    {
         for(;across < TABSIZE; across++)
         {
              std::cout << tab[down][across] << " ";
         }
         across = 0;
         std::cout << std::endl;
    }
    
    return 0;
    Edit: Sorry, didn't notice the last part of your post, but if you just want to print a 2d array without any conditions, i think my example is prefferable. Also avoids the use of magic numbers and printf() in c++...
    Last edited by Neo1; 01-11-2008 at 09:10 AM.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Code:
    int main()
    {
    	int example[10][10];
    	int down = 0, across = 0;
    	while(down < 10)
    	{
    		while(across < 10)
    		{
    			example[down][across] = across;
    			cout << example[down][across];
    			across++;
    		}
    		across = 0;		/*set across back to start */
    		down++;		/* down a row */
    		cout << "\n";		/* newline for neat grid printout*/
    	}
    	
    	down = 0;
    	return (0);
    }
    This is pretty much C code because just main() is illegal in C++
    Also, indenting properly helps. Here, let me help clean it up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Hey i know, but i thought the original query was straightforward enough not to matter, but yeah, should have used pseudocode or summat, i really need to fire up the c++ compiler more often!

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rogster001 View Post
    my regular way of printing two dimensional array is as follows

    Code:
    main()
    {
    
    int example[10][10];
    int down = 0, across = 0;
    
    while(down < 10)
    {
         
         while(across < 10)
          {
               example[down][across] = across;
               printf("%d ", example[down][across]);  
               across++;
           }
    across = 0;                                                        /*set across back to start */
    down++;                                                            /* down a row */
    printf("\n");                                                       /* newline for neat grid printout*/
    }
    
    down = 0;
    
    return (0);
    }
    the WHILE loop is useful here rather than FOR loop as it lets you control when the counters are incremented, you could have a condition in the loop that only increases 'across' when the condition is met, if not it will just loop until the right condition occurs thus ensuring you only collect and print 10 of the values you want.
    The "ability to control where you increament the counter" and the NEED to do so should decide whether you use for or while. I'd go a step further than Elysia, and write the above code in the most obvious way:
    Code:
    int main()
    {
    
      int example[10][10];
      for (int down  = 0; down < 10; down++)
      {
        for(int across = 0; across < 10; across++)
        {
          example[down][across] = across;
          printf("%d ", example[down][across]);  
        }
        printf("\n"); /* newline for neat grid printout*/
      }
    
      return (0);
    }
    The goal should be to convey as much as possible where the loop is controlled how the loop works. By using a for-loop in this case, we see exactly that there's not "strange" goings on inside the loop.

    If you NEED to use a while-loop to do something with an array/matrix, it should be because you HAVE to, not "because I can easily change the place where I increment the index".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    The "ability to control where you increament the counter" and the NEED to do so should decide whether you use for or while.
    A good point, its just most of the time i use this its in cases where the control is needed and i forgot about the simpler approach. Also i suppose i wanted to highlight for a 'newbie' something that has often proved useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. initialize 2 dimensional char array
    By meka in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 01:13 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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