1. ## Formulae problems???

I am having difficulties with my first real windows program and I don't believe it is with the programming end of it.
I believe it may be and error with the formulae I am using.
Hopefully, someone here can give a look and offer some suggestions because I am seriously stuck!

I've attached a copy of the formulae I am using. I have checked it numerous times to see if I possibly have coded it
incorrectly but I don't see any errors.
Also, if Iyo is correct how can Ixo be wrong?( See the second output list below.)
Same thing with xbar and ybar, how can 1 be wrong and the other be right?

Code:
void CalculateData()
{
int x;
for(x=0;x<3;x++)
{
// Area
subsectionData.area = subsectionData.area + (rectangleData.ptY[x+1] - rectangleData.ptY[x])
*(rectangleData.ptX[x+1] + rectangleData.ptX[x])/2;

// Ixo  FUBAR...
subsectionData.Ixo = subsectionData.Ixo + ((rectangleData.ptX[x+1] - rectangleData.ptX[x])
* (rectangleData.ptY[x+1] + rectangleData.ptY[x])/24)
* ((pow((rectangleData.ptY[x+1] + rectangleData.ptY[x]), 2)
+ pow((rectangleData.ptY[x+1] - rectangleData.ptY[x]), 2)));

// Iyo  BUT THIS IS CORRECT!
subsectionData.Iyo = subsectionData.Iyo + ((rectangleData.ptY[x+1] - rectangleData.ptY[x])
* (rectangleData.ptX[x+1] + rectangleData.ptX[x])/24)
* ((pow((rectangleData.ptX[x+1] + rectangleData.ptX[x]), 2)
+ pow((rectangleData.ptX[x+1] - rectangleData.ptX[x]), 2)));
}
subsectionData.Iyo = -1 * subsectionData.Iyo;
subsectionData.area = -1 * subsectionData.area;

for(x=0;x<3;x++)
{
subsectionData.YBAR = subsectionData.YBAR + (((rectangleData.ptX[x+1] - rectangleData.ptX[x])/8)
* ((pow((rectangleData.ptY[x+1] + rectangleData.ptY[x]), 2))
+ pow((rectangleData.ptY[x+1] - rectangleData.ptY[x]), 2)/3));

subsectionData.XBAR = subsectionData.XBAR + (((rectangleData.ptY[x+1] - rectangleData.ptY[x])/8)
* ((pow((rectangleData.ptX[x+1] + rectangleData.ptX[x]), 2))
+ pow((rectangleData.ptX[x+1] - rectangleData.ptX[x]), 2)/3));
}

subsectionData.XBAR = -1 / subsectionData.area * subsectionData.XBAR;
subsectionData.YBAR =  1 / subsectionData.area * subsectionData.YBAR;
subsectionData.Ixc = subsectionData.Ixo - (subsectionData.area * (pow(subsectionData.YBAR, 2)));
subsectionData.Iyc = subsectionData.Iyo - (subsectionData.area * (pow(subsectionData.XBAR, 2)));
return;
}

First trial.
Input data:

Subsection Type = rectangle
ptX[0] = 0.000000 ptY[0] = 0.000000
ptX[1] = 0.000000 ptY[1] = 1.000000
ptX[2] = 1.000000 ptY[2] = 1.000000
ptX[3] = 1.000000 ptY[3] = 0.000000

Output data: // All correct!

Area subsection = 1.000000
Ixo subsection = 0.333333
Iyo subsection = 0.333333
XBAR subsection = 0.500000
YBAR subsection = 0.500000
//The following value are based on the values above...
Ixc subsection = 0.083333
Iyc subsection = 0.083333

Second trial.
Input data:

Subsection Type = rectangle
ptX[0] = 1.000000 ptY[0] = 1.000000
ptX[1] = 1.000000 ptY[1] = 2.000000
ptX[2] = 2.000000 ptY[2] = 2.000000
ptX[3] = 2.000000 ptY[3] = 1.000000

Output Data:

Area subsection = 1.000000
Ixo subsection = 2.666667 //Wrong should be 2.333 checked with: Ixo = Ixc +ad^2 where Ixc = bh^3/12
Iyo subsection = 2.333333
XBAR subsection = 1.500000
YBAR subsection = 2.000000 //Wrong should be 1.5
//The following value are based on the values above...
Ixc subsection = -1.333333 //Wrong should be .08333
Iyc subsection = 0.083333

2. In your loops, you make assignments such as:

subsectionData.area = subsectionData.area + ...

However, you don't initialise these values before starting the loop.

Try:

Code:
...
int x;
subsectionData.area = 0;
subsectionData.Ixo = 0;
subsectionData.Iyo = 0;
subsectionData.XBAR = 0;
subsectionData.YBAR = 0;

for(x = 0; x <3; x++)
...

3. ## Davros thanks for the quick reply, but...

Sorry I forgot to mention that I have actually called a function 'ZeroStructures' at the beginning of my program that initializes all these variables.

I actually think there may be an error in my formulae. I just got back from the library but it was no use. They don't have much on this subject.

Code:
void ZeroStructures()
{
subsectionData.area = 0;
subsectionData.Ixc = 0;
subsectionData.Ixo = 0;
subsectionData.Iyc = 0;
subsectionData.Iyo = 0;
subsectionData.k = 0;
subsectionData.XBAR = 0;
subsectionData.YBAR = 0;
subsectionData.Ixy = 0;
return;
}