Variables not retaining numerical assignments

I have written a program of sorts to calculate the element stiffness matrix of a 2-D rectangular finite element. For brevity's sake I will not post the entire thing, but here is my problem: I have created arrays with elements for which I have assigned numerical values, however these values are not being retained in the contents of the array. For example, when I define the integration weights toward the very beginning of the program, it looks something like this:

int w2d[3][1];
int w1,w2,w3;

w2d[0][0] = .555555555;
w2d[1][0] = .888888888;
w2d[2][0] = .555555555;

w1 = w2d[0][0];
w2 = w2d[1][0];
w3 = w2d[2][0];

Now, if I follow this code with:

cout<<w1<<w2<<w3;

The output is the following:

000

I noticed this because the rest of the program relies on these values, and my final product was returning all zeros. I went through line by line and discovered that the arrays I thought had numerical values assigned to them were in fact equal to zero.

I am rather new to C++, but I have exhausted the Xcode manual and my patience with Google so I was hoping someone here could lend a hand. The heading of my program looks like this:

#include <iostream>

using namespace std;

int main()
{ "Finite element program that's way too long to post"
}

So it's pretty basic I think. I'm quite confident there is a much more efficient way to have coded this whole thing, but I'm just a lowly mechanical engineer so my programming experience starts and ends with matlab.

Any idea why I am getting zeros in this case?

Thanks for your consideration.

Last edited on
The variables are retaining the values assigned to them, the problem is that what you think is being assigned is incorrect. The variables that you are using are of type int, meaning that they can hold integer values. You are attempting to assign decimal numbers, which are not integers. You will have to use a variable type that allows decimal numbers, such as floating point numbers, or double precision floating point numbers:

http://cplusplus.com/doc/tutorial/variables/

The reason that you are getting zeros is due to rounding to allow decimal values to be assigned to integer variables.
Excellent. Thank you for the quick response.
Topic archived. No new replies allowed.