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 ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>


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:

1
2
3
4
5
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:
1
2
3
4
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.