Ok. I'm not experienced at all, and I need to program a set of different math formulas into a code.

Here is the first step I'm trying to take. I have an octahedron that has 8 plates. Each plate is composed of 3 vectors. There are 6 vectors in total and they are denoted as 1,2,3,4,5,6. Vectors are 1={1,0,0}, 2={0,1,0}, 3={-1,0,0}, 4={0,-1,0}, 5={0,0,1}, 6={0,0,-1} }.

These are the plates and each is composed out of the 3 vectors listed.
Plate 1= vector 1, vector 2, vector 5
Plate 2=2,3,5
Plate 3=3,4,5
Plate 4= 4,1,5
Plate 5=1,2,6
Plate 6=2,3,6
Plate 7= 3,4,6
Plate 8= 4,1, 6

So this is what I'm trying to do. I want to sum up all the vectors that compose each plate and divide by 3 to find the centroid. So the answer will look some like this: (x/3, y/3, z/3).

then I want the program to list each plate and the vectors it is associated with, and then give me the centroid.

Please help me in anyway you can. I know this a vague question, but I have no where else to turn and this has been stumping for the past week.

Here is my code so far:

``````#include <iostream>

using namespace std;

int main()
{
float Vector[6][3] = {{1,0,0}, {0,1,0}, {-1,0,0}, {0,-1,0}, {0,0,1}, {0,0,-1} };
int Plate[8][3] = {{1,2,5}, {2,3,5}, {3,4,5}, {4,1,5}, {1,2,6}, {2,3,6}, {3,4,6}, {4,1,6} };
float R[8][3];

for (int i=0; i<8; i++)
{
int k = Plate[i][1];
int l = Plate[i][2];
int m = Plate[i][3];
for (int j=0; j<3; j++)
{
R[i][j]=(Vector[k][j]+Vector[l][j]+Vector[m][j])/3;
}
cout << "Centroid[" << i+1 << "]: ";
cout << R[i][1] << R[i][2] << R[i][3]  << endl << endl;
return 0;
}``````
4
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by dougy83

TIP !

Show what code you have written, makes it easier to see what direction you want/have to take !

Working with a an STL vector ?
Working with an array ?
... etc.

I'm trying to work with arrays. I don't know how to work with STL vectors. I posted my crappy code for you. It works but it crashes.

Ok. I'm not experienced at all, and I need to program a set of different math formulas into a code.

Here is the first step I'm trying to take. I have an octahedron that has 8 plates. Each plate is composed of 3 vectors. There are 6 vectors in total and they are denoted as 1,2,3,4,5,6. Vectors are 1={1,0,0}, 2={0,1,0}, 3={-1,0,0}, 4={0,-1,0}, 5={0,0,1}, 6={0,0,-1} }.

These are the plates and each is composed out of the 3 vectors listed.
Plate 1= vector 1, vector 2, vector 5
Plate 2=2,3,5
Plate 3=3,4,5
Plate 4= 4,1,5
Plate 5=1,2,6
Plate 6=2,3,6
Plate 7= 3,4,6
Plate 8= 4,1, 6

So this is what I'm trying to do. I want to sum up all the vectors that compose each plate and divide by 3 to find the centroid. So the answer will look some like this: (x/3, y/3, z/3).

then I want the program to list each plate and the vectors it is associated with, and then give me the centroid.

Please help me in anyway you can. I know this a vague question, but I have no where else to turn and this has been stumping for the past week.

Here is my code so far:

[

#include <iostream>

using namespace std;

int main()
{
float Vector[6][3] = {{1,0,0}, {0,1,0}, {-1,0,0}, {0,-1,0}, {0,0,1}, {0,0,-1} };
int Plate[8][3] = {{1,2,5}, {2,3,5}, {3,4,5}, {4,1,5}, {1,2,6}, {2,3,6}, {3,4,6}, {4,1,6} };
float R[8][3];

for (int i=0; i<8; i++)
{
int k = Plate[1];
int l = Plate[2];
int m = Plate[3];
for (int j=0; j<3; j++)
{
R[j]=(Vector[k][j]+Vector[l][j]+Vector[m][j])/3;
}
cout << "Centroid[" << i+1 << "]: ";
cout << R[1] << R[2] << R[3] << endl << endl;
return 0;
}

]

[code]

// paste code here

[/code]

My math regarding Octahedrons is rusty, but as far as the code you posted:

``````for (int i=0; i<8; i++)
{
int k = Plate[i][1];
int l = Plate[i][2];
int m = Plate[i][3];
for (int j=0; j<3; j++)
{
R[i][j]=(Vector[k][j]+Vector[l][j]+Vector[m][j])/3;
}
cout << "Centroid[" << i+1 << "]: ";
cout << R[i][1] << R[i][2] << R[i][3]  << endl << endl;
[COLOR="Red"]return 0;[/COLOR]
}``````

Your return 0 line is short-circuiting your loop. `i` will never be anything other than 0 because the program ends. Put the `return 0` line AFTER the bracket that ends the `i` for-loop in order for this loop to execute eight times.

Arrays are zero based. This means the index goes from 0..ArraySize-1 (not from 1..ArraySize, as you sometimes have it). This is causing errors in your code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.