float divided by int output error I believe

Mar 4, 2009 at 5:54am
Hey Everyone,

I hae a problem that I dont know why occurs. I am crating a simple prog for testing runtime of Euclids algorithm and I dont understand the output error for only ONE of the calculated values see below. I think it may be because I divide the float by an int? I need the float for the average to base it on the fractional values. The amount of zeros I left out is HUGE (in bold below). Here is simple code


NOTE: This is all done in VStudio 2005.

#include<cstdlib>
#include<iostream>
#include<cmath> //for log
#include<iomanip>
#include<conio.h> //for getch()
using namespace std;


int main(){

//these are used to make only certain length floats appear
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);

cout << endl << endl;
cout << "Program to compute the average runtime" << " "<< "av/log(n)" << "\n\n";
cout << setw(10) << " n" << setw(7) << "av" << setw(15) << "av/log(n)" << endl;

int nmax = 200;
int count;
float av = 0.00;


for(int n=1; n<=nmax; n=n+1) //1 - 200
{
count = 0;
for(int m=1; m<=n; m++)
{
//loop to implement Euclid's Algorithm for gcd(n,m)
int nn = n; //nn=1
int mm = m; //mm=1
while(mm != 0)
{
int r = nn % mm; // r=1%1 = 0
nn = mm; //nn=1
mm = r; //mm=0
count++; //count=1
}//end while
}//end for


av = count / (1.0 + n); // av = 1 / 1.0 +

cout << endl << setw(10) << n << setw(10) << av << setw(10) << av/(log(n*1.0)/log(2.0));
}//end for

getch();
return 1;
}//end main

OUTPUT
Program to compute the average runtime av/log(n)

n av av/log(n)

1 0.5000100....(LOTS of ZEROs here...000.#INF
2 0.6667 0.6667
3 1.0000 0.6309
4 1.0000 0.5000
5 1.5000 0.6460
6 1.1429 0.4421
7 1.7500 0.6234
8 1.6667 0.5556
9 1.7000 0.5363
10 1.6364 0.4926
11 2.3333 0.6745
12 1.6154 0.4506
13 2.3571 0.6370
14 2.1333 0.5603
15 2.0000 0.5119
16 2.0588 0.5147
17 2.6111 0.6388
18 2.2105 0.5301
19 2.8000 0.6591
20 2.1429 0.4958
21 2.3182 0.5278
22 2.4348 0.5460
23 3.0833 0.6816
24 2.2000 0.4798
25 2.6538 0.5715
26 2.7407 0.5831
27 2.7143 0.5708
28 2.5172 0.5236
29 3.2333 0.6656
30 2.3226 0.4733
31 3.1875 0.6434
32 2.7576 0.5515
33 2.8529 0.5656
34 3.0286 0.5953
35 2.8333 0.5524
36 2.5135 0.4862
37 3.2895 0.6314
38 3.0769 0.5863
39 3.1000 0.5865
40 2.7073 0.5087
41 3.5000 0.6533
42 2.6512 0.4917
43 3.5455 0.6534
44 2.9556 0.5414
45 2.8913 0.5265
46 3.2340 0.5855
47 3.7083 0.6676
48 2.8367 0.5079
49 3.3000 0.5877
50 3.1765 0.5628
51 3.2692 0.5763
52 3.1132 0.5461
53 3.7963 0.6628
54 3.0545 0.5308
55 3.2857 0.5683
56 2.9298 0.5045
57 3.3276 0.5705
58 3.4237 0.5845
59 3.8667 0.6573
60 2.9016 0.4912
61 3.8871 0.6554
62 3.5556 0.5972
63 3.2813 0.5490
64 3.3692 0.5615
65 3.4091 0.5661
66 3.1343 0.5186
67 3.8824 0.6400
68 3.4928 0.5738
69 3.5857 0.5870
70 3.1549 0.5147
71 4.0278 0.6550
72 3.0000 0.4862
73 3.9865 0.6440
74 3.7600 0.6055
75 3.4737 0.5577
76 3.4935 0.5591
77 3.6282 0.5790
78 3.3418 0.5317
79 4.1250 0.6544
80 3.2469 0.5136
81 3.6220 0.5713
82 3.7831 0.5951
83 4.1429 0.6499
84 3.2118 0.5024
85 3.7093 0.5787
86 3.8621 0.6010
87 3.8182 0.5926
88 3.5393 0.5479
89 4.2111 0.6503
90 3.1868 0.4909
91 3.7609 0.5779
92 3.6237 0.5555
93 3.8617 0.5906
94 3.8737 0.5910
95 3.9583 0.6025
96 3.4124 0.5182
97 4.2347 0.6416
98 3.8182 0.5772
99 3.6400 0.5491
100 3.5347 0.5320
101 4.2843 0.6435
102 3.5728 0.5355
103 4.2885 0.6414
104 3.6476 0.5444
105 3.5755 0.5325
106 4.0561 0.6029
107 4.3704 0.6483
108 3.4954 0.5175
109 4.3727 0.6461
110 3.6036 0.5314
111 4.0714 0.5992
112 3.6018 0.5291
113 4.4123 0.6469
114 3.7043 0.5421
115 4.0172 0.5868
116 3.9402 0.5745
117 3.8729 0.5637
118 4.1681 0.6056
119 4.1000 0.5946
120 3.3306 0.4822
121 4.1393 0.5983
122 4.1138 0.5936
123 4.1129 0.5924
124 4.0400 0.5809
125 4.1984 0.6027
126 3.5276 0.5056
127 4.5156 0.6461
128 3.9922 0.5703
129 4.1615 0.5936
130 3.8015 0.5413
131 4.6061 0.6549
132 3.5865 0.5091
133 4.1418 0.5870
134 4.2667 0.6038
135 3.8235 0.5403
136 3.8175 0.5386
137 4.5725 0.6442
138 3.9281 0.5526
139 4.6286 0.6502
140 3.6667 0.5143
141 4.2465 0.5948
142 4.2517 0.5947
143 4.2222 0.5897
144 3.6897 0.5146
145 4.2260 0.5886
146 4.3129 0.5999
147 4.0541 0.5631
148 4.1678 0.5781
149 4.6467 0.6437
150 3.7616 0.5204
151 4.6974 0.6490
152 4.0458 0.5582
153 4.1494 0.5717
154 3.9355 0.5416
155 4.3205 0.5938
156 3.7006 0.5080
157 4.6646 0.6395
158 4.3774 0.5993
159 4.3750 0.5983
160 3.8696 0.5285
161 4.3272 0.5903
162 3.9877 0.5433
163 4.7317 0.6439
164 4.2727 0.5807
165 3.9819 0.5406
166 4.4551 0.6041
167 4.8214 0.6530
168 3.7101 0.5019
169 4.5000 0.6080
170 4.0351 0.5446
171 4.1860 0.5643
172 4.2832 0.5768
173 4.7874 0.6439
174 4.1143 0.5528
175 4.2614 0.5719
176 4.0621 0.5446
177 4.4326 0.5936
178 4.5251 0.6053
179 4.8444 0.6473
180 3.6961 0.4934
181 4.7967 0.6396
182 4.0656 0.5415
183 4.5000 0.5987
184 4.1892 0.5568
185 4.5000 0.5975
186 4.1604 0.5518
187 4.4787 0.5935
188 4.3651 0.5778
189 4.2263 0.5589
190 4.1466 0.5478
191 4.8854 0.6447
192 3.9534 0.5212
193 4.8608 0.6402
194 4.6051 0.6059
195 4.1020 0.5392
196 4.2081 0.5526
197 4.8737 0.6394
198 4.0201 0.5269
199 4.8700 0.6377
200 4.0547 0.5305
Mar 4, 2009 at 8:04am
Take a calculator and calculate: 0.5 / (log ( 1 ) / log(2))
Then try to figure out how this relates to the last cout in your program. Then you'll know how to fix it!

BTW: Use the "#" Button to format your code nicely.
Topic archived. No new replies allowed.