CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2008
    Posts
    6

    Resolved c++ How can I append 3 values at once to a 2d array?

    #ifndef __dot_h
    #define __dot_h
    #include <iostream>

    struct dots
    {
    private:

    dots();
    ~dots();
    float points[3][3];
    float mids[2][3];

    public:

    float x(int point);
    float y(int point);
    float z(int point);
    void Init_dots();
    void Add_dots(float x1,float y1, float z1);
    void Add_dots(float x1,float y1, float z1,float x2, float y2, float z2);
    void Print_dots();

    };

    dots:ots()
    {

    }

    dots::~dots()
    {
    delete points;
    delete mids;
    }

    float dots::x(int point)
    {
    return points[point-1][0];
    }

    float dots::y(int point)
    {
    return points[point-1][1];
    }

    float dots::z(int point)
    {
    return points[point-1][2];
    }

    void dots::Init_dots()
    {
    for(int i=0;i<((sizeof(points)/sizeof(float))-1)/3;i++)
    {
    for(int j=0;j<3;j++)
    {
    points[i][j]=i+j;
    }
    }
    }

    void dots::Add_dots(float x1,float y1, float z1)
    {
    points[(sizeof(points)/sizeof(float))/3]={x1,y1,z1};
    }

    void dots::Add_dots(float x1,float y1, float z1,float x2,float y2, float z2)
    {
    points[sizeof(points)/sizeof(float)]={x1,y1,z1};
    points[sizeof(points)/sizeof(float)]={x2,y2,z2};
    }

    void dots::Print_dots()
    {
    for(int p=0;p<=(sizeof(points)/sizeof(float))-1;p++)
    {
    cout << points[p];
    cout << "\n";
    }
    }



    #endif;

    The problem occurs in "Add_dots" fuction. I want to be able to append 3 vales to the 2d array all at once if this is possible. I am pretty new to programming so if you see any other errors/better ways of doing things I am open to suggestions. Thank you.
    Last edited by fishies; July 22nd, 2008 at 01:00 AM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: c++ How can I append 3 values at once to a 2d array?

    Okay, what *exactly* are you trying to do here?
    Code:
    points[sizeof(points)/sizeof(float)]={x1,y1,z1};
    I suggest never using sizeof() on an array type, because that'll get you into bad habits when you encounter dynamic arrays, first of all. Second, that index doesn't make any sense.

  3. #3
    Join Date
    Jul 2008
    Posts
    6

    Re: c++ How can I append 3 values at once to a 2d array?

    What I was trying to do is to append the three values to the end of the 2d array.

    ex:

    float points[3][3]={{x1,y1,z1},
    {x2,y2,z2},
    {x3,y3,z3}};

    //then append three values..

    points[3]={x4,y4,z4};

    Hopefully this clarifies my question a little.

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: c++ How can I append 3 values at once to a 2d array?

    Quote Originally Posted by fishies
    Hopefully this clarifies my question a little.
    It does. And the answer is simple: you can't do it (change the size of an array).
    You might want to use std::vector that allows dynamic resizing.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: c++ How can I append 3 values at once to a 2d array?

    Quote Originally Posted by fishies
    What I was trying to do is to append the three values to the end of the 2d array.
    You cannot do that -- arrays are fixed in size.

    Why not just make it float points[4][3]?

    If you want to add rows dynamically, use std::vector<std::vector<float> >.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: c++ How can I append 3 values at once to a 2d array?

    Quote Originally Posted by fishies
    What I was trying to do is to append the three values to the end of the 2d array.
    Also, it makes much more sense to have the 3 points in one structure:
    Code:
    #include <vector>
    #include <iostream>
    
    struct OnePoint
    {
       float x;
       float y;
       float z;
       OnePoint(float px=0, float py=0, float pz=0) : x(px), y(py), z(pz) { }
    };
    
    std::vector<OnePoint> AllPoints;
    
    using namespace std;
    
    int main()
    {
        AllPoints.push_back(OnePoint(0,43,1));
        AllPoints.push_back(OnePoint(2,6,1));
        AllPoints.push_back(OnePoint(-1,2,1));
    
        for (std::vector<OnePoint>::size_type i = 0; i < AllPoints.size(); ++i )
        {
            cout << AllPoints[i].x << " " 
                   << AllPoints[i].y << " "
                   << AllPoints[i].z << "\n";
        }
    }
    This prints out the 3 points that were added. You can add as many points as you want usint the push_back() method.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jul 2008
    Posts
    6

    Resolved Re: c++ How can I append 3 values at once to a 2d array?

    Quote Originally Posted by Paul McKenzie
    Also, it makes much more sense to have the 3 points in one structure

    Paul McKenzie
    Would it be possible to use that structure with 3 points in another structure? For example, a curve structure/class?


    Thank you all for the information. I will learn how to use the std::vector.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: c++ How can I append 3 values at once to a 2d array?

    Quote Originally Posted by fishies
    Would it be possible to use that structure with 3 points in another structure? For example, a curve structure/class?
    Yes.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: c++ How can I append 3 values at once to a 2d array?


    Would it be possible to use that structure with 3 points in another structure? For example, a curve structure/class?

    If you need to define a curve class, then you need to extend from base class which is two point one and add another point in derived class.


    I hope this help.

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