Click to See Complete Forum and Search --> : c++ How can I append 3 values at once to a 2d array?


fishies
July 21st, 2008, 04:14 PM
#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::dots()
{

}

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.

Lindley
July 21st, 2008, 04:31 PM
Okay, what *exactly* are you trying to do here?
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.

fishies
July 21st, 2008, 07:01 PM
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.

VladimirF
July 21st, 2008, 07:11 PM
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.

Paul McKenzie
July 21st, 2008, 07:13 PM
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

Paul McKenzie
July 21st, 2008, 07:23 PM
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:

#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

fishies
July 21st, 2008, 07:39 PM
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.

Paul McKenzie
July 21st, 2008, 07:41 PM
Would it be possible to use that structure with 3 points in another structure? For example, a curve structure/class?Yes.

Regards,

Paul McKenzie

Peter_APIIT
July 21st, 2008, 08:44 PM
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.