CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2013
    Posts
    16

    Question 3-dimensional arrays

    Hi. I receive unexpected outcome in my programm. Instead of geting all combinations of 0, 1, 2 (i.e. 000, 001, ..., 222) I get only 000 001 002 010 011 012. Can somebody tell me why?
    The idea of the progarmm is to create a crystal lattice. Each atom of the lattice has 3 coordinates (x, y, z). That's why I create class Atom. Then I create 3-dim array of the type derived from class Atom. Now each element of the class will represent an atom. If somebody has an idea how to implement it in a more sophisticated way, I will appreciate it. Thanks in advance!
    Code:
    #include <iostream>
     
    using namespace std;
     
    class Atom
    {
        public:
     
        float x, y, z;
    };
     
    typedef class Atom AtomType;
     
    int main ()
    {
        float a=1; // lattice parameter
        int Lx=2, Ly=2, Lz=2; // number of translated lattices along each axis
     
        AtomType ***Atom1; // 3-dimensional dynamic array for atom type 1
     
        Atom1 = new AtomType** [Lx];
     
        for (int i=0; i<Lx; i=i+a) // start of for loop
        {
            Atom1[i] = new AtomType* [Ly];
        } // end of for loop
        for (int i=0; i<Lx; i=i+a) // start of the outer for loop
        {
            for (int j=0; j<Ly; j=j+a)
            {
                Atom1[i][j] = new AtomType [Lz];
            }
        } // end of the outer for loop
    cout << "Atom1:" << endl;
        for (int i=0; i<=Lx; i++) // start of the 3 nested for loops to populate atoms of type 1
        {
            for (int j=0; j<=Ly; j++)
            {
                for (int k=0; k<=Lz; k++)
                {
                    Atom1[i][j][k].x = i*a;
                    Atom1[i][j][k].y = j*a;
                    Atom1[i][j][k].z = k*a;
     
                    cout << Atom1[i][j][k].x << "   " << Atom1[i][j][k].y << "   " << Atom1[i][j][k].z << endl;
                }
            }
        } // end of the 3 nested for loops to populate atoms of type 1
     
     
        for (int i=0; i<=Lx; i++) // start deleting array Atom1
        {
            for (int j=0; j<=Ly; j++)
            {
                delete[] Atom1[i][j];
            }
        }
        for (int i=0; i<=Lx; i++)
        {
            delete[] Atom1[i];
        }
        delete[] Atom1; // end of deleting array Atom1
        
        return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: 3-dimensional arrays

    Code:
    for (int i=0; i<=Lx; i++) // start of the 3 nested for loops to populate atoms of type 1
        {
            for (int j=0; j<=Ly; j++)
            {
                for (int k=0; k<=Lz; k++)
    Array subscripts start at 0 and end at one less than the number of elements in that dimension. So the loops should start at 0 and terminate when i < Lx etc. You have the same problem when you are deleting the array. When I tried your code I got an exception.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2013
    Posts
    16

    Re: 3-dimensional arrays

    Thank you very much. You are absolutely right.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured