3 dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • desktop

    3 dimensional array

    I am simulating a display that consists of 20x20 pixels. All pixels are
    per default white but I would like to be able to turn on some or all the
    pixels.

    My idea was to create a 3 dimensional array:

    in a[][][]

    where the first index is the potential numbers of pixels that I would
    like to paint (20x20). The following two index should be the coordinates
    for the pixels that should be painted. If I want to paint 3 pixels
    (2,4), (17,9), (10,3) the following should be made:

    a[0][2][4];
    a[1][17][9];
    a[2][10][3];

    But when I declare a 3-dim array:

    a[][][];


    I get:

    error: declaration of ‘a’ as multidimensiona l array must have bounds for
    all dimensions except the first

    I have then tried:

    a[20*20][20*20][20*20];

    But is there not some way to accomplish my above 3 pixels without making
    such a huge array?
  • Victor Bazarov

    #2
    Re: 3 dimensional array

    desktop wrote:
    I am simulating a display that consists of 20x20 pixels. All pixels
    are per default white but I would like to be able to turn on some or
    all the pixels.
    By "turn on" you mean make them black?
    >
    My idea was to create a 3 dimensional array:
    >
    in a[][][]
    I believe you meant

    int a[][][]
    where the first index is the potential numbers of pixels that I would
    like to paint (20x20). The following two index should be the
    coordinates for the pixels that should be painted. If I want to paint
    3 pixels (2,4), (17,9), (10,3) the following should be made:
    >
    a[0][2][4];
    a[1][17][9];
    a[2][10][3];
    What's the meaning of [2]? What does it mean "shoudl be made"?
    But when I declare a 3-dim array:
    >
    a[][][];
    >
    >
    I get:
    >
    error: declaration of ‘a’ as multidimensiona l array must have bounds
    for all dimensions except the first
    Yes. Any array needs its size defined.
    >
    I have then tried:
    >
    a[20*20][20*20][20*20];
    >
    But is there not some way to accomplish my above 3 pixels without
    making such a huge array?
    Each pixel can be modelled by a single element in the array. That
    means you need

    char a[20][20] = {}; // initially zero

    and if you want to set certain pixels to something, do

    a[2][4] = 1;
    a[17][9] = 1;
    a[10][3] = 1;

    (or any other non-zero value). You could even use 'bool', but then
    if you suddenly want 255 levels of gray, you'd have to rewrite it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • desktop

      #3
      Re: 3 dimensional array

      Victor Bazarov wrote:
      desktop wrote:
      >I am simulating a display that consists of 20x20 pixels. All pixels
      >are per default white but I would like to be able to turn on some or
      >all the pixels.
      >
      By "turn on" you mean make them black?
      >
      >My idea was to create a 3 dimensional array:
      >>
      >in a[][][]
      >
      I believe you meant
      >
      int a[][][]
      >
      >where the first index is the potential numbers of pixels that I would
      >like to paint (20x20). The following two index should be the
      >coordinates for the pixels that should be painted. If I want to paint
      >3 pixels (2,4), (17,9), (10,3) the following should be made:
      >>
      >a[0][2][4];
      >a[1][17][9];
      >a[2][10][3];
      >
      What's the meaning of [2]? What does it mean "shoudl be made"?
      >
      >But when I declare a 3-dim array:
      >>
      >a[][][];
      >>
      >>
      >I get:
      >>
      >error: declaration of ‘a’ as multidimensiona l array must have bounds
      >for all dimensions except the first
      >
      Yes. Any array needs its size defined.
      >
      >I have then tried:
      >>
      >a[20*20][20*20][20*20];
      >>
      >But is there not some way to accomplish my above 3 pixels without
      >making such a huge array?
      >
      Each pixel can be modelled by a single element in the array. That
      means you need
      >
      char a[20][20] = {}; // initially zero
      >
      and if you want to set certain pixels to something, do
      >
      a[2][4] = 1;
      a[17][9] = 1;
      a[10][3] = 1;
      >
      (or any other non-zero value). You could even use 'bool', but then
      if you suddenly want 255 levels of gray, you'd have to rewrite it.
      >
      V
      But now I have to iterate through the whole matrix with a nested for
      loop to print/draw only 3 pixels.

      Instead I would like to iterate an array corresponding to the number of
      pixels that I want to draw (in this case 3) and for each index extract
      the coordinate.

      I was thinking of making a vector consisting of std::pair's instead
      since the idea of double or triple indexing a binary value seems to
      expensive (later I want to extend the window to 1024x768).

      Comment

      • Mark P

        #4
        Re: 3 dimensional array

        desktop wrote:
        Victor Bazarov wrote:
        >desktop wrote:
        >>I am simulating a display that consists of 20x20 pixels. All pixels
        >>are per default white but I would like to be able to turn on some or
        >>all the pixels.
        >>
        >By "turn on" you mean make them black?
        >>
        >>My idea was to create a 3 dimensional array:
        >>>
        >>in a[][][]
        >>
        >I believe you meant
        >>
        > int a[][][]
        >>
        >>where the first index is the potential numbers of pixels that I would
        >>like to paint (20x20). The following two index should be the
        >>coordinates for the pixels that should be painted. If I want to paint
        >>3 pixels (2,4), (17,9), (10,3) the following should be made:
        >>>
        >>a[0][2][4];
        >>a[1][17][9];
        >>a[2][10][3];
        >>
        >What's the meaning of [2]? What does it mean "shoudl be made"?
        >>
        >>But when I declare a 3-dim array:
        >>>
        >>a[][][];
        >>>
        >>>
        >>I get:
        >>>
        >>error: declaration of ‘a’ as multidimensiona l array must have bounds
        >>for all dimensions except the first
        >>
        >Yes. Any array needs its size defined.
        >>
        >>I have then tried:
        >>>
        >>a[20*20][20*20][20*20];
        >>>
        >>But is there not some way to accomplish my above 3 pixels without
        >>making such a huge array?
        >>
        >Each pixel can be modelled by a single element in the array. That
        >means you need
        >>
        > char a[20][20] = {}; // initially zero
        >>
        >and if you want to set certain pixels to something, do
        >>
        > a[2][4] = 1;
        > a[17][9] = 1;
        > a[10][3] = 1;
        >>
        >(or any other non-zero value). You could even use 'bool', but then
        >if you suddenly want 255 levels of gray, you'd have to rewrite it.
        >>
        >V
        >
        But now I have to iterate through the whole matrix with a nested for
        loop to print/draw only 3 pixels.
        >
        Instead I would like to iterate an array corresponding to the number of
        pixels that I want to draw (in this case 3) and for each index extract
        the coordinate.
        >
        I was thinking of making a vector consisting of std::pair's instead
        since the idea of double or triple indexing a binary value seems to
        expensive (later I want to extend the window to 1024x768).
        This seems to be a data structure problem. You have two basic choices
        here: 1) make a 2D array where each element corresponds to a single
        pixel; 2) make a list where each element is a single pixel that is
        turned on. Each has its pros and cons-- an array allows you to quickly
        determine if a *particular* pixel is turned on, a list allows you to
        quickly iterate over *all* pixels that are turned on. Without knowing
        what you intend to do, we can't recommend one over the other.

        FWIW, array vs. list is a ubiquitous issue in all sorts of programming
        problems. Weigh the pros and cons and see which better suits your
        needs, or make your own structure if neither is sufficient.

        Comment

        • desktop

          #5
          Re: 3 dimensional array

          Mark P wrote:
          desktop wrote:
          >Victor Bazarov wrote:
          >>desktop wrote:
          >>>I am simulating a display that consists of 20x20 pixels. All pixels
          >>>are per default white but I would like to be able to turn on some or
          >>>all the pixels.
          >>>
          >>By "turn on" you mean make them black?
          >>>
          >>>My idea was to create a 3 dimensional array:
          >>>>
          >>>in a[][][]
          >>>
          >>I believe you meant
          >>>
          >> int a[][][]
          >>>
          >>>where the first index is the potential numbers of pixels that I would
          >>>like to paint (20x20). The following two index should be the
          >>>coordinate s for the pixels that should be painted. If I want to paint
          >>>3 pixels (2,4), (17,9), (10,3) the following should be made:
          >>>>
          >>>a[0][2][4];
          >>>a[1][17][9];
          >>>a[2][10][3];
          >>>
          >>What's the meaning of [2]? What does it mean "shoudl be made"?
          >>>
          >>>But when I declare a 3-dim array:
          >>>>
          >>>a[][][];
          >>>>
          >>>>
          >>>I get:
          >>>>
          >>>error: declaration of ‘a’ as multidimensiona l array must have bounds
          >>>for all dimensions except the first
          >>>
          >>Yes. Any array needs its size defined.
          >>>
          >>>I have then tried:
          >>>>
          >>>a[20*20][20*20][20*20];
          >>>>
          >>>But is there not some way to accomplish my above 3 pixels without
          >>>making such a huge array?
          >>>
          >>Each pixel can be modelled by a single element in the array. That
          >>means you need
          >>>
          >> char a[20][20] = {}; // initially zero
          >>>
          >>and if you want to set certain pixels to something, do
          >>>
          >> a[2][4] = 1;
          >> a[17][9] = 1;
          >> a[10][3] = 1;
          >>>
          >>(or any other non-zero value). You could even use 'bool', but then
          >>if you suddenly want 255 levels of gray, you'd have to rewrite it.
          >>>
          >>V
          >>
          >But now I have to iterate through the whole matrix with a nested for
          >loop to print/draw only 3 pixels.
          >>
          >Instead I would like to iterate an array corresponding to the number
          >of pixels that I want to draw (in this case 3) and for each index
          >extract the coordinate.
          >>
          >I was thinking of making a vector consisting of std::pair's instead
          >since the idea of double or triple indexing a binary value seems to
          >expensive (later I want to extend the window to 1024x768).
          >
          This seems to be a data structure problem. You have two basic choices
          here: 1) make a 2D array where each element corresponds to a single
          pixel; 2) make a list where each element is a single pixel that is
          turned on. Each has its pros and cons-- an array allows you to quickly
          determine if a *particular* pixel is turned on, a list allows you to
          quickly iterate over *all* pixels that are turned on. Without knowing
          what you intend to do, we can't recommend one over the other.
          >
          FWIW, array vs. list is a ubiquitous issue in all sorts of programming
          problems. Weigh the pros and cons and see which better suits your
          needs, or make your own structure if neither is sufficient.
          I think I have found a solution. I just make two global int arrays and a
          global pointer:

          int x[20*20];
          int y[20*20];
          int counter = 0;

          Each time I want to store a pixel I just do the following:

          x[counter] = x_coord;
          y[counter] = y_coord;
          counter++;

          I can then read the coordinates from the two arrays afterwards...se ems
          pretty simple and efficient.

          Comment

          • Mark P

            #6
            Re: 3 dimensional array

            desktop wrote:
            Mark P wrote:
            >desktop wrote:
            >>Victor Bazarov wrote:
            >>>desktop wrote:
            >>>>I am simulating a display that consists of 20x20 pixels. All pixels
            >>>>are per default white but I would like to be able to turn on some or
            >>>>all the pixels.
            >>>>
            >>>By "turn on" you mean make them black?
            >>>>
            >>>>My idea was to create a 3 dimensional array:
            >>>>>
            >>>>in a[][][]
            >>>>
            >>>I believe you meant
            >>>>
            >>> int a[][][]
            >>>>
            >>>>where the first index is the potential numbers of pixels that I would
            >>>>like to paint (20x20). The following two index should be the
            >>>>coordinat es for the pixels that should be painted. If I want to paint
            >>>>3 pixels (2,4), (17,9), (10,3) the following should be made:
            >>>>>
            >>>>a[0][2][4];
            >>>>a[1][17][9];
            >>>>a[2][10][3];
            >>>>
            >>>What's the meaning of [2]? What does it mean "shoudl be made"?
            >>>>
            >>>>But when I declare a 3-dim array:
            >>>>>
            >>>>a[][][];
            >>>>>
            >>>>>
            >>>>I get:
            >>>>>
            >>>>error: declaration of ‘a’ as multidimensiona l array must have bounds
            >>>>for all dimensions except the first
            >>>>
            >>>Yes. Any array needs its size defined.
            >>>>
            >>>>I have then tried:
            >>>>>
            >>>>a[20*20][20*20][20*20];
            >>>>>
            >>>>But is there not some way to accomplish my above 3 pixels without
            >>>>making such a huge array?
            >>>>
            >>>Each pixel can be modelled by a single element in the array. That
            >>>means you need
            >>>>
            >>> char a[20][20] = {}; // initially zero
            >>>>
            >>>and if you want to set certain pixels to something, do
            >>>>
            >>> a[2][4] = 1;
            >>> a[17][9] = 1;
            >>> a[10][3] = 1;
            >>>>
            >>>(or any other non-zero value). You could even use 'bool', but then
            >>>if you suddenly want 255 levels of gray, you'd have to rewrite it.
            >>>>
            >>>V
            >>>
            >>But now I have to iterate through the whole matrix with a nested for
            >>loop to print/draw only 3 pixels.
            >>>
            >>Instead I would like to iterate an array corresponding to the number
            >>of pixels that I want to draw (in this case 3) and for each index
            >>extract the coordinate.
            >>>
            >>I was thinking of making a vector consisting of std::pair's instead
            >>since the idea of double or triple indexing a binary value seems to
            >>expensive (later I want to extend the window to 1024x768).
            >>
            >This seems to be a data structure problem. You have two basic choices
            >here: 1) make a 2D array where each element corresponds to a single
            >pixel; 2) make a list where each element is a single pixel that is
            >turned on. Each has its pros and cons-- an array allows you to
            >quickly determine if a *particular* pixel is turned on, a list allows
            >you to quickly iterate over *all* pixels that are turned on. Without
            >knowing what you intend to do, we can't recommend one over the other.
            >>
            >FWIW, array vs. list is a ubiquitous issue in all sorts of programming
            >problems. Weigh the pros and cons and see which better suits your
            >needs, or make your own structure if neither is sufficient.
            >
            I think I have found a solution. I just make two global int arrays and a
            global pointer:
            >
            int x[20*20];
            int y[20*20];
            int counter = 0;
            >
            Each time I want to store a pixel I just do the following:
            >
            x[counter] = x_coord;
            y[counter] = y_coord;
            counter++;
            >
            I can then read the coordinates from the two arrays afterwards...se ems
            pretty simple and efficient.
            But not very good, I would say. You're taking a fundamental piece of
            data, an (x,y) coordinate, and dividing it into two separate data
            structures connected by an essentially meaningless counter variable.

            typedef std::pair<int,i ntPoint;
            std::list<Point allPoints;

            or

            std::vector<Poi ntallPoints;

            or even

            std::set<Pointa llPoints;

            makes a lot more sense to me.

            Comment

            • Jim Langston

              #7
              Re: 3 dimensional array

              "desktop" <asdfsf@asd.com wrote in message
              news:fcplgr$nfo $1@news.net.uni-c.dk...
              >I am simulating a display that consists of 20x20 pixels. All pixels are per
              >default white but I would like to be able to turn on some or all the
              >pixels.
              >
              My idea was to create a 3 dimensional array:
              >
              in a[][][]
              >
              where the first index is the potential numbers of pixels that I would like
              to paint (20x20). The following two index should be the coordinates for
              the pixels that should be painted. If I want to paint 3 pixels (2,4),
              (17,9), (10,3) the following should be made:
              >
              a[0][2][4];
              a[1][17][9];
              a[2][10][3];
              >
              But when I declare a 3-dim array:
              >
              a[][][];
              >
              >
              I get:
              >
              error: declaration of ‘a’ as multidimensiona l array must have bounds for
              all dimensions except the first
              >
              I have then tried:
              >
              a[20*20][20*20][20*20];
              >
              But is there not some way to accomplish my above 3 pixels without making
              such a huge array?
              I don't understand why you are trying to make such a huge array and what the
              first element will be used for. An array of 20x20 elements would be:
              int a[20][20];
              each pixel containing an int. You could set that int to some value to
              indicate if it's on or off or whatever. I would initialize them all to 0
              first with:
              int a[20][20] = {0};

              Now if I wanted to paint (2,4) (17,9) (10,3) I would do something like:
              a[2][4] = 1;
              a[17][9] = 1;
              a[10][3] = 1;
              or whatever value I wanted for "on".

              Now, please explain what the 1st element you used is, why would one pixel
              need more than one int value?

              An array
              int a[20*20][20*20][20*20]
              is acutally quite huge and you wouldn't use most of the elements as I see
              it.

              Either you don't have a clear understanding of arrays, or I dont' have a
              clear understanding of what you are trying to achieve.


              Comment

              • Jerry Coffin

                #8
                Re: 3 dimensional array

                In article <fcplgr$nfo$1@n ews.net.uni-c.dk>, asdfsf@asd.com says...
                I am simulating a display that consists of 20x20 pixels. All pixels are
                per default white but I would like to be able to turn on some or all the
                pixels.
                It sounds to me like you really want some separate data structures. One
                is the simulated screen:

                bool screen[20][20]; // Apparently you only need black and white

                The second is a set of coordinates for a single pixel:

                struct point {
                unsigned char x;
                unsigned char y;
                };

                and the third is a vector of pixels that you want to display:

                point display_list[] = {
                { 2, 4},
                { 17, 9 },
                { 10, 3 }
                };

                Then you can display those pixels pretty easily with code that steps
                through the list, and for each pixel in the list, sets a pixel in the
                simulated screen.

                --
                Later,
                Jerry.

                The universe is a figment of its own imagination.

                Comment

                • BobR

                  #9
                  Re: 3 dimensional array


                  Jerry Coffin wrote in message...
                  In article <fcplgr$nfo$1@n ews.net.uni-c.dk>, asdfsf@asd.com says...
                  I am simulating a display that consists of 20x20 pixels. All pixels are
                  per default white but I would like to be able to turn on some or all the
                  pixels.
                  >
                  It sounds to me like you really want some separate data structures. One
                  is the simulated screen:
                  >
                  bool screen[20][20]; // Apparently you only need black and white
                  >
                  The second is a set of coordinates for a single pixel:
                  >
                  struct point
                  unsigned char x;
                  unsigned char y;
                  };
                  >
                  and the third is a vector of pixels that you want to display:
                  >
                  point display_list[] = {
                  { 2, 4},
                  { 17, 9 },
                  { 10, 3 }
                  };
                  >
                  Then you can display those pixels pretty easily with code that steps
                  through the list, and for each pixel in the list, sets a pixel in the
                  simulated screen.
                  Or, maybe (assuming black-n-white):

                  size_t const Sz(20);
                  std::vector<std ::bitset<Sz Screen(Sz);

                  Screen.at( 2 ).set( 4 );
                  Screen[ 17 ].set( 9 );
                  Screen[ 10 ][ 3 ] = 1;
                  int pix = Screen[ 10 ][ 3 ];
                  Screen[ 10 ][ 2 ] = pix;
                  Screen.at( 0 ).set(); // turn on whole row
                  Screen.at( 0 ).reset(); // turn off whole row
                  Screen.at( 17 ).flip();
                  .....etc..
                  // 'print' the screen
                  for( size_t i(0); i < Screen.size(); ++i ){
                  std::cout<<Scre en[ i ]<<std::endl;
                  }// for(i)

                  Only 'catch' is LSB is on right, MSB is on left in the bitset (opposite of
                  an array).

                  --
                  Bob R
                  POVrookie


                  Comment

                  Working...