assigning coordinates to triangles

Hi,

I am trying to write a simple program that assigns x,y coordinates to the centroids and angles of trianlges. This is the code:




# include <iostream>
#include <fstream>

using namespace std;

float rightisosceles [100][4][10];
float n, n1;
float ydisplacement2 = 0.0;
float righttrianglebase = 1.0;
float righttriangleheight = 1.2;
int v = 1, isoscelescounter = 1;
int p, hh, oo, pp;


int main()

{


for (hh = 0; hh <=100; hh++) {
for (oo = 0; oo <=100; oo++) { for (pp = 0; pp <=100; pp++)
{
rightisosceles [hh][oo][pp] = 0.0;


}
}
}

{


for (n = 3.0 ; n <= 70; n + 5) {
for (n1 = 4.0 ; n1 <= 39; n1 + 5 ) {

rightisosceles [v][0][0] = n1 ; // right isosceles x coordinates
rightisosceles [v][1][0] = (rightisosceles [v][0][0]) + (righttrianglebase /2);
rightisosceles [v][2][0] = (rightisosceles [v][0][0]) - (righttrianglebase /2);
rightisosceles [v][3][0] = (rightisosceles [v][0][0]);

rightisosceles [v][0][1] = n ; // right isosceles y coordinates
rightisosceles [v][1][1] = (rightisosceles [v][0][1]) + (righttriangleheight /3);
rightisosceles [v][2][1] = (rightisosceles [v][1][1]);
rightisosceles [v][3][1] = (rightisosceles [v][2][1]) - (righttriangleheight);

v = v + 1;
isoscelescounter = isoscelescounter + 1;

cout << rightisosceles [v][0][0] << endl;
}
}

}
}



When I run the program, a console window opens with nothing in it and I dont get any results. I am not sure why.Can anyone help with this.

Thanks.


Maybe the program crashes when the first 3 for loops write out of bounds of rightisosceles. The sizes are [100][4][10] but you write form 0 to 100 in each place. Note that from 0 to 100 there are 101 values while you only have 100 so even the first loop isn't right. You should use < in your for loops.

Another problem is with your other for loops. n+5 does not modify n, so the loop would be infinite. The right way would be n=n+5 or n+=5.
Last edited on
Thank you hamsterman. I modified the code but it is still having a problem... it's giving me all 0s for the values with the exception of 2 values and then it crashes and the operating system shuts it down. I am using code::blocks with Win XP. Here is the modified code:




# include <iostream>
#include <fstream>

using namespace std;

float rightisosceles [100][4][10];
float n, n1;
float ydisplacement2 = 0.0;
float righttrianglebase = 1.0;
float righttriangleheight = 1.2;
int v = 1, isoscelescounter = 1;
int p, hh, oo, pp;


int main()

{


for (hh = 0; hh <100; hh++) {
for (oo = 0; oo <4; oo++) { for (pp = 0; pp <10; pp++)
{
rightisosceles [hh][oo][pp] = 0.0;


}
}
}



for (n = 3.0 ; n <=70; n = n + 5) { // positioning particles
for (n1 = 4.0 ; n1 <= 39; n1 = n1 + 5) {



rightisosceles [v][0][0] = n1 ; // right isosceles x coordinates
rightisosceles [v][1][0] = (rightisosceles [v][0][0]) + (righttrianglebase /2);
rightisosceles [v][2][0] = (rightisosceles [v][0][0]) - (righttrianglebase /2);
rightisosceles [v][3][0] = (rightisosceles [v][0][0]);

rightisosceles [v][0][1] = n ; // right isosceles y coordinates
rightisosceles [v][1][1] = (rightisosceles [v][0][1]) + (righttriangleheight /3);
rightisosceles [v][2][1] = (rightisosceles [v][1][1]);
rightisosceles [v][3][1] = (rightisosceles [v][2][1]) - (righttriangleheight);

v = v + 1;
isoscelescounter = isoscelescounter + 1;

cout << rightisosceles [v][0][0] << endl;
}
}

}



Last edited on
The same. You never make sure that v < 100. If it is not, the program crashes.
ok. I increased the size of rightisosceles [150][4][10]. The program is not crashing anymore but the x,y values are all 0s. This is the modified program:



# include <iostream>
#include <fstream>

using namespace std;

float rightisosceles [150][4][10];
float n, n1;
float ydisplacement2 = 0.0;
float righttrianglebase = 1.0;
float righttriangleheight = 1.2;
int v = 1, isoscelescounter = 1;
int p, hh, oo, pp;


int main()

{


for (hh = 0; hh <150; hh++) {
for (oo = 0; oo <4; oo++) { for (pp = 0; pp <10; pp++)
{
rightisosceles [hh][oo][pp] = 0.0;


}
}
}



for (n = 3.0 ; n <=70; n = n + 5) { // positioning particles
for (n1 = 4.0 ; n1 <= 39; n1 = n1 + 5) {



rightisosceles [v][0][0] = n1 ; // right isosceles x coordinates
rightisosceles [v][1][0] = (rightisosceles [v][0][0]) + (righttrianglebase /2);
rightisosceles [v][2][0] = (rightisosceles [v][0][0]) - (righttrianglebase /2);
rightisosceles [v][3][0] = (rightisosceles [v][0][0]);

rightisosceles [v][0][1] = n ; // right isosceles y coordinates
rightisosceles [v][1][1] = (rightisosceles [v][0][1]) + (righttriangleheight /3);
rightisosceles [v][2][1] = (rightisosceles [v][1][1]);
rightisosceles [v][3][1] = (rightisosceles [v][2][1]) - (righttriangleheight);

v = v + 1;
isoscelescounter = isoscelescounter + 1;

cout << rightisosceles [v][0][1] << endl;
cout << rightisosceles [v][1][1] << endl;
cout << rightisosceles [v][2][1] << endl;
cout << rightisosceles [v][3][1] << endl;

}
}

}



Why is the second loop not functioning correctly? It is supposed to give me values other than 0s.
You increment v before printing, so the value you print is not the value you just computed.
Thank you hamsterman but again all values I have are 0s. I tried printing the n's n1's and v's and they print just fine but when I try to assign values to the array (any array) they keep on giving me 0s.Something is not working in this code, or could it be a software glitch..because it totally does not make any sense.
Did you misunderstand my last post? Look, let's take the first cycle, where v = 0.
First you set rightisosceles [0][0][1] to n and etc.
Then you increment v so that it is 1.
And finally you print rightisosceles [1][0][1]. You filled all the array with 0s, so of course, the next element will be 0.
Now if you do incrementing after printing, this problem will not occur.
You are absolutely right..thank you so much...
Topic archived. No new replies allowed.