Thread: 3 Dimensional Array

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    36

    Lightbulb 3 Dimensional Array

    Can somebody give a good and simple example of a 3D Array displaying the provided values neatly on the console/screen so that one can easily understand how it is working?

    Example 1:

    Code:
    int array[2][2][2];
    Example 2:

    Code:
    int array[2][4][6];

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Pretty tough to show a good 3D array, with values, on a flat 2D computer screen, don't you think?

    What's your problem with the 3D array?

    You have rows, columns, and depth. Like a stack of graph papers on a table. They have rows, and columns, but they are a stack of papers, so they have depth, also.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Just show a series of tables.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A 2D bitmap + colour would be one way.

    Depends on what the 3rd dimension is measuring. Temperature is an obvious natural choice for displaying in colour. Though any linear measurement could be done the same way.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    36
    Well, I will come up with an example soon.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
         char array[9][10][20] =
         {
              {" ### ","    "," ### "," ### ","     "," ### "," ### "," ### "," ### "," ### "},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"     ","    "," ### "," ### "," ### "," ### "," ### ","     "," ### "," ### "},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {" ### ","    "," ### "," ### ","     "," ### "," ### ","     "," ### "," ### "},
         },
         *nr = argv[1];
         unsigned int i = 0, h = 0, idx = 0;
    
         if(argc > 2)
         {
             char dispChar = argv[2][0];
    
             for( i = 0; i < 9; i++)
             {
                 for(h = 0; h < 20; h++)
                 {
                    for(idx = 0; idx < strlen(array[i][h]); idx++)
                    {
                        if(array[i][h][idx] != ' ')
                            array[i][h][idx] = dispChar;
                    }
                 }
             }
    
         }
    
         for( i = 0; i < 9; i++)
         {
              for(h = 0; h < strlen(nr); h++)
                   if(nr[h] !=  ' ')
                        printf(" %s ", array[i][nr[h] - 0x30]);
    
              putchar('\n');
    
         }
    
    
         return 0;
    
    }
    3-dimensional array. What it does should be obvious, but how it does it might not be.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @Ice Dane - all I see are flat 2D digits. How do you get it to show depth?

    If you say it's 3D, you have to show that third dimension, somehow. This is a clever 2D program, but hasn't shown any third dimension, yet.

    If you'd like to show time, or any other dimension with this program except depth, please be my guest.
    Last edited by Adak; 12-16-2008 at 08:41 PM.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    As Salem already said, the 3rd dimension doesn't have to be z it can be anything you like, as can the first and second dimensions. No-one said we were talking about space :-)

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    36

    Thumbs up Digital Display of Numbers Provided as Command Prompt Arguments

    IceDane, your program is amazing!


    For the rest of the users here, I would like to make this note:


    Save this program (for example 3d.c) and after compiling go to command prompt and change drive/directory where the file is saved. Type 3d <any digits without any space in between> and press the enter/return key to see the result.

    The numbers we give as a command prompt argument are represented in a digital format as we see in a digital clock.


    Thanks buddy!
    Quote Originally Posted by IceDane View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
         char array[9][10][20] =
         {
              {" ### ","    "," ### "," ### ","     "," ### "," ### "," ### "," ### "," ### "},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"#   #","   #","    #","    #","#   #","#    ","#    ","    #","#   #","#   #"},
              {"     ","    "," ### "," ### "," ### "," ### "," ### ","     "," ### "," ### "},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {"#   #","   #","#    ","    #","    #","    #","#   #","    #","#   #","    #"},
              {" ### ","    "," ### "," ### ","     "," ### "," ### ","     "," ### "," ### "},
         },
         *nr = argv[1];
         unsigned int i = 0, h = 0, idx = 0;
    
         if(argc > 2)
         {
             char dispChar = argv[2][0];
    
             for( i = 0; i < 9; i++)
             {
                 for(h = 0; h < 20; h++)
                 {
                    for(idx = 0; idx < strlen(array[i][h]); idx++)
                    {
                        if(array[i][h][idx] != ' ')
                            array[i][h][idx] = dispChar;
                    }
                 }
             }
    
         }
    
         for( i = 0; i < 9; i++)
         {
              for(h = 0; h < strlen(nr); h++)
                   if(nr[h] !=  ' ')
                        printf(" %s ", array[i][nr[h] - 0x30]);
    
              putchar('\n');
    
         }
    
    
         return 0;
    
    }
    3-dimensional array. What it does should be obvious, but how it does it might not be.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, yeah! Got it. It's clever.

    It's not 3D, and doesn't give the illusion of being 3D.

    It is clever, though.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    ingenious indeed but a dimension short of three and as Salem and others have said before a dimension can be anything.
    a crude example maybe "the number of medals (tally) won by the top country (name) in the recent Olympics (year)..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM

Tags for this Thread