Different output for the same input data - c++

Why does this code return 10 on the test machine for the input data:
5
4 2 3 3 3
while on my PC it correctly returns 12?
#include <iostream>
#include <cstdlib>
using namespace std;
int n;
int b[99];
int money = 0;
int min()
{
int min = b[0];
int index = 0;
for (int i = 1; i < n; i++)
if (b[i] < min && b[i] % 2 != 0) {
min = b[i];
index = i;
}
for (int i = index; i < n - 1; i++)
{
b[i] = b[i + 1];
}
b[n - 1] = 0;
return min;
}
int main()
{
money = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> b[i];
money += b[i];
}
while (money % 2 != 0)
{
money -= min();
}
if (money % 2 == 0)
if (money != 0)
cout << money;
else cout << "NIESTETY";
system("pause");
}
Thanks!
Edit: It happened on my PC too, once. But for other input data it works perfectly. And i'm not talking about some simple values. I've run some recommended tests for this problem, this for example:
581
906 369 899 998 73 717 269 51 875 61 123 237 50 924 576 52 693 394 952 24 534 452 634 139 642 570 20 643 128 165 144 867 86 256 981 26 344 912 489 524 667 375 502 944 961 61 586 834 62 240 644 602 563 758 587 973 440 920 977 785 999 186 384 231 414 184 84 492 277 787 737 312 849 595 238 892 377 103 275 797 847 652 263 526 473 643 972 701 209 70 95 852 337 387 651 451 843 649 445 172 592 299 110 479 191 148 324 600 928 258 354 693 522 637 977 180 394 306 846 885 85 339 678 355 799 297 232 929 133 173 698 362 592 426 319 690 195 944 143 571 831 765 847 106 293 460 650 4 876 891 272 987 171 767 495 486 161 243 959 431 707 761 634 574 774 176 84 903 102 223 7 759 886 113 626 917 873 177 307 46 56 415 162 537 845 956 704 725 551 266 673 255 567 520 278 1000 450 573 205 447 656 946 460 331 317 125 340 372 246 635 667 642 422 766 669 894 217 759 396 160 985 46 61 832 375 272 276 49 820 440 259 482 569 97 903 135 164 570 412 320 827 403 610 577 350 543 328 481 136 595 584 935 830 134 587 658 604 865 742 111 668 313 669 233 592 451 735 539 890 319 882 674 306 376 945 313 872 683 915 92 954 598 750 106 576 259 563 293 158 708 716 259 434 833 867 754 678 576 981 55 8 421 31 176 641 707 175 531 299 65 55 118 990 696 533 713 829 20 310 201 748 697 397 550 909 584 366 6 515 298 128 483 454 564 986 171 225 35 733 664 368 674 146 479 70 610 734 708 550 962 227 606 520 751 449 254 6 253 757 942 63 926 592 768 516 843 187 836 722 329 745 54 703 437 671 463 288 548 847 563 354 964 27 100 120 351 928 557 757 937 872 417 18 335 181 68 834 994 83 178 67 45 965 31 976 155 883 726 173 813 66 199 211 619 429 643 237 464 586 929 546 675 392 350 811 187 936 64 167 394 969 36 630 430 377 905 283 36 352 221 293 125 975 214 903 622 769 955 759 83 104 988 876 612 308 495 612 634 288 581 164 595 685 620 817 988 355 134 71 159 493 730 952 70 620 961 407 78 159 660 542 991 858 101 140 531 156 667 323 621 365 193 825 252 848 897 5 16 277 461 909 549 194 638 422 718 979 705 515 499 488 447 249 229 382 315 851 625 249 818 889 580 224 926 270 734 689 275 842 834 838 774 949 101 728 930 478 397 188 228 675 36 259 834 745 629 374 664 335 30 745 963 918 38 848 482 739 745 912 154 408 250 271 480 767 120 142 652 169 764 541 228 919 482 984 942 631 46 529 395 576
//Output: 289826

Related

AVX512 Vectorizing Modulo Gives Negative Result For Very Large Inputs

I am currently trying to vectorize a modulo calculation using AVX512.
Because there is no AVX modulo operation (except SVML) or an integer division, I am using the following formula d % p = d - int(float(d)/float(p))*p.
However, for very large inputs, I get negative results.
#include <bits/stdc++.h>
#include <immintrin.h>
int main() {
const auto SIZE = 1024;
int64_t input[SIZE];
int64_t output[SIZE] = {};
const auto p = 1'536; // 1.5 * 1024
std::iota(input, input + SIZE, 15596705878733779060ULL);
__m512i _divider_512 = _mm512_set1_epi64(p);
for (size_t idx = 0; idx < SIZE; idx += 8) {
__m512i _inputs = _mm512_loadu_si512(&input[idx]);
__m512i _e = _mm512_cvt_roundpd_epi64(_mm512_div_pd(_mm512_cvtepi64_pd(_inputs), _mm512_cvtepi64_pd(_divider_512)), _MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC);
__m512i _mult = _mm512_mullo_epi64(_e, _divider_512);
__m512i _modulo_result = _mm512_sub_epi64(_inputs, _mult);
_mm512_storeu_si512(&output[idx], _modulo_result);
}
for (auto i = 0; i< SIZE; ++i) {
std::cout << output[i] << std::endl;
}
}
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
-255
-254
-253
-252
-251
-250
-249
-248
-247
-246
-245
-244
-243
-242
-241
-240
-239
-238
-237
-236
-235
-234
-233
-232
-231
-230
-229
-228
-227
-226
-225
-224
-223
-222
-221
-220
-219
-218
-217
-216
-215
-214
-213
-212
-211
-210
-209
-208
-207
-206
-205
-204
-203
-202
-201
-200
-199
-198
-197
-196
-195
-194
-193
-192
-191
-190
-189
-188
-187
-186
-185
-184
-183
-182
-181
-180
-179
-178
-177
-176
-175
-174
-173
-172
-171
-170
-169
-168
-167
-166
-165
-164
-163
-162
-161
-160
-159
-158
-157
-156
-155
-154
-153
-152
-151
-150
-149
-148
-147
-146
-145
-144
-143
-142
-141
-140
-139
-138
-137
-136
-135
-134
-133
-132
-131
-130
-129
-128
-127
-126
-125
-124
-123
-122
-121
-120
-119
-118
-117
-116
-115
-114
-113
-112
-111
-110
-109
-108
-107
-106
-105
-104
-103
-102
-101
-100
-99
-98
-97
-96
-95
-94
-93
-92
-91
-90
-89
-88
-87
-86
-85
-84
-83
-82
-81
-80
-79
-78
-77
-76
-75
-74
-73
-72
-71
-70
-69
-68
-67
-66
-65
-64
-63
-62
-61
-60
-59
-58
-57
-56
-55
-54
-53
-52
-51
-50
-49
-48
-47
-46
-45
-44
-43
-42
-41
-40
-39
-38
-37
-36
-35
-34
-33
-32
-31
-30
-29
-28
-27
-26
-25
-24
-23
-22
-21
-20
-19
-18
-17
-16
-15
-14
-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
Is this a rounding error between ints and doubles? Or am I missing something else?
And how could I workaround it?

Reading from an ifstream into integers. While passing ifstream to functions

I am working on a program where I need to read ints from a file into an array and use the array to do work. not super complicated and shouldtm take next to no time at all.
i am still a student learning c++ and i've exhausted all my options trying to get this to function as I understand it.
The text file has 200 lines each line contains a int followed by a '\n' newline character.
When I read the file in the main function into an int it works as expected. when I pass the ifstream by reference into the function that does the exact same for loop it breaks giving me an uninitialized in as the only value.
#include<iostream> // required
#include<fstream>
#include<random>
#include<string>
using namespace std; // using standard namespace as for this entire program
void readNumbers(ifstream&, int[], int); // passing a input filestream by reference, c style array, and an integer for the array length
int totalInts(ifstream&); // this will read how many lines the file has
int main()
{
ifstream is("numbers.txt"); // this is the text file that we are reading from
int ar[200]; // the array of length 200 that everything will be read into
int temp; // temporary holding for our ints
if (!is) // error checking
cout << "somethings <expletive deleted>";
else {
for (int i = 0; i < 200; i++) // iterating through the file 200 times and inputting the int into the temp holding, this is being used to test if this method works as expected.
{
is >> temp;
cout << i << ' ' << temp << '\n'; // prints it out for troubleshooting
}
// cout << totalInts(is);
readNumbers(is, ar, 200);
}
return 0;
}
int totalInts(ifstream& file)
{
string i;
int intNum;
intNum = 0;
while (getline(file, i))
{
intNum++;
}
return intNum;
}
void readNumbers(ifstream &reading, int papi[], int rayLength) // where we really want to read the file
{
int temp; // for temp holding while we troubleshoot
if (!reading)
cout << "somethings fucked";
else
{
for (int i = 0; i < rayLength;i++) // reads the file for as many time as we have space in the array.
{
reading >> temp; // reads into temp
cout << temp; // outputs for troubleshooting
}
}
}
the program outputs below
0 41
1 485
2 340
3 526
4 188
5 739
6 489
7 387
8 988
9 488
10 710
11 173
12 304
13 843
14 970
15 491
16 997
17 953
18 831
19 441
20 423
21 618
22 905
23 153
24 292
25 394
26 438
27 734
28 737
29 914
30 452
31 747
32 785
33 549
34 870
35 931
36 692
37 325
38 52
39 903
40 731
41 834
42 353
43 363
44 690
45 668
46 156
47 718
48 281
49 874
50 572
51 671
52 694
53 789
54 57
55 871
56 731
57 750
58 556
59 778
60 328
61 38
62 212
63 843
64 288
65 136
66 49
67 950
68 283
69 670
70 473
71 828
72 905
73 735
74 394
75 365
76 21
77 132
78 417
79 551
80 648
81 635
82 108
83 973
84 774
85 851
86 970
87 383
88 944
89 334
90 960
91 471
92 650
93 334
94 542
95 559
96 134
97 84
98 951
99 557
100 837
101 146
102 643
103 687
104 726
105 939
106 990
107 308
108 704
109 408
110 26
111 773
112 950
113 91
114 276
115 834
116 803
117 588
118 102
119 528
120 10
121 303
122 170
123 654
124 377
125 791
126 678
127 589
128 35
129 64
130 377
131 151
132 957
133 745
134 979
135 433
136 138
137 221
138 25
139 348
140 472
141 299
142 780
143 393
144 959
145 917
146 241
147 767
148 245
149 606
150 428
151 970
152 533
153 43
154 429
155 197
156 900
157 623
158 780
159 656
160 427
161 365
162 651
163 557
164 569
165 489
166 622
167 45
168 605
169 374
170 301
171 866
172 383
173 31
174 600
175 45
176 375
177 222
178 687
179 508
180 289
181 738
182 53
183 1
184 444
185 965
186 906
187 791
188 145
189 467
190 731
191 907
192 672
193 505
194 824
195 423
196 324
197 623
198 835
199 523
-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460
E:\Github Repo's\assignment 3.2\Debug\assignment 3.2.exe (process 12832) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
as you can see the output for the first for loop is exactly as expected, however the function which has all the same functionality is not working and is showing an uninitialized int for temp.
this means that it is not reading from the ifstream which I passed to the function. so wrote another function that reads the file using a newly created ifstream as well as the passed ifstream and prints what is read from both streams.
totalInts(ifstream &file)
{
ifstream tester("numbers.txt");
string l,i;
int intNum;
intNum = 0;
for (int j = 0; j < 200; j++)
{
file >> l;
tester >> i;
intNum++;
cout << intNum << ' ' << i << ' ' << l << '\n';
}
file.close();
return intNum;
}
when this is used the output is as expected, so passing the ifstream as a param using reference is the issue, as when I make a new ifstream the program runs fine using that ifstream and reading the file as expected where as the passed ifstream shows no values read.
0 41
1 485
2 340
3 526
4 188
5 739
6 489
7 387
8 988
9 488
10 710
11 173
12 304
13 843
14 970
15 491
16 997
17 953
18 831
19 441
20 423
21 618
22 905
23 153
24 292
25 394
26 438
27 734
28 737
29 914
30 452
31 747
32 785
33 549
34 870
35 931
36 692
37 325
38 52
39 903
40 731
41 834
42 353
43 363
44 690
45 668
46 156
47 718
48 281
49 874
50 572
51 671
52 694
53 789
54 57
55 871
56 731
57 750
58 556
59 778
60 328
61 38
62 212
63 843
64 288
65 136
66 49
67 950
68 283
69 670
70 473
71 828
72 905
73 735
74 394
75 365
76 21
77 132
78 417
79 551
80 648
81 635
82 108
83 973
84 774
85 851
86 970
87 383
88 944
89 334
90 960
91 471
92 650
93 334
94 542
95 559
96 134
97 84
98 951
99 557
100 837
101 146
102 643
103 687
104 726
105 939
106 990
107 308
108 704
109 408
110 26
111 773
112 950
113 91
114 276
115 834
116 803
117 588
118 102
119 528
120 10
121 303
122 170
123 654
124 377
125 791
126 678
127 589
128 35
129 64
130 377
131 151
132 957
133 745
134 979
135 433
136 138
137 221
138 25
139 348
140 472
141 299
142 780
143 393
144 959
145 917
146 241
147 767
148 245
149 606
150 428
151 970
152 533
153 43
154 429
155 197
156 900
157 623
158 780
159 656
160 427
161 365
162 651
163 557
164 569
165 489
166 622
167 45
168 605
169 374
170 301
171 866
172 383
173 31
174 600
175 45
176 375
177 222
178 687
179 508
180 289
181 738
182 53
183 1
184 444
185 965
186 906
187 791
188 145
189 467
190 731
191 907
192 672
193 505
194 824
195 423
196 324
197 623
198 835
199 523
1 41
2 485
3 340
4 526
5 188
6 739
7 489
8 387
9 988
10 488
11 710
12 173
13 304
14 843
15 970
16 491
17 997
18 953
19 831
20 441
21 423
22 618
23 905
24 153
25 292
26 394
27 438
28 734
29 737
30 914
31 452
32 747
33 785
34 549
35 870
36 931
37 692
38 325
39 52
40 903
41 731
42 834
43 353
44 363
45 690
46 668
47 156
48 718
49 281
50 874
51 572
52 671
53 694
54 789
55 57
56 871
57 731
58 750
59 556
60 778
61 328
62 38
63 212
64 843
65 288
66 136
67 49
68 950
69 283
70 670
71 473
72 828
73 905
74 735
75 394
76 365
77 21
78 132
79 417
80 551
81 648
82 635
83 108
84 973
85 774
86 851
87 970
88 383
89 944
90 334
91 960
92 471
93 650
94 334
95 542
96 559
97 134
98 84
99 951
100 557
101 837
102 146
103 643
104 687
105 726
106 939
107 990
108 308
109 704
110 408
111 26
112 773
113 950
114 91
115 276
116 834
117 803
118 588
119 102
120 528
121 10
122 303
123 170
124 654
125 377
126 791
127 678
128 589
129 35
130 64
131 377
132 151
133 957
134 745
135 979
136 433
137 138
138 221
139 25
140 348
141 472
142 299
143 780
144 393
145 959
146 917
147 241
148 767
149 245
150 606
151 428
152 970
153 533
154 43
155 429
156 197
157 900
158 623
159 780
160 656
161 427
162 365
163 651
164 557
165 569
166 489
167 622
168 45
169 605
170 374
171 301
172 866
173 383
174 31
175 600
176 45
177 375
178 222
179 687
180 508
181 289
182 738
183 53
184 1
185 444
186 965
187 906
188 791
189 145
190 467
191 731
192 907
193 672
194 505
195 824
196 423
197 324
198 623
199 835
200 523
E:\Github Repo's\assignment 3.2\Debug\assignment 3.2.exe (process 24600) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
If this was up to me I would just be passing the filename through to the functions as a string param and making new ifstreams in these functions to get the thing to work, however the requirement for this is to have ad least one function that passes an ifstream through reference and utilize it. however after lots of troubleshooting and reading stackexange posts on the topic I cannot get it to function as expected.
what am I doing wrong?
any help would be appreciated.

What should I use to read integers from a file

Hi I am a beginner in programming, and I want to read many integers distributed in row and columns, and how many integer is there. Whenever I try infile>>x it just take the first value.
I would appreciate it so much ,if you can just explain how can you do it to me.
the file looks like this:(much longer but I want you to get the idea):
41 467 334 500 169 724 478 358 962 464 705 145 281 827 961 491 995 942 827 436 391 604 902 153 292 382 421 716 718 895 447 726 771 538 869 912 667 299 35 894 703 811 322 333 673 664 141 711 253 868 547 644 662 757 37 859 723 741 529 778 316 35 190 842 288 106 40 942 264 648 446 805 890 729 370 350 6 101 393 548 629 623 84 954 756 840 966 376 931 308 944 439 626 323 537 538 118 82 929 541 833 115 639 658 704 930 977 306 673 386 21 745 924 72 270 829 777 573 97 512 986 290 161 636 355 767 655 574 31 52 350 150 941 724 966 430 107 191 7 337 457 287 753 383 945 909 209 758 221 588 422 946 506 30 413 168 900 591 762 655 410 359 624 537 548 483 595 41 602 350 291 836 374 20 596 21 348 199 668 484 281 734 53 999 418 938 900 788 127 467 728 893 648 483 807 421 310 617 813 514 309 616 935 451 600 249 519 556 798 303 224 8 844 609 989 702 195 485 93 343 523 587 314 503 448 200 458 618 580 796 798 281 589 798 9 157 472 622 538 292 38 179 190 657 958 191 815 888 156 511 202 634 272 55 328 646 362 886 875 433 869 142 844 416 881 998 322 651 21 699 557 476 892 389 75 712 600 510 3 869 861 688 401 789 255 423 2 585 182 285 88 426 617 757 832 932 169 154 721 189 976 329 368 692 425 555 434 549 441 512 145 60 718 753 139 423 279 996 687 529 549 437 866 949 193 195 297 416 286 105 488 282 455 734 114 701 316 671 786 263 313 355 185 53 912 808 832 945 313 756 321 558 646 982 481 144 196 222 129 161 535 450 173 466 44 659 292 439 253 24 154 510 745 649 186 313 474 22 168 18 787 905 958 391 202 625 477 414 314 824 334 874 372 159 833 70 487 297 518 177 773 270 763 668 192 985 102 480 213 627 802 99 527 625 543 924 23 972 61 181 3 432 505 593 725 31 492 142 222 286 64 900 187 360 413 974 270 170 235 833 711 760 896 667 285 550 140 694 695 624 19 125 576 694 658 302 371 466 678 593 851 484 18 464 119 152 800 87 60 926 10 757 170 315 576
Try using a while to loop through every integer in the file:
while(infile>>x) {
// Do something with the integer
}
Note that if you want it in a 2 dimensional array of rows and columns, you could do something more elaborate like this using a std::stringstream:
#include <sstream>
#include <string>
//...
std::vector<std::vector<int>> result;
std::string tmp;
while(getline(infile,tmp)) {
stringstream stream;
stream << tmp;
// add new row
std::vector<int> row;
while (ss >> x) {
// add x to the new row
}
result.push_back(row);
}

c++ Multiples of 5 in sieve of atkin implemenation

I'm solving a problem over at project euler which requires me to find the sum of all primes under 2 million. I tried to implement sieve of atkin and strangely it sets numbers like 65,85 as primes. I looked at the code and algorithm for over a a day but can't find anything wrong. I'm sure it must be something silly but i can't find it. Thanks in advance
i'm using visual studio express 2012.
here's the code:
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <vector>
#include <fstream>
#include <conio.h>
int main(){
long long int limit,n;
std::cout<<"Enter a number...."<<std::endl;
std::cin>>limit;
std::vector<bool> prime;
for(long long int k=0;k<limit;k++){ //sets all entries in the vector 'prime' to false
prime.push_back(false);
}
long long int root_limit= ceil(sqrt(limit));
//sive of atkin implementation
for(long long int x=1;x<=root_limit;x++){
for(long long int y=1;y<=root_limit;y++){
n=(4*x*x)+(y*y);
if(n<=limit && (n%12==1 || n%12==5)){
prime[n]=true;
}
n=(3*x*x)+(y*y);
if(n<=limit && n%12==7){
prime[n]=true;
}
n=(3*x*x)-(y*y);
if(x>y && n<=limit && n%12==11){
prime[n]=true;
}
}
}
//loop to eliminate squares of the primes(making them square free)
for(long long int i=5;i<=root_limit;i++){
if(prime[i]==true){
for(long long int j=i*i;j<limit;j+=(i*i)){
prime[j]=false;
}
}
}
unsigned long long int sum=0;
//print values to a seperate text file
std::ofstream outputfile("data.txt");
outputfile<<"2"<<std::endl;
outputfile<<"3"<<std::endl;
for(long long int l=5;l<limit;l++){
if(prime[l]==true){
sum+=l;
outputfile<<l<<std::endl;;
}
}
outputfile.close();
std::cout<<"The sum is...."<<sum+5<<std::endl;
prime.clear();
return 0;
}
and hers the data.txt i pointed out few errors
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
65<-----
67
71
73
79
83
85<-----
89
91
97
101
103
107
109
113
127
131
137
139
143
145<----
149
151
157
163
167
173
179
181
185<----
191
193
197
199
205
211
221
223
227
229
233
239
241
247
251
257
259
263
265
269
271
277
281
283
293
299
305
307
311
313
317
331
337
347
349
353
359
365
367
373
377
379
383
389
397
401
403
407
409
419
421
427
431
433
439
443
445
449
457
461
463
467
479
481
485
487
491
493
499
503
505
509
511
521
523
533
541
545
547
557
559
563
565
569
571
577
587
593
599
601
607
611
613
617
619
629
631
641
643
647
653
659
661
671
673
677
679
683
685
689
691
697
701
703
709
719
727
733
739
743
745
751
757
761
763
767
769
773
785
787
793
797
803
809
811
821
823
827
829
839
851
853
857
859
863
865
871
877
881
883
887
901
905
907
911
919
923
929
937
941
947
949
953
965
967
971
977
983
985
991
997
1009
1013
1019
1021
1027
1031
1033
1037
1039
1049
1051
1061
1063
1067
1069
1073
1079
1087
1091
1093
1097
1099
1103
1105
1109
1117
1123
1129
1145
1147
1151
1153
1157
1159
1163
1165
1171
1181
1187
1189
1193
1199
1201
1205
1213
1217
1223
1229
1231
1237
1241
1249
1259
1261
1267
1277
1279
1283
1285
1289
1291
1297
1301
1303
1307
1313
1319
1321
1327
1339
1345
1351
1361
1367
1373
1381
1385
1387
1391
1399
1403
1405
1409
1417
1423
1427
1429
1433
1439
1447
1451
1453
1459
1465
1469
1471
1481
1483
1487
1489
1493
1499
1511
1513
1517
1523
1531
1537
1543
1549
1553
1559
1565
1567
1571
1579
1583
1585
1591
1597
1601
1603
1607
1609
1613
1619
1621
1627
1637
1649
1651
1657
1663
1667
1669
1679
1685
1687
1693
1697
1699
1703
1709
1717
1721
1723
1727
1733
1739
1741
1745
1747
1753
1759
1765
1769
1777
1781
1783
1787
1789
1801
1807
1811
1823
1831
1843
1847
1853
1861
1865
1867
1871
1873
1877
1879
1885
1889
1891
1901
1907
1913
1921
1931
1933
1937
1939
1945
1949
1951
1961
1963
1973
1979
1985
1987
1991
1993
1997
1999
You're supposed to flip the entries to the sieve list. In the first nested for loops instead of prime[n]=true; you should have prime[n]=!prime[n];

How to fix this round-off error?

Apologies for the long code. This is as far as I could reduce it.
#include <QtGui/QApplication>
#include <QtGui/QWidget>
#include <QtGui/QImage>
#include <QtGui/QPainter>
#include <vector>
using namespace std;
class View : public QWidget {
typedef pair<double, double> Point;
unsigned char* _buffer;
double centerx, centery, scale;
double xmin, xmax, ymin, ymax;
double xprec, yprec;
double xratio, yratio;
double fwidth, fheight;
double xlen, ylen;
int width;
int height;
public:
View(int w, int h) : width(w), height(h) {
_buffer = new unsigned char[4 * w * h];
fwidth = static_cast<double>(width);
fheight = static_cast<double>(height);
double aspectRatio = fwidth / fheight;
centerx = 0;
centery = 0;
scale = 2.3;
xlen = aspectRatio * scale;
ylen = 1.0 * scale;
xmin = -(xlen * 0.5) + centerx;
xmax = -xmin;
ymin = -(ylen * 0.5) + centery;
ymax = -ymin;
xprec = xlen / fwidth;
yprec = ylen / fheight;
xratio = fwidth / scale / aspectRatio;
yratio = fheight / scale;
}
double roundX(double x) { return std::floor(x / xprec) * xprec; }
double roundY(double y) { return std::floor(y / yprec) * yprec; }
protected:
void paintEvent(QPaintEvent* event) {
QPainter painter(this);
render();
painter.drawImage(
QPoint(0, 0),
QImage(_buffer, width, height, QImage::Format_RGB32));
}
private:
void render() {
memset(_buffer, 0, 4 * width * height);
for (double i = xmin; i < xmax; i += xprec) {
for (double j = ymin; j < ymax; j += yprec) {
Point p(roundX(i), roundY(j));
int x = static_cast<int>((p.first * xratio) - (xmin * xratio) );
int y = static_cast<int>((p.second * yratio) - (ymin * yratio) );
_buffer[4 * (x * width + y) ] = 255;
_buffer[4 * (x * width + y) + 1] = 255;
_buffer[4 * (x * width + y) + 2] = 255;
}
}
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
View view(512, 512);
view.show();
return app.exec();
}
The code, instead of producing a white window, produces a white window with lines which are the result of round-off error. I think the source of the problem are roundX() and roundY() functions, but I'm not sure. I also don't know how to fix this. Any ideas?
I don't have Qt and I prefer C. So, below is a C program that imitates your render() in render1() and has a corrected implementation in render2().
The main problem of your render() is that it uses prematurely rounded values from Point p(roundX(i), roundY(j));. If you use unrounded i and j, things will improve dramatically, but you're still going to suffer from the rounding errors of your additions, multiplications and so on.
But those rounding errors aren't large and they don't accumulate to a large error in the end. So, the final correction is the addition of .5 prior to conversion to the integers x and y. Without it, the floating point value can be slightly less than the nearest integer that you want to get and when you convert this double into int, you get a value that is one less.
#include <stdio.h>
#include <string.h>
#include <math.h>
double centerx, centery, scale;
double xmin, xmax, ymin, ymax;
double xprec, yprec;
double xratio, yratio;
double fwidth, fheight;
double xlen, ylen;
int width;
int height;
void InitView(int w, int h)
{
double aspectRatio;
width = w; height = h;
// _buffer = new unsigned char[4 * w * h];
fwidth = width;
fheight = height;
aspectRatio = fwidth / fheight;
centerx = 0;
centery = 0;
scale = 2.3;
xlen = aspectRatio * scale;
ylen = 1.0 * scale;
xmin = -(xlen * 0.5) + centerx;
xmax = -xmin;
ymin = -(ylen * 0.5) + centery;
ymax = -ymin;
xprec = xlen / fwidth;
yprec = ylen / fheight;
xratio = fwidth / scale / aspectRatio;
yratio = fheight / scale;
}
double roundX(double x) { return floor(x / xprec) * xprec; }
double roundY(double y) { return floor(y / yprec) * yprec; }
void render1(void)
{
double i, j;
int cnt;
char usedx[1 + 512 + 1];
char usedy[1 + 512 + 1];
printf("render1():\n");
memset(usedx, 0, sizeof usedx);
memset(usedy, 0, sizeof usedy);
printf("x's:\n");
for (cnt = 0, i = xmin; i < xmax; i += xprec)
{
int x = ((roundX(i) * xratio) - (xmin * xratio));
printf("%d ", x);
cnt++;
usedx[1 + x] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedx[cnt])
printf("missing x: %d\n", cnt - 1);
printf("y's:\n");
for (cnt = 0, j = ymin; j < ymax; j += yprec)
{
int y = ((roundY(j) * yratio) - (ymin * yratio));
printf("%d ", y);
cnt++;
usedy[1 + y] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedy[cnt])
printf("missing y: %d\n", cnt - 1);
}
void render2(void)
{
double i, j;
int cnt;
char usedx[1 + 512 + 1];
char usedy[1 + 512 + 1];
printf("render2():\n");
memset(usedx, 0, sizeof usedx);
memset(usedy, 0, sizeof usedy);
printf("x's:\n");
for (cnt = 0, i = xmin; i < xmax; i += xprec)
{
int x = ((i * xratio) - (xmin * xratio) + .5);
printf("%d ", x);
cnt++;
usedx[1 + x] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedx[cnt])
printf("missing x: %d\n", cnt - 1);
printf("y's:\n");
for (cnt = 0, j = ymin; j < ymax; j += yprec)
{
int y = ((j * yratio) - (ymin * yratio) + .5);
printf("%d ", y);
cnt++;
usedy[1 + y] = 1;
}
printf("\ncount: %d\n", cnt);
for (cnt = 1; cnt <= 512; cnt++)
if (!usedy[cnt])
printf("missing y: %d\n", cnt - 1);
}
int main(void)
{
InitView(512, 512);
render1();
render2();
return 0;
}
Output (ideone):
render1():
x's:
0 0 0 2 2 4 5 5 7 7 9 10 10 12 12 14 15 15 17 17 19 20 20 22 22 24 25 25 27 27 29 30 30 32 32 33 35 36 36 37 38 40 41 41 42 43 45 46 46 47 48 50 51 51 52 54 55 56 56 57 59 60 61 61 62 64 65 66 66 67 69 70 71 71 72 74 75 76 76 77 79 80 81 81 82 84 85 86 86 87 89 90 91 91 92 94 95 95 96 97 99 100 100 101 103 104 105 105 106 108 109 110 110 111 113 114 115 115 116 118 119 120 120 121 123 124 125 125 126 128 129 130 130 131 133 134 135 135 136 138 139 140 140 141 143 144 145 146 146 148 148 150 151 152 153 153 155 156 157 158 158 160 162 163 163 165 166 167 168 168 170 171 172 173 174 175 175 177 178 179 180 180 182 183 184 185 185 187 188 189 190 190 192 193 194 195 195 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 371 373 374 375 376 376 378 379 380 381 381 383 384 385 386 386 388 389 390 391 391 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 420 422 423 424 425 425 427 428 429 430 430 432 433 434 435 435 437 438 439 440 440 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 479 481 482 482 484 484 486 487 487 489 489 491 492 492 494 495 496 497 497 499 500 501 502 502 504 505 506 507 507 509 510 511
count: 512
missing x: 1
missing x: 3
missing x: 6
missing x: 8
missing x: 11
missing x: 13
missing x: 16
missing x: 18
missing x: 21
missing x: 23
missing x: 26
missing x: 28
missing x: 31
missing x: 34
missing x: 39
missing x: 44
missing x: 49
missing x: 53
missing x: 58
missing x: 63
missing x: 68
missing x: 73
missing x: 78
missing x: 83
missing x: 88
missing x: 93
missing x: 98
missing x: 102
missing x: 107
missing x: 112
missing x: 117
missing x: 122
missing x: 127
missing x: 132
missing x: 137
missing x: 142
missing x: 147
missing x: 149
missing x: 154
missing x: 159
missing x: 161
missing x: 164
missing x: 169
missing x: 176
missing x: 181
missing x: 186
missing x: 191
missing x: 196
missing x: 372
missing x: 377
missing x: 382
missing x: 387
missing x: 392
missing x: 421
missing x: 426
missing x: 431
missing x: 436
missing x: 441
missing x: 480
missing x: 483
missing x: 485
missing x: 488
missing x: 490
missing x: 493
missing x: 498
missing x: 503
missing x: 508
y's:
0 0 0 2 2 4 5 5 7 7 9 10 10 12 12 14 15 15 17 17 19 20 20 22 22 24 25 25 27 27 29 30 30 32 32 33 35 36 36 37 38 40 41 41 42 43 45 46 46 47 48 50 51 51 52 54 55 56 56 57 59 60 61 61 62 64 65 66 66 67 69 70 71 71 72 74 75 76 76 77 79 80 81 81 82 84 85 86 86 87 89 90 91 91 92 94 95 95 96 97 99 100 100 101 103 104 105 105 106 108 109 110 110 111 113 114 115 115 116 118 119 120 120 121 123 124 125 125 126 128 129 130 130 131 133 134 135 135 136 138 139 140 140 141 143 144 145 146 146 148 148 150 151 152 153 153 155 156 157 158 158 160 162 163 163 165 166 167 168 168 170 171 172 173 174 175 175 177 178 179 180 180 182 183 184 185 185 187 188 189 190 190 192 193 194 195 195 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 371 373 374 375 376 376 378 379 380 381 381 383 384 385 386 386 388 389 390 391 391 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 420 422 423 424 425 425 427 428 429 430 430 432 433 434 435 435 437 438 439 440 440 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 479 481 482 482 484 484 486 487 487 489 489 491 492 492 494 495 496 497 497 499 500 501 502 502 504 505 506 507 507 509 510 511
count: 512
missing y: 1
missing y: 3
missing y: 6
missing y: 8
missing y: 11
missing y: 13
missing y: 16
missing y: 18
missing y: 21
missing y: 23
missing y: 26
missing y: 28
missing y: 31
missing y: 34
missing y: 39
missing y: 44
missing y: 49
missing y: 53
missing y: 58
missing y: 63
missing y: 68
missing y: 73
missing y: 78
missing y: 83
missing y: 88
missing y: 93
missing y: 98
missing y: 102
missing y: 107
missing y: 112
missing y: 117
missing y: 122
missing y: 127
missing y: 132
missing y: 137
missing y: 142
missing y: 147
missing y: 149
missing y: 154
missing y: 159
missing y: 161
missing y: 164
missing y: 169
missing y: 176
missing y: 181
missing y: 186
missing y: 191
missing y: 196
missing y: 372
missing y: 377
missing y: 382
missing y: 387
missing y: 392
missing y: 421
missing y: 426
missing y: 431
missing y: 436
missing y: 441
missing y: 480
missing y: 483
missing y: 485
missing y: 488
missing y: 490
missing y: 493
missing y: 498
missing y: 503
missing y: 508
render2():
x's:
0 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
count: 512
y's:
0 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
count: 512