Why does it give a "multiple test case" error? - c++

I've written the code for eliminating the largest 2 elements of an array, but this code gives junk value for testcase > 1. Why?
Input:
no of TestCase
size of array
elements of array
Sorting function:
int sort_asc(int arr[], int n)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[i])
{
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
int main() {
//code
int test;
cin>>test;
while(test--){
//taking size and array as inputs
int size;
cin>>size;
int a[size];
cin>>a[size];
for(int i=0;i<size;i++){
cin>>a[i];
}
//sorting the array
sort_asc(a,size);
//printing the output discarding last 2 elements of the array
for(int i=0;i<size-2;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
}
return 0;
}
Expected:
12 23 28 43 44 59 60 68 70 85 88 92 124 125 136 168 171 173 179 199 212
230 277 282 306 314 316 325 328 336 337 363 365 368 369 371 374 387 394 414
422 427 430 435 457 493 506 527 531 538 541 546 568 583 650 691 730 737 751
764 778 783 785 789 794 803 809 815 847 858 863 874 887 896 916 920 926 927 930 957
My output:
12 23 28 43 44 59 60 68 70 81 85 88 92 124 125 136 168 171 173 179 199 212 230 277 282 306 314 316 325 328 336 337 363 365 368 369 371 374 387 394 414 422 427 430 435 457 493 506 527 531 538 541 546 568 583 650 691 730 737 751 764 778 783 785 789 794 803 809 815 847 858 863 874 887 896 916 920 926 930 957

A VLA (variable length array) is invalid C++ code. It is tolerated by some compilers, but it is still invalid.
But that is not your main problem. You produced an out of bound error. An array index starts with 0. The last element is at position size-1. So your statement
cin>>a[size];
will write past the end of your array. Producing undefined behavior.
I am not sure, why you put the statement at all, but after that, anything undefined can and most probably will happen.

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?

Is it possible to stop a parallel process in CUDA [duplicate]

I am working with CUDA and I am trying to stop my kernels work (i.e. terminate all running threads) after a certain if block is being hit. How can I do that? I am really stuck in here.
The CUDA execution model doesn't allow for inter-block communication by design. That can potentially make this sort of kernel abort on condition operation difficult to achieve reliably without resorting to the assert or trap type approaches which can potentially result in context destruction and loss of data which isn't what you probably want.
If your kernel design involves a small number of blocks with "resident" threads, then the only approach is some sort of atomic spinlock, which is hard to get to work reliably, and which will greatly degrade memory controller performance and achievable bandwidth.
If, on the other hand, your kernel design has rather large grids with a lot of blocks, and your main goal is to stop blocks which are not yet scheduled from running, then you could try something like this:
#include <iostream>
#include <vector>
__device__ unsigned int found_idx;
__global__ void setkernel(unsigned int *indata)
{
indata[115949] = 0xdeadbeef;
indata[119086] = 0xdeadbeef;
indata[60534] = 0xdeadbeef;
indata[37072] = 0xdeadbeef;
indata[163107] = 0xdeadbeef;
}
__global__ void searchkernel(unsigned int *indata, unsigned int *outdata)
{
if (found_idx > 0) {
return;
} else if (threadIdx.x == 0) {
outdata[blockIdx.x] = blockIdx.x;
};
unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x;
if (indata[tid] == 0xdeadbeef) {
unsigned int oldval = atomicCAS(&found_idx, 0, 1+tid);
}
}
int main()
{
const unsigned int N = 1 << 19;
unsigned int* in_data;
cudaMalloc((void **)&in_data, sizeof(unsigned int) * size_t(N));
cudaMemset(in_data, 0, sizeof(unsigned int) * size_t(N));
setkernel<<<1,1>>>(in_data);
cudaDeviceSynchronize();
unsigned int block_size = 1024;
unsigned int grid_size = N / block_size;
unsigned int* out_data;
cudaMalloc((void **)&out_data, sizeof(unsigned int) * size_t(grid_size));
cudaMemset(out_data, 0xf0, sizeof(unsigned int) * size_t(grid_size));
const unsigned int zero = 0;
cudaMemcpyToSymbol(found_idx, &zero, sizeof(unsigned int));
searchkernel<<<grid_size, block_size>>>(in_data, out_data);
std::vector<unsigned int> output(grid_size);
cudaMemcpy(&output[0], out_data, sizeof(unsigned int) * size_t(grid_size), cudaMemcpyDeviceToHost);
cudaDeviceReset();
std::cout << "The following blocks did not run" << std::endl;
for(int i=0, j=0; i<grid_size; i++) {
if (output[i] == 0xf0f0f0f0) {
std::cout << " " << i;
if (j++ == 20) {
std::cout << std::endl;
j = 0;
}
}
}
std::cout << std::endl;
return 0;
}
Here I have a simple kernel which is searching for a magic word in a large array. To get the early exit behaviour, I use a single global word, which is set atomically by those threads which "win" or trigger the termination condition. Every new block checks the state of this global word, and if it is set, they return without doing any work.
If I compile and run this on a moderate sized Kepler device:
$ nvcc -arch=sm_30 -o blocking blocking.cu
$ ./blocking
The following blocks did not run
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
you can see that a large number of blocks in the grid saw the change in the global word and early terminated without running the search code. This might be the best you can do without a severely invasive spinlock approach which will greatly harm performance.
I assume you want to stop a running kernel (not a single thread).
The simplest approach (and the one that I suggest) is to set up a global memory flag which is been tested by the kernel.
You can set the flag using cudaMemcpy() (or without if using unified memory).
Like the following:
if (gm_flag) {
__threadfence(); // ensure store issued before trap
asm("trap;"); // kill kernel with error
}
ams("trap;") will stop all running thread
Note that since cuda 2.0 you can use assert() to terminate a kernel!
A different approach could be the following (I haven't tried the code!)
__device__ bool go(int val){
return true;
}
__global__ void stopme(bool* flag, int* val, int size){
int idx= blockIdx.x *blockDim.x + threadIdx.x;
if(idx < size){
bool canContinue = true;
while(canContinue && (flag[0])){
printf("HELLO from %i\n",idx);
if(!(*flag)){
return;
}
else{
//do some computation
val[idx]++;
val[idx]%=100;
}
canContinue = go(val[idx]);
}
}
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
int main(void)
{
int size = 128;
int* h_val = (int*)malloc(sizeof(int)*size);
bool * h_flag = new bool;
*h_flag=true;
bool* d_flag;
cudaMalloc(&d_flag,sizeof(bool));
cudaMemcpy(d_flag,h_flag,1,cudaMemcpyHostToDevice);
int* d_val;
cudaMalloc(&d_val,sizeof(int)*size );
for(int i=0;i<size;i++){
h_val[i] = i;
}
cudaMemcpy(d_val,h_val,size,cudaMemcpyHostToDevice);
int BSIZE=32;
int nblocks =size/BSIZE;
printf("%i,%i",nblocks,BSIZE);
stopme<<<nblocks,BSIZE>>>(d_flag,d_val,size);
//--------------sleep for a while --------------------------
*h_flag=false;
cudaMemcpy(d_flag,h_flag,1,cudaMemcpyHostToDevice);
cudaDeviceSynchronize();
gpuErrchk( cudaPeekAtLastError() );
printf("END\n");
}
where the kernel stopMe keeps running until someone from the host side sets up the flag to false. Note that your kernel could be much more complicated than this and the effort to synchronize all threads in order to execute the return could be much more than this (and can affect performance). Hope this helped.
More info here

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);
}

Different output for the same input data

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