Shuffling vectors in C++ - c++

I am working on a programme which takes an initial vector as an inpit.This vector is vector of size 20.The programme then generates 10 random vectors from this vector.For this purpose I choose 2 ransom indices in the initial vector and swap them with each other to generate a new vector.This is done to generate all the 10 new vectors.
The 10 new vectors generated should be stored in the following 2 dimensional vector
vector<vector<int>> allparents
I have been able to generate 2 random indice numbers using the srand() function and then swap the elements at these indices for the initial vector.However I am unable to generate 10 of such random parents and then store them in the allparents 2D vector.My code is as follows :
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<ctime>
using namespace std;
int main () {
srand(time(NULL));
int i1=(rand()%19+1);
int i2=(rand()%19+1);
cout<<i1<<endl;
cout<<i2<<endl;
vector<int> initial={71,127,428,475,164,253,229,395,92,189,41,110,443,490,278,305,28,58,371,560};
vector<vector<int>> allparents;
for(int r=0;r<10;r++){
for(int c=0;c<20;c++){
swap(initial[i1],initial[i2]);
allparents[r][c]=initial[c];
cout<<allparents[r][c]<" "<<endl;
}
}
return 0;
}
As I am new to vectors,I would request your help in this programme.Thanks.

First of all, I would not say that you're "generating random vectors", you're just shuffling a predefined one.
Second, I suggest to create small working functions and assemble your program with those:
vector<int> shuffle(vector<int> v) {
// Use the implementation you want here, I will use a std algorithm
static auto rng = std::default_random_engine {};
std::shuffle(std::begin(v), std::end(v), rng);
return v; // this is a copy of the vector
}
int main() {
vector<int> initial= {
71,127,428,475,164,253,229,395,92,189,41,110,443,490,278,305,28,58,371,560
};
// Generate 10 shuffled vectors
vector<vector<int>> shuffledVectors;
for (int i = 0; i < 10; i++) {
vector<int> shuffled = shuffle(initial);
shuffledVectors.push_back(shuffled);
}
// Print them
for (vector<int>& v : shuffledVectors) {
for (int& i : v)
cout << i << " ";
cout << endl;
}
return 0;
}
Output:
164 41 110 305 278 28 58 127 229 189 475 395 560 428 71 443 253 371 490 92
490 71 305 58 428 127 28 110 92 443 189 229 278 475 371 395 560 41 253 164
395 278 560 490 28 164 71 229 58 41 428 305 127 253 475 371 92 189 110 443
164 475 92 253 229 189 127 560 71 58 41 443 428 395 371 490 110 278 28 305
443 253 428 110 278 71 475 127 58 41 371 229 305 189 395 164 28 490 92 560
560 28 58 71 229 41 490 475 189 443 253 395 305 164 371 278 428 92 110 127
395 443 371 58 253 305 92 127 475 110 428 229 189 41 164 278 71 560 28 490
278 189 71 127 443 110 28 428 305 560 371 58 229 253 395 164 41 490 475 92
28 395 92 443 560 278 371 71 58 305 475 253 428 490 229 189 164 110 41 127
443 71 428 229 127 278 490 58 475 253 164 110 92 189 395 560 305 41 28 371

Related

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.

Question about tabling and declaring functions in main

the problem is in the int main()
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
const int R = 15;
const int C = 20;
void clearArray(int arr[R][C])
{
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
arr[i][j] = 0;
}
}
}
void makeSpiral(int arr[R][C],int r , int c)
{
int i, a = 0, b = 0;
int value = 1;
while (a < r && b < c)
{
for (i = a; i < r; ++i)
{
arr[i][b] = value++;
}
b++;
for (i = b; i < c; ++i)
{
arr[r - 1][i] = value++;
}
r--;
if (a < r)
{
for (i = r - 1; i >= a; --i)
{
arr[i][c-1] = value++;
}
c--;
}
if (b < c)
{
for (i = c - 1; i >= b; --i)
{
arr[a][i] = value++;
}
a++;
}
}
}
void printSpiral(int arr[R][C],int r , int c, char str[])
{
str[30];
ofstream fout;
fout.open(str, ios::out | ios::app);
if (!fout)
{
cout << "Error: file could not be opened" << endl;
exit(1);
}
fout << endl;
fout << "---------------------------------------------";
for (int i = 0; i < r; i++)
{
fout << endl;
for (int j = 0; j < c; j++)
{
fout <<setw(5)<< arr[i][j] << " ";
}
}
fout.close();
}
int main(int argc, char *argv[])
{
char str[30];
ofstream fout;
int r,c;
int i,j,t;
int arr[R][C];
if (argc < 2)
{
cout << "Error missing file name!" << endl;
return -1;
}
strcpy(str, argv[1]);
int matrix [R][C] = {{1,1},{2,2},{3,3},{4,4},{5,5},{4,7},{7,4},{15,20}};
for (i = 0 ; i < 8 ; i++)
{
clearArray(matrix);
makeSpiral(matrix,R,C);
printSpiral (matrix,R,C,str);
}
return 0;
}
I'm a new C++ programmer please go easy on me. So i have this piece of code, the professor requires me to store corner sizes of a spiral matrix into a table, then use a loop to call out my functions so i don't have to call my functions repeatedly like this:
clearArray(matrix);
makeSpiral(matrix,1,1);
printSpiral (matrix,1,1,str);
clearArray(matrix);
makeSpiral(matrix,2,2);
printSpiral (matrix,2,2,str);
clearArray(matrix);
makeSpiral(matrix,3,3);
printSpiral (matrix,3,3,str);
clearArray(matrix);
makeSpiral(matrix,4,4);
printSpiral (matrix,4,4,str);
I want to print a counter-clockwise matrix 8 times which with corner 1 by 1 ; 2 by 2; 3 by 3; 4 by 4; 5 by 5; 4 by 7; 7 by 4; and 15 by 20.
My code doesn't give me the correct output, can you tell me where I did wrong, thank you!
The output that i supposed to have
---------------------------------
1
---------------------------------
1 3
---------------------------------
3
2
---------------------------------
1 4
2 3
---------------------------------
1 8 7
2 9 6
3 4 5
---------------------------------
1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7
---------------------------------
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
---------------------------------
1 18 17 16 15 14 13
2 19 28 27 26 25 12
3 20 21 22 23 24 11
4 5 6 7 8 9 10
---------------------------------
1 18 17 16
2 19 28 15
3 20 27 14
4 21 26 13
5 22 25 12
6 23 24 11
7 8 9 10
---------------------------------
1 20 19 18 17 16 15 14
2 21 32 31 30 29 28 13
3 22 23 24 25 26 27 12
4 5 6 7 8 9 10 11
---------------------------------
1 16 15 14
2 17 24 13
3 18 23 12
4 19 22 11
5 20 21 10
6 7 8 9
What my code output looks like:
---------------------------------------------------------
1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47
3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46
4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45
5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44
6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43
7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42
8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41
9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40
10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39
11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38
12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37
13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36
14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
------------------------------------------------------------
1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47
3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46
4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45
5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44
6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43
7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42
8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41
9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40
10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39
11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38
12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37
13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36
14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
---------------------------------------------------------
1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 47
3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161 160 107 46
4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204 159 106 45
5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203 158 105 44
6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202 157 104 43
7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201 156 103 42
8 73 130 179 220 253 278 295 296 297 298 299 300 287 266 237 200 155 102 41
9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199 154 101 40
10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198 153 100 39
11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197 152 99 38
12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196 151 98 37
13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 97 36
14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
--------------------------------------------------------
......
It prints the 15x20 corner size 8 times.
In your main function, arguments to makeSpiral are fixed R and C. This means makeSpiral always tries to fill a 15 rows, 20 columns spiral matrix to your matrix:
makeSpiral(matrix,R,C);
printSpiral(matrix,R,C,str);
You may want this instead:
int matrix[R][C]; // no need of initialization
int matrixSize[8][2] = {{1,1},{2,2},{3,3},{4,4},{5,5},{4,7},{7,4},{15,20}};
for (i = 0 ; i < 8 ; i++) {
clearArray(matrix, matrixSize[i][0], matrixSize[i][1]);
makeSpiral(matrix, matrixSize[i][0], matrixSize[i][1]);
printSpiral (matrix, matrixSize[i][0], matrixSize[i][1], str);
}
Here you use an assist matrixSize to store expected matrix sizes. Within the for loop makeSpiral will fill matrixSize[i][0] rows, matrixSize[i][1] columns only, so does printSpiral.

reading file using istringstream, unexpected output

I have a .txt file with tab separated data(several rows):
42 2 62
2 2 42
2 2 62
I tried to use istringstream to read the data from the file, and then use each column's data for each respective row as locations for adding 1's into a 2-D array.
This was to loop until the 2-D array was filled with 1's for each position designated by the loop(rows) and the file data(columns).
Note: the second column is not used, but is put in since this is how the .txt file appears.
I print out each row one by one, and expect there to be three 1's in each row, for all of the rows.
However when I print my output for the 2-D array(code block near bottom), I get unexpected results. For some rows, there will be only two 1's within the row, and for some there will be four 1's.
I am not a very good coder, so any advice would be appreciated, the code is posted below.
#include "stdafx.h"
using namespace std;
//prints the diagonal of array
void printarray(int arg[][300], int length) {
for (int n = 0; n<length; ++n)
cout << arg[n][n] << ' ';
cout << '\n';
}
//makes 2d array values all 0
void zeros(int a[][300], int size)
{
cout << "Setting all elements of array to zero...." << endl;
for (int k = 0; k < (size*size); k++)
{
for (int i = 0; i<size; i++)
{
for (int j = 0; j<size; j++)
{
a[i][j] = 0;
}
}
}
}
int main()
{
//2D array of 300by300
int adMatrix [300][300];
zeros(adMatrix, 300);
int counter = 0;
string line;
int firstVar,secondVar,thirdVar; // variables to hold the bond neighbors while looping
// Read sample file
ifstream infile("myTestFile.txt");
while (counter < 300 ) {
getline(infile, line);
istringstream stream(line);
stream >> firstVar >> secondVar >> thirdVar;
adMatrix[counter][firstVar] = 1;
adMatrix[counter][thirdVar] = 1;
stream.str("");
stream.clear();
getline(infile, line);
istringstream stream2(line);
stream2 >> firstVar >> secondVar >> thirdVar;
adMatrix[counter][firstVar] = 1;
adMatrix[counter][thirdVar] = 1;
stream2.str("");
stream2.clear();
getline(infile, line);
istringstream stream3(line);
stream3 >> firstVar >> secondVar >> thirdVar;
adMatrix[counter][firstVar] = 1;
adMatrix[counter][thirdVar] = 1;
stream3.str("");
stream3.clear();
counter++;
}
// iterates through entire matrix, prints values, also tallys sum of each row, should equal 3 for each row.
int var = 0;
for (int i = 0; i < 300; ++i)
{
int sum = 0;
for (int i = 0; i < 300; ++i)
{
cout << adMatrix[var][i];
sum = adMatrix[var][i] + sum;
}
var++;
cout << endl << "sum is : " << sum << endl;
}
return 0;
}
Here is the data for .txt file I used:
43 1 61
2 1 43
2 1 61
1 2 3
1 2 11
3 2 11
2 3 45
4 3 45
2 3 4
3 4 13
5 4 13
3 4 5
4 5 6
6 5 162
4 5 162
15 6 123
5 6 15
5 6 123
59 7 77
8 7 59
8 7 77
7 8 9
7 8 21
9 8 21
8 9 61
10 9 61
8 9 10
9 10 23
11 10 23
9 10 11
2 11 12
10 11 12
2 11 10
13 12 25
11 12 25
11 12 13
12 13 14
4 13 14
4 13 12
15 14 27
13 14 27
13 14 15
14 15 16
6 15 16
6 15 14
29 16 133
15 16 29
15 16 133
75 17 93
18 17 75
18 17 93
17 18 19
17 18 34
19 18 34
18 19 77
20 19 77
18 19 20
19 20 36
21 20 36
19 20 21
8 21 22
20 21 22
8 21 20
23 22 38
21 22 38
21 22 23
22 23 24
10 23 24
10 23 22
25 24 40
23 24 40
23 24 25
24 25 26
12 25 26
12 25 24
27 26 42
25 26 42
25 26 27
26 27 28
14 27 28
14 27 26
27 28 29
29 28 44
27 28 44
16 29 28
28 29 158
16 29 158
91 30 109
31 30 91
31 30 109
30 31 32
30 31 50
32 31 50
31 32 93
33 32 93
31 32 33
32 33 52
34 33 52
32 33 34
18 34 35
33 34 35
18 34 33
36 35 54
34 35 54
34 35 36
35 36 37
20 36 37
20 36 35
38 37 56
36 37 56
36 37 38
37 38 39
22 38 39
22 38 37
40 39 58
38 39 58
38 39 40
39 40 41
24 40 41
24 40 39
40 41 42
42 41 60
40 41 60
26 42 41
41 42 43
26 42 43
42 43 44
1 43 42
1 43 44
28 44 43
28 44 45
43 44 45
3 45 44
44 45 157
3 45 157
107 46 122
47 46 107
47 46 122
46 47 48
46 47 66
48 47 66
47 48 109
49 48 109
47 48 49
48 49 68
50 49 68
48 49 50
31 50 51
49 50 51
31 50 49
52 51 70
50 51 70
50 51 52
51 52 53
33 52 53
33 52 51
54 53 72
52 53 72
52 53 54
53 54 55
35 54 55
35 54 53
56 55 74
54 55 74
54 55 56
55 56 57
37 56 57
37 56 55
56 57 58
58 57 76
56 57 76
39 58 57
57 58 59
39 58 59
58 59 60
7 59 58
7 59 60
41 60 59
41 60 61
59 60 61
1 61 60
9 61 60
1 61 9
120 62 132
63 62 120
63 62 132
62 63 64
62 63 82
64 63 82
63 64 122
65 64 122
63 64 65
64 65 84
66 65 84
64 65 66
47 66 67
47 66 65
65 66 67
68 67 86
66 67 86
66 67 68
67 68 69
49 68 69
49 68 67
70 69 88
68 69 88
68 69 70
69 70 71
51 70 71
51 70 69
72 71 90
70 71 90
70 71 72
71 72 73
53 72 73
53 72 71
72 73 74
74 73 92
72 73 92
73 74 75
55 74 73
55 74 75
74 75 76
17 75 74
17 75 76
57 76 75
57 76 77
75 76 77
7 77 76
19 77 76
7 77 19
130 78 138
79 78 130
79 78 138
78 79 80
78 79 98
80 79 98
79 80 132
81 80 132
79 80 81
80 81 100
80 81 82
82 81 100
63 82 83
63 82 81
81 82 83
84 83 102
82 83 102
82 83 84
83 84 85
65 84 85
65 84 83
86 85 104
84 85 104
84 85 86
85 86 87
67 86 87
67 86 85
88 87 106
86 87 106
86 87 88
87 88 89
69 88 89
69 88 87
90 89 108
88 89 90
88 89 108
71 90 89
89 90 91
71 90 91
90 91 92
30 91 90
30 91 92
73 92 91
73 92 93
91 92 93
17 93 92
32 93 92
17 93 32
95 94 136
136 94 160
95 94 160
94 95 96
94 95 111
96 95 111
95 96 138
97 96 138
95 96 97
96 97 113
96 97 98
98 97 113
79 98 99
79 98 97
97 98 99
100 99 115
98 99 115
98 99 100
99 100 101
81 100 101
81 100 99
102 101 117
100 101 117
100 101 102
101 102 103
83 102 103
83 102 101
104 103 119
102 103 119
102 103 104
103 104 105
85 104 105
85 104 103
106 105 121
104 105 106
104 105 121
105 106 107
87 106 105
87 106 107
106 107 108
46 107 106
46 107 108
89 108 107
89 108 109
107 108 109
30 109 108
48 109 108
30 109 48
111 110 123
123 110 161
111 110 161
95 111 112
95 111 110
110 111 112
113 112 125
111 112 125
111 112 113
112 113 114
97 113 114
97 113 112
115 114 127
113 114 127
113 114 115
114 115 116
99 115 116
99 115 114
117 116 129
115 116 129
115 116 117
116 117 118
101 117 118
101 117 116
119 118 131
117 118 119
117 118 131
103 119 118
118 119 120
103 119 120
119 120 121
62 120 119
62 120 121
105 121 120
105 121 122
120 121 122
46 122 121
64 122 121
46 122 64
6 123 124
110 123 124
6 123 110
125 124 133
123 124 133
123 124 125
124 125 126
112 125 126
112 125 124
127 126 135
125 126 135
125 126 127
126 127 128
114 127 128
114 127 126
127 128 129
129 128 137
127 128 137
128 129 130
116 129 128
116 129 130
129 130 131
78 130 129
78 130 131
118 131 130
118 131 132
130 131 132
62 132 131
80 132 131
62 132 80
16 133 134
124 133 134
16 133 124
133 134 135
135 134 159
133 134 159
126 135 134
134 135 136
126 135 136
135 136 137
94 136 135
94 136 137
128 137 136
128 137 138
136 137 138
78 138 137
96 138 137
78 138 96
141 139 156
141 139 220
156 139 220
141 140 157
141 140 158
157 140 158
139 141 142
140 141 142
139 141 140
141 142 144
141 142 283
144 142 283
144 143 158
144 143 159
158 143 159
142 144 145
142 144 143
143 144 145
144 145 147
147 145 284
144 145 284
147 146 159
147 146 160
159 146 160
145 147 148
146 147 148
145 147 146
147 148 150
147 148 215
150 148 215
150 149 160
150 149 161
160 149 161
148 150 151
149 150 151
148 150 149
150 151 153
153 151 164
150 151 164
153 152 161
153 152 162
161 152 162
151 153 154
151 153 152
152 153 154
153 154 156
153 154 163
156 154 163
156 155 157
156 155 162
157 155 162
139 156 154
154 156 155
139 156 155
140 157 155
45 157 155
45 157 140
140 158 143
29 158 143
29 158 140
143 159 146
134 159 143
134 159 146
146 160 149
94 160 149
94 160 146
149 161 152
110 161 149
110 161 152
152 162 155
5 162 155
5 162 152
165 163 184
154 163 165
154 163 184
166 164 185
151 164 166
151 164 185
163 165 181
163 165 187
181 165 187
164 166 182
164 166 188
182 166 188
169 167 183
169 167 189
183 167 189
170 168 186
170 168 190
186 168 190
167 169 191
167 169 193
191 169 193
168 170 192
168 170 194
192 170 194
173 171 195
173 171 196
195 171 196
174 172 197
174 172 198
197 172 198
171 173 199
171 173 201
199 173 201
172 174 200
172 174 203
200 174 203
177 175 202
177 175 205
202 175 205
178 176 204
178 176 206
204 176 206
175 177 207
175 177 211
207 177 211
176 178 208
176 178 212
208 178 212
180 179 209
180 179 213
209 179 213
179 180 210
179 180 214
210 180 214
165 181 183
183 181 220
165 181 220
166 182 186
186 182 215
166 182 215
167 183 181
181 183 216
167 183 216
163 184 185
185 184 218
163 184 218
164 185 184
184 185 219
164 185 219
168 186 182
182 186 217
168 186 217
165 187 189
189 187 221
165 187 221
166 188 190
190 188 222
166 188 222
167 189 187
187 189 225
167 189 225
168 190 188
188 190 226
168 190 226
169 191 195
169 191 223
195 191 223
170 192 197
197 192 224
170 192 224
169 193 196
196 193 227
169 193 227
170 194 198
198 194 228
170 194 228
171 195 191
191 195 231
171 195 231
171 196 193
193 196 229
171 196 229
172 197 192
192 197 232
172 197 232
172 198 194
194 198 230
172 198 230
173 199 202
202 199 233
173 199 233
174 200 204
204 200 234
174 200 234
173 201 205
205 201 235
173 201 235
175 202 199
199 202 237
175 202 237
174 203 206
206 203 236
174 203 236
176 204 200
200 204 238
176 204 238
175 205 201
201 205 243
175 205 243
176 206 203
203 206 244
176 206 244
177 207 209
177 207 239
209 207 239
178 208 210
210 208 240
178 208 240
179 209 207
207 209 241
179 209 241
180 210 208
208 210 242
180 210 242
177 211 213
177 211 245
213 211 245
178 212 214
214 212 246
178 212 246
179 213 211
211 213 247
179 213 247
180 214 212
212 214 248
180 214 248
182 215 250
148 215 182
148 215 250
183 216 223
183 216 251
223 216 251
186 217 224
224 217 254
186 217 254
184 218 221
184 218 252
221 218 252
185 219 222
222 219 253
185 219 253
181 220 249
139 220 181
139 220 249
187 221 218
218 221 255
187 221 255
188 222 219
219 222 256
188 222 256
191 223 216
216 223 259
191 223 259
192 224 217
217 224 260
192 224 260
189 225 227
227 225 257
189 225 257
190 226 228
228 226 258
190 226 258
193 227 225
225 227 261
193 227 261
194 228 226
226 228 262
194 228 262
196 229 233
196 229 264
233 229 264
198 230 234
234 230 266
198 230 266
195 231 235
195 231 263
235 231 263
197 232 236
236 232 265
197 232 265
199 233 229
229 233 267
199 233 267
200 234 230
230 234 268
200 234 268
201 235 231
231 235 269
201 235 269
203 236 232
232 236 271
203 236 271
202 237 239
239 237 270
202 237 270
204 238 240
204 238 272
240 238 272
207 239 237
237 239 275
207 239 275
208 240 238
238 240 276
208 240 276
209 241 242
209 241 277
242 241 277
210 242 241
241 242 278
210 242 278
205 243 245
205 243 273
245 243 273
206 244 246
246 244 274
206 244 274
211 245 243
243 245 279
211 245 279
212 246 244
244 246 280
212 246 280
213 247 248
213 247 281
248 247 281
214 248 247
247 248 282
214 248 282
220 249 251
251 249 285
220 249 285
215 250 254
215 250 286
254 250 286
216 251 249
249 251 287
216 251 287
218 252 253
218 252 283
253 252 283
219 253 252
252 253 284
219 253 284
217 254 250
250 254 288
217 254 288
221 255 257
221 255 285
257 255 285
222 256 258
222 256 286
258 256 286
225 257 255
255 257 287
225 257 287
226 258 256
256 258 288
226 258 288
223 259 263
223 259 289
263 259 289
224 260 265
265 260 290
224 260 290
227 261 264
227 261 289
264 261 289
228 262 266
228 262 290
266 262 290
231 263 259
259 263 291
231 263 291
229 264 261
261 264 291
229 264 291
232 265 260
260 265 292
232 265 292
230 266 262
262 266 292
230 266 292
233 267 270
270 267 293
233 267 293
234 268 272
234 268 294
272 268 294
235 269 273
235 269 293
273 269 293
237 270 267
267 270 295
237 270 295
236 271 274
236 271 294
274 271 294
238 272 268
268 272 296
238 272 296
243 273 269
269 273 295
243 273 295
244 274 271
271 274 296
244 274 296
239 275 277
277 275 297
239 275 297
240 276 278
278 276 298
240 276 298
241 277 275
275 277 299
241 277 299
242 278 276
276 278 300
242 278 300
245 279 281
245 279 297
281 279 297
246 280 282
282 280 298
246 280 298
247 281 279
279 281 299
247 281 299
248 282 280
280 282 300
248 282 300
252 283 285
142 283 252
142 283 285
253 284 286
145 284 253
145 284 286
255 285 283
249 285 283
249 285 255
256 286 284
250 286 284
250 286 256
251 287 257
257 287 289
251 287 289
258 288 290
254 288 290
254 288 258
259 289 287
261 289 287
259 289 261
262 290 288
260 290 288
260 290 262
263 291 264
263 291 293
264 291 293
265 292 266
266 292 294
265 292 294
267 293 291
269 293 291
267 293 269
268 294 292
271 294 292
268 294 271
270 295 273
273 295 297
270 295 297
274 296 298
272 296 298
272 296 274
279 297 295
275 297 295
275 297 279
276 298 296
280 298 296
276 298 280
281 299 300
277 299 300
277 299 281
278 300 299
282 300 299
278 300 282
Here is a picture of the weird output I have been getting. (The output of sums of 2 and 4, when I want sum of 3.) (Also Note there is more than one instance of these sums of 2 and 4 appearing.)
The format of the file looks a bit weird and surely redundant (even without considering the symmetry of the resulting matrix).
The choice of a 2D array of ints to represent a symmetric sparse (1% of non-zero elements, all of value 1) matrix may be a waste of space too, but the question doesn't specify the underlying problem, so I will not speculate any further.
It seems that in the input file rows and column are indexed starting by 1, while in C++ array indeces start by 0, so when a line like
281 299 300
is read by OP's code:
stream >> firstVar >> secondVar >> thirdVar;
adMatrix[counter][firstVar] = 1;
adMatrix[counter][thirdVar] = 1;
The value 300 is off by one and being 2D arrays contiguous in memory, instead of the last element of the row (the second value represent the row number, apparently) the first element of next row is set to 1.
Given a well formatted input file, like the one presented, is also easier to read the values directly from the file stream using operator>>, without using a string stream.
The same snippet can be rewritten and "simplified" like this:
#include <iostream>
#include <fstream>
template <size_t Dim>
bool is_symmetric(int (&arr)[Dim][Dim]);
template <size_t Dim>
bool are_there_exactly_3_nonzeroes_each_row(int (&arr)[Dim][Dim]);
template <size_t Dim>
int nonzeroes_on_diagonal(int (&arr)[Dim][Dim]);
const size_t size = 300;
int main()
{
// Declares a 2D array of 300 by 300 int and initializes it to zero:
int adMatrix [size][size] = {};
std::ifstream infile("myTestFile.txt");
if ( !infile )
{
std::cerr << "Unable to open input file.\n";
return EXIT_FAILURE;
}
// The value in the second column is the line number.
// All the the values in the file are indeces in the range 1 - 300
int first_col, row, second_col;
// This will read all the file, if it is well formatted.
while ( infile >> first_col >> row >> second_col )
{
if ( first_col < 1 or first_col > size
or row < 1 or row > size
or second_col < 1 or second_col > size )
{
std::cerr << "Wrong file format.\n The line with data: "
<< first_col << ' ' << row << ' ' << second_col
<< " have out of range indeces.\n";
return EXIT_FAILURE;
}
adMatrix[row - 1][first_col - 1] = 1;
adMatrix[row - 1][second_col - 1] = 1;
}
// Print values or ...
if ( is_symmetric(adMatrix) == false )
{
std::cerr << "The matrix isn't symmetric.\n";
}
if ( are_there_exactly_3_nonzeroes_each_row(adMatrix) == false )
{
std::cerr << "There aren't exactly three non zero elements each row.\n";
}
std::cout << "Number of non zero elements in the main diagonal: "
<< nonzeroes_on_diagonal(adMatrix) << '\n';
}
template <size_t Dim>
bool is_symmetric(int (&arr)[Dim][Dim])
{
for ( size_t i = 0; i < Dim; ++i )
{
for ( size_t j = i + 1; j < Dim; ++j )
{
if ( arr[i][j] != arr[j][i] )
return false;
}
}
return true;
}
template <size_t Dim>
bool are_there_exactly_3_nonzeroes_each_row(int (&arr)[Dim][Dim])
{
for ( size_t i = 0; i < Dim; ++i )
{
int count = 0;
for ( size_t j = 0; j < Dim; ++j )
{
if ( arr[i][j] != 0 )
++count;
}
if ( count != 3 )
return false;
}
return true;
}
template <size_t Dim>
int nonzeroes_on_diagonal(int (&arr)[Dim][Dim])
{
int count = 0;
for ( size_t i = 0; i < Dim; ++i )
{
if ( arr[i][i] != 0 )
{
++count;
}
}
return count;
}
This will fix the issue, but for a more general solution and to really improve your language knowledge it would be far more useful to start learning about classes, the C++ Standard Library, its containers and generic functions.

Getline puts program on pause

When "text" is larger than about 280 numbers the program waits ...
With any "text" of 280 numbers, it works fine.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string text;
getline (cin, text);
cout << text;
}
eg:
167 214 280 265 278 292 196 249 242 297 7 125 151 4 25 172 293 157 290 277 240 155 201 90 44 230 94 185 184 65 189 159 74 30 59 279 169 136 142 80 46 124 66 203 138 182 171 241 267 294 32 233 165 39 149 181 156 170 137 96 130 238 239 37 298 48 288 6 100 174 268 144 272 109 275 190 160 154 57 15 83 16 183 236 95 97 147 215 77 34 219 91 68 81 52 207 187 105 229 153 243 20 71 53 3 102 259 13 115 123 98 193 87 208 120 221 113 261 126 178 111 133 255 36 287 93 228 263 47 227 188 191 295 205 28 82 244 152 281 166 58 192 162 60 256 76 50 179 235 247 282 118 88 212 112 21 273 141 222 56 209 134 237 2 121 104 23 150 194 146 24 300 64 92 78 79 116 108 286 223 70 61 67 284 19 33 173 216 42 164 29 199 63 69 140 132 211 101 103 119 106 198 296 168 224 158 232 27 254 246 262 110 250 225 135 86 26 51 180 231 114 257 75 202 217 251 218 18 89 213 85 220 117 266 206 127 234 197 291 248 14 258 129 226 148 260 84 204 73 299 31 264 276 107 11 145 1 54 200 49 72 177 62 45 163 271 274 270 195 186 252 139 99 55 41 38 253 285 5 176 283 22 122 161 17 175 131 43 289 269 9 40 245 12 10 143 35 210 128 8
The only reason I could imagine your code is pausing is if you were overfilling the space allocated for a string. This seems extremely unlikely. There is a limit to the maximum size for a string based on the size of size_t but typically 32 bits are allocated for size_t... This means that the max number of characters for a string is typically 2^32 - 1 characters. Obviously, you are nowhere near reaching that length.
In normal circumstances, you are far more likely to be bound by the amount of space that your system can allocate for the string. This is dependent on the amount of memory available and how your system breaks it up.
Both of these situations seem extremely unlikely.
In this situation, the issue is most likely an issue with your compiler or platform...
What's your reason for taking in such a long string? Have you considered other approaches like reading in the integers from a text file? You could use a vector to store the integers in a more organized way by reading them in individually from the text file, just a thought.

What's wrong with my MergeSort?

#include <cstring>
#include <stdint.h>
#include <algorithm>
#include <ctime>
#include <iostream>
#include <cstdlib>
using namespace std;
int array[1000];
int temp[1000];
void mergeSort(int start, int count) {
if(count == 1) {
return;
}
int startFirst = start;
int countFirst = count/2;
int startSecond = startFirst + countFirst;
int countSecond = count - countFirst;
mergeSort(startFirst, countFirst);
mergeSort(startSecond, countSecond);
int refFirst = 0;
int refSecond = 0;
for(int i = start; i < start + count; i++){
if(refFirst == countFirst || array[startSecond + refSecond] < array[startFirst + refFirst]) {
temp[i] = array[startSecond + refSecond];
refSecond++;
} else {
temp[i] = array[startFirst + refFirst];
refFirst++;
}
}
memcpy(array + start, temp + start, count*sizeof(int));
}
int main() {
for(int i = 0; i <1000; i++){
array[i] = 1000 - i;
}
mergeSort(0, 1000);
for(int i =0; i <1000; ++i){
cout << array[i] << " ";
}
cout << endl;
return 0;
}
I wrote a simple MergeSort implementation using two arrays and it outputs garbage:
1 2 3 2 5 4 4 3 9 8 8 7 8 7 6 5 17 16 16 15 16 15 14 13 16 15 14 13 12 11 10 9 33 32 32 31 32 31 30 29 32 31 30 29 28 27 26 25 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 64 63 63 62 63 62 61 60 63 62 61 60 59 58 57 56 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 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 126 125 125 124 125 124 123 122 125 124 123 122 121 120 119 118 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 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 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 251 250 250 249 250 249 248 247 250 249 248 247 246 245 244 243 250 249 248 247 246 245 244 243 242 241 240 239 238 237 236 235 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 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 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 1 2 3 2 5 4 4 3 9 8 8 7 8 7 6 5 17 16 16 15 16 15 14 13 16 15 14 13 12 11 10 9 33 32 32 31 32 31 30 29 32 31 30 29 28 27 26 25 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 64 63 63 62 63 62 61 60 63 62 61 60 59 58 57 56 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 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 126 125 125 124 125 124 123 122 125 124 123 122 121 120 119 118 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 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 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 251 250 250 249 250 249 248 247 250 249 248 247 246 245 244 243 250 249 248 247 246 245 244 243 242 241 240 239 238 237 236 235 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 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 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
When merging the arrays you should only write from the second array when it's not exhausted. You should replace array[startSecond + refSecond] < array[startFirst + refFirst] with (refSecond != countSecond && array[startSecond + refSecond] < array[startFirst + refFirst])
Need to check that refSecond != countSecond also. Short circuit evaluation should eliminate the need for an extra set of parenthesis for the &&.
if(refFirst == countFirst || refSecond != countSecond
&& array[startSecond + refSecond] < array[startFirst + refFirst]) {
temp[i] = array[startSecond + refSecond];
refSecond++;
} else {
temp[i] = array[startFirst + refFirst];
refFirst++;
}