I have a dataset That is set up as follows:
Standard
129 106 122 114 121 111 135 106 122 148 102 121 129 101 109 123 109 123 101 119
138 151 137 116 118 118 143 104 119 113 121 98 116 103 132 113 105 127 113 118
109 94 110 119 125 105 106 131 104 126 122 106 118 123 110 134 138 135 131 116
117 123 103 111 120 137 106 112 100 112 128 102 116 118 140 97 122 133 129 127
120 120 127 136 123 112 99 124 129 116 127 123 131 127 109 99 134 128 109 129
New
112 101 109 124 131 97 98 106 115 119 116 125 108 116 111 121 109 124 120 96
102 130 106 112 115 111 122 106 107 109 115 104 125 114 135 127 117 113 98 95
121 116 111 116 118 112 117 114 128 125 104 118 122 123 124 119 110 96 123 124
127 100 121 108 133 118 114 116 125 118 137 115 131 108 100 121 113 116 104 101
126 123 135 116 118 111 101 118 111 125 104 124 132 121 114 132 123 121 121 110
I have only ever read in data to SAS that was in column form. I tried to set it up as two separate raw datasets one Standard, one New and then merge the two.
data blood1;
input Standard;
datalines;
129 106 122 114 121 111 135 106 122 148 102 121 129 101 109 123 109 123 101 119
138 151 137 116 118 118 143 104 119 113 121 98 116 103 132 113 105 127 113 118
109 94 110 119 125 105 106 131 104 126 122 106 118 123 110 134 138 135 131 116
117 123 103 111 120 137 106 112 100 112 128 102 116 118 140 97 122 133 129 127
120 120 127 136 123 112 99 124 129 116 127 123 131 127 109 99 134 128 109 129
;
But this only reads the first number of each row. what would be the best way to read this data in?
You need to add ## to the input statement. That tells SAS not to advance the line pointer after each read.
data blood1;
input Standard ##;
datalines;
129 106 122 114 121 111 135 106 122 148 102 121 129 101 109 123 109 123 101 119
138 151 137 116 118 118 143 104 119 113 121 98 116 103 132 113 105 127 113 118
109 94 110 119 125 105 106 131 104 126 122 106 118 123 110 134 138 135 131 116
117 123 103 111 120 137 106 112 100 112 128 102 116 118 140 97 122 133 129 127
120 120 127 136 123 112 99 124 129 116 127 123 131 127 109 99 134 128 109 129
;
Related
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.
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.
I have an input in format as shown in below Image.I am using the vector graph[200] to read input. Let's suppose program read first number of first row and column (as shown in Image) . Now I want to add all vertices which is connected to first node like graph[1].push_back(next integer in same row) and so on . But when should I stop reading input for particular Node(graph[i]). Because after reading first row I need to add vertices into another graph[i+1] to node's corresponding list . If you don't understand my questions plz have a look to my code.
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[201];
int main()
{
int n=2,i,node,temp;
for(i=1;i<=n;i++)
{
cin>>node;
while(scanf("%d",&temp)!=EOF/*What is the correct conditon to stop loop*/)
{
graph[node].push_back(temp);
}
}
return 0;
}
Input format :
1 37 79 164 155 32 87 39 113 15 18 78 175 140 200 4 160 97 191 100 91 20 69 198 196
20 123 134 10 141 13 12 43 47 3 177 101 179 77 182 117 116 36 103 51 154 162 128 30
3 48 123 134 109 41 17 159 49 136 16 130 141 29 176 2 190 66 153 157 70 114 65 173 104 194 54
14 91 171 118 125 158 76 107 18 73 140 42 193 127 100 84 121 60 81 99 80 150 55 1 35 23 93
5 193 156 102 118 175 39 124 119 19 99 160 75 20 112 37 23 145 135 146 73 35
60 155 56 52 120 131 160 124 119 14 196 144 25 75 76 166 35 87 26 20
7 156 185 178 79 27 52 144 107 78 22 71 26 31 15 56 76 112 39 8 113 93
8 185 155 171 178 108 64 164 53 140 25 100 133 9 52 191 46 20 150 144 39 62 131 42 119 127 31 7
9 91 155 8 160 107 132 195 26 20 133 39 76 100 78 122 127 38 156 191 196
10 190 184 154 49 2 182 173 170 161 47 189 101 153 50 30 109 177 148 179 16 163 116 13 90 185
111 123 134 163 41 12 28 130 13 101 83 77 109 114 21 82 88 74 24 94 48 33
12 161 109 169 21 24 36 65 50 2 101 159 148 54 192 88 47 11 142 43 70 182 177 179 189 194 33
13 161 141 157 44 83 90 181 41 2 176 10 29 116 134 182 170 165 173 190 159 47 82 111 142 72 154 110 21 103 130 11 33 138 152
and so on...
Here is the screenshot of my input format
Assuming you want to read line-by-line basis, you could do following:
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[201];
int main(){
string line;
int n=2,i,node,temp;
for(i=1;i<=n;i++){
getline(cin, line);
istringstream in( line );
in>>node;
while(in>>temp){
graph[node].push_back(temp);
}
}
return 0;
}
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.
#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++;
}