strange float array issue .....

can any one tell me why my array cpts always contains 0.0,0.0,0.0,0.0,1.0 instead of 0.0,0.25,0.5,0.75,1.0 ?
i tried all the possible ways but can't find the source of this strange problem can any one help ?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137`` ``````#include #include #include #include #include int ncr(int n,int r); int fact(int n); int round(int z); void bcurve(int a, int b, int c, int d, float l, float m); static int counter = 1; static float m[6]; static float l[6]; //this array 'cpts ' is my problem float cpts[5] = { 0, 0.25f, 0.5f, 0.75f, 1 }; int main() { int gd = 0, gm; int n,r; clrscr(); initgraph(&gd,&gm,"C:\\TC\\BGI"); //cout << "enter no of edges in poly" << endl; for(int a = 0 ; a < sizeof(m) ; a++ ) { m[a] = 0; m[a] = 0; } for(int b = 0; b < sizeof(l); b++) { l[b] = 0; l[b] = 0; } n = 4; int x[10][2]; for(int i = 0; i < n; i++) { cout << "enter x & y " << endl; int m, n; cin >> m >>n; x[i][0] = m; x[i][1] = n; } x[n][0] = x[0][0]; x[n][1] = x[0][1]; //int fbptx = x[0][0]; //int fbpty = x[0][1]; //int lbptx = x[n][0]; //int lbpty = x[n][1]; //float u[5]; //even tried reassigning the value, but nothing works for me! cpts[0] = (float) 0; cpts[1] = (float) 0.25f; cpts[2] = (float) 0.5f; cpts[3] = (float) 0.75f; cpts[4] = (float) 1; m[0] = x[0][0]; l[0] = x[0][1]; m[n] = x[n-1][0]; l[n] = x[n-1][1]; m[5] = m[0]; l[5] = l[0]; for(int j = 0 ; j < 3 ; j++ ) { for(int i = 0 ; i < 4 ; i++ ) { int f = x[i][0]; int y = x[i][1]; float uth = pow( cpts[j+1],i); float uk = pow( ( 1-cpts[j+1] ) , 3-i ); bcurve(f,y,3,i,uth,uk); } counter++; } setcolor(GREEN); for(int c = 0 ; c < 5 ; c++) { int x = round(m[c]); int y = round(l[c]); int x1 = round(m[c+1]); int y1 = round(l[c+1]); line(x,y,x1,y1); } getch(); closegraph(); return 0; } int round(int x) { return (x+0.5); } void bcurve(int x, int y, int n, int r, float uth, float uk) { int xval = x * ncr(n,r) * uth * uk; int yval = y * ncr(n,r) * uth * uk; m[counter] += xval; l[counter] += yval; } int ncr(int n, int r) { int nfact = fact(n); int rfact = fact(r); int m = n-r; if(m >= 0){ int nminusrfact = fact(m); int result = nfact/(rfact*(nminusrfact)); return result; }else{ return -1; } } int fact( int n ) { if(n == 1 || n == 0 ) return 1; else return (n* fact(n-1)); } ``````
Last edited on
How do you know it's the cpts fault? You are probably assigning float to int and thus losing fractional data.
Why do you think cpts contains erroneous values?
whats wrong in this
`float cpts[5] = { 0, 0.25f, 0.5f, 0.75f, 1 };`
isnt it tells the compiler to create an array of type float having size = 5 with values 0,0.25,0.5,0.75,1 ?

so when i debug the above file the array cpts contains 0.0,0.0,0.0,0.0,1.0 , just wanna ask WHY?
Last edited on
What debugger are you using?
well, i dont know how are you debugging, but everything is fine for me.
print cptsin in the console to see its values
AAAAAAHHHHHhhhh !!!!

After opening my eyes I've seen the bug:

 ``12345`` ``````for(int a = 0 ; a < sizeof(m) ; a++ ) { m[a] = 0; m[a] = 0; // (BTW: Why a second assignment?) }``````

If I'm right sizeof(m) == 24. Your assining the value 0.0 to 24 floats - but your array does contain only 6. So you're overwriting the following variables namely cpts. Your debugger's right!
The same applies to some other loop termination conditions.

You may

1. Define a macro
`#define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a))) `
and use it as loop termination condition by applying it to your array m.

2. Use one of the standard containers like std::vector. They manage their size internally and could be asked for it.

I would insist to use the second solution.
thanks tcs.... now i have solved the problem...c++ is a hell for people who migrate from Java, seriously :D
3) you may stop using magic numbers and start using named constants:
 ``1234`` ``````const int array_size(5); int array[array_size]; for(int i = 0; i < array_size;++i) //... ``````
Why are you using TurboC++?

It would be easier to learn C++ with a modern IDE.
Topic archived. No new replies allowed.