I'm beginner in C
this is my code that prints ascii table in different representation such as decimal, hexadecimal, octal and binary.
and I don't know how to reprint a statement (row) that shows what in column every 20 line or rows.
this is my code
#include <stdio.h>
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
int main () {
unsigned char c = 0; /* for the character */
int n = 0; /* integer for the for loop Iteration */
printf_s("\nthis's ASCII table with all representations: \n");
printf_s("\n deci hex oct binary characters\n"); /* header for the table */
for (c = 0; c <= 255; c++)
{
printf_s ("\n %3d %2X %03o \t", c, c, c, c); /* print Ascii using for loop with multi representations */
for (n = sizeof (c) * CHAR_BIT - 1; n >= 0; n--) /* method to convert decimal to base 2 (binary) */
printf_s ("%d", ((c >> n) & 0x1) ? 1 : 0);
printf_s("%5c", c);
putchar ('\n');
if(c==255)
break; /* to stop in 255 and not making infinite loop (because of unsigned char) */
}
putchar ('\n');
return 0;
}
this is how my output look like
deci hex oct binary characters
0 0 000 00000000
1 1 001 00000001 ☺
2 2 002 00000010 ☻
3 3 003 00000011 ♥
4 4 004 00000100 ♦
5 5 005 00000101 ♣
6 6 006 00000110 ♠
7 7 007 00000111
8 8 010 00001000
9 9 011 00001001
10 A 012 00001010
11 B 013 00001011 ♂
12 C 014 00001100 ♀
13 D 015 00001101
14 E 016 00001110 ♫
15 F 017 00001111 ☼
16 10 020 00010000 ►
17 11 021 00010001 ◄
18 12 022 00010010 ↕
19 13 023 00010011 ‼
20 14 024 00010100 ¶
21 15 025 00010101 §
22 16 026 00010110 ▬
23 17 027 00010111 ↨
24 18 030 00011000 ↑
25 19 031 00011001 ↓
26 1A 032 00011010 →
27 1B 033 00011011
8 1C 034 00011100 ∟
29 1D 035 00011101 ↔
30 1E 036 00011110 ▲
31 1F 037 00011111 ▼
32 20 040 00100000
33 21 041 00100001 !
34 22 042 00100010 "
35 23 043 00100011 #
36 24 044 00100100 $
37 25 045 00100101 %
38 26 046 00100110 &
39 27 047 00100111 '
40 28 050 00101000 (
41 29 051 00101001 )
42 2A 052 00101010 *
43 2B 053 00101011 +
44 2C 054 00101100 ,
45 2D 055 00101101 -
46 2E 056 00101110 .
47 2F 057 00101111 /
48 30 060 00110000 0
49 31 061 00110001 1
50 32 062 00110010 2
51 33 063 00110011 3
52 34 064 00110100 4
53 35 065 00110101 5
54 36 066 00110110 6
55 37 067 00110111 7
56 38 070 00111000 8
57 39 071 00111001 9
58 3A 072 00111010 :
59 3B 073 00111011 ;
60 3C 074 00111100 <
61 3D 075 00111101 =
62 3E 076 00111110 >
63 3F 077 00111111 ?
64 40 100 01000000 #
65 41 101 01000001 A
66 42 102 01000010 B
67 43 103 01000011 C
68 44 104 01000100 D
69 45 105 01000101 E
70 46 106 01000110 F
71 47 107 01000111 G
72 48 110 01001000 H
73 49 111 01001001 I
74 4A 112 01001010 J
75 4B 113 01001011 K
76 4C 114 01001100 L
77 4D 115 01001101 M
78 4E 116 01001110 N
79 4F 117 01001111 O
80 50 120 01010000 P
81 51 121 01010001 Q
82 52 122 01010010 R
83 53 123 01010011 S
84 54 124 01010100 T
85 55 125 01010101 U
86 56 126 01010110 V
87 57 127 01010111 W
88 58 130 01011000 X
89 59 131 01011001 Y
90 5A 132 01011010 Z
91 5B 133 01011011 [
92 5C 134 01011100 \
93 5D 135 01011101 ]
94 5E 136 01011110 ^
95 5F 137 01011111 _
96 60 140 01100000 `
97 61 141 01100001 a
98 62 142 01100010 b
99 63 143 01100011 c
100 64 144 01100100 d
101 65 145 01100101 e
102 66 146 01100110 f
103 67 147 01100111 g
104 68 150 01101000 h
105 69 151 01101001 i
106 6A 152 01101010 j
107 6B 153 01101011 k
108 6C 154 01101100 l
109 6D 155 01101101 m
110 6E 156 01101110 n
111 6F 157 01101111 o
112 70 160 01110000 p
113 71 161 01110001 q
114 72 162 01110010 r
115 73 163 01110011 s
116 74 164 01110100 t
117 75 165 01110101 u
118 76 166 01110110 v
119 77 167 01110111 w
120 78 170 01111000 x
121 79 171 01111001 y
122 7A 172 01111010 z
123 7B 173 01111011 {
124 7C 174 01111100 |
125 7D 175 01111101 }
126 7E 176 01111110 ~
127 7F 177 01111111 ⌂
128 80 200 10000000 Ç
129 81 201 10000001 ü
130 82 202 10000010 é
131 83 203 10000011 â
132 84 204 10000100 ä
133 85 205 10000101 à
134 86 206 10000110 å
135 87 207 10000111 ç
136 88 210 10001000 ê
137 89 211 10001001 ë
138 8A 212 10001010 è
139 8B 213 10001011 ï
140 8C 214 10001100 î
141 8D 215 10001101 ì
142 8E 216 10001110 Ä
143 8F 217 10001111 Å
144 90 220 10010000 É
145 91 221 10010001 æ
146 92 222 10010010 Æ
147 93 223 10010011 ô
148 94 224 10010100 ö
149 95 225 10010101 ò
150 96 226 10010110 û
151 97 227 10010111 ù
152 98 230 10011000 ÿ
153 99 231 10011001 Ö
154 9A 232 10011010 Ü
155 9B 233 10011011 ¢
156 9C 234 10011100 £
157 9D 235 10011101 ¥
158 9E 236 10011110 ₧
159 9F 237 10011111 ƒ
160 A0 240 10100000 á
161 A1 241 10100001 í
162 A2 242 10100010 ó
163 A3 243 10100011 ú
164 A4 244 10100100 ñ
165 A5 245 10100101 Ñ
166 A6 246 10100110 ª
167 A7 247 10100111 º
168 A8 250 10101000 ¿
169 A9 251 10101001 ⌐
170 AA 252 10101010 ¬
171 AB 253 10101011 ½
172 AC 254 10101100 ¼
173 AD 255 10101101 ¡
174 AE 256 10101110 «
175 AF 257 10101111 »
176 B0 260 10110000 ░
177 B1 261 10110001 ▒
178 B2 262 10110010 ▓
179 B3 263 10110011 │
180 B4 264 10110100 ┤
181 B5 265 10110101 ╡
182 B6 266 10110110 ╢
183 B7 267 10110111 ╖
184 B8 270 10111000 ╕
185 B9 271 10111001 ╣
186 BA 272 10111010 ║
187 BB 273 10111011 ╗
188 BC 274 10111100 ╝
189 BD 275 10111101 ╜
190 BE 276 10111110 ╛
191 BF 277 10111111 ┐
192 C0 300 11000000 └
193 C1 301 11000001 ┴
194 C2 302 11000010 ┬
195 C3 303 11000011 ├
196 C4 304 11000100 ─
197 C5 305 11000101 ┼
198 C6 306 11000110 ╞
199 C7 307 11000111 ╟
200 C8 310 11001000 ╚
201 C9 311 11001001 ╔
202 CA 312 11001010 ╩
203 CB 313 11001011 ╦
204 CC 314 11001100 ╠
205 CD 315 11001101 ═
206 CE 316 11001110 ╬
207 CF 317 11001111 ╧
208 D0 320 11010000 ╨
209 D1 321 11010001 ╤
210 D2 322 11010010 ╥
211 D3 323 11010011 ╙
212 D4 324 11010100 ╘
213 D5 325 11010101 ╒
214 D6 326 11010110 ╓
215 D7 327 11010111 ╫
216 D8 330 11011000 ╪
217 D9 331 11011001 ┘
218 DA 332 11011010 ┌
219 DB 333 11011011 █
220 DC 334 11011100 ▄
221 DD 335 11011101 ▌
222 DE 336 11011110 ▐
223 DF 337 11011111 ▀
224 E0 340 11100000 α
225 E1 341 11100001 ß
226 E2 342 11100010 Γ
227 E3 343 11100011 π
228 E4 344 11100100 Σ
229 E5 345 11100101 σ
230 E6 346 11100110 µ
231 E7 347 11100111 τ
232 E8 350 11101000 Φ
233 E9 351 11101001 Θ
234 EA 352 11101010 Ω
235 EB 353 11101011 δ
236 EC 354 11101100 ∞
237 ED 355 11101101 φ
238 EE 356 11101110 ε
239 EF 357 11101111 ∩
240 F0 360 11110000 ≡
241 F1 361 11110001 ±
242 F2 362 11110010 ≥
243 F3 363 11110011 ≤
244 F4 364 11110100 ⌠
245 F5 365 11110101 ⌡
246 F6 366 11110110 ÷
247 F7 367 11110111 ≈
248 F8 370 11111000 °
249 F9 371 11111001 ∙
250 FA 372 11111010 ·
251 FB 373 11111011 √
252 FC 374 11111100 ⁿ
253 FD 375 11111101 ²
254 FE 376 11111110 ■
255 FF 377 11111111
And the output I want it to be look like this
deci hex oct binary characters
0 0 000 00000000
1 1 001 00000001 ☺
2 2 002 00000010 ☻
3 3 003 00000011 ♥
4 4 004 00000100 ♦
5 5 005 00000101 ♣
6 6 006 00000110 ♠
7 7 007 00000111
8 8 010 00001000
9 9 011 00001001
10 A 012 00001010
11 B 013 00001011 ♂
12 C 014 00001100 ♀
13 D 015 00001101
14 E 016 00001110 ♫
15 F 017 00001111 ☼
16 10 020 00010000 ►
17 11 021 00010001 ◄
18 12 022 00010010 ↕
19 13 023 00010011 ‼
20 14 024 00010100 ¶
deci hex oct binary characters
21 15 025 00010101 §
22 16 026 00010110 ▬
23 17 027 00010111 ↨
24 18 030 00011000 ↑
25 19 031 00011001 ↓
26 1A 032 00011010 →
27 1B 033 00011011
28 1C 034 00011100 ∟
29 1D 035 00011101 ↔
30 1E 036 00011110 ▲
31 1F 037 00011111 ▼
32 20 040 00100000
33 21 041 00100001 !
34 22 042 00100010 "
35 23 043 00100011 #
36 24 044 00100100 $
37 25 045 00100101 %
38 26 046 00100110 &
39 27 047 00100111 '
40 28 050 00101000 (
deci hex oct binary characters
41 29 051 00101001 )
42 2A 052 00101010 *
43 2B 053 00101011 +
44 2C 054 00101100 ,
45 2D 055 00101101 -
46 2E 056 00101110 .
47 2F 057 00101111 /
48 30 060 00110000 0
49 31 061 00110001 1
50 32 062 00110010 2
51 33 063 00110011 3
52 34 064 00110100 4
53 35 065 00110101 5
54 36 066 00110110 6
55 37 067 00110111 7
56 38 070 00111000 8
57 39 071 00111001 9
58 3A 072 00111010 :
59 3B 073 00111011 ;
60 3C 074 00111100 <
deci hex oct binary characters
61 3D 075 00111101 =
62 3E 076 00111110 >
63 3F 077 00111111 ?
64 40 100 01000000 #
65 41 101 01000001 A
66 42 102 01000010 B
67 43 103 01000011 C
68 44 104 01000100 D
69 45 105 01000101 E
70 46 106 01000110 F
71 47 107 01000111 G
72 48 110 01001000 H
73 49 111 01001001 I
74 4A 112 01001010 J
75 4B 113 01001011 K
76 4C 114 01001100 L
77 4D 115 01001101 M
78 4E 116 01001110 N
79 4F 117 01001111 O
80 50 120 01010000 P
deci hex oct binary characters
81 51 121 01010001 Q
82 52 122 01010010 R
83 53 123 01010011 S
84 54 124 01010100 T
85 55 125 01010101 U
86 56 126 01010110 V
87 57 127 01010111 W
88 58 130 01011000 X
89 59 131 01011001 Y
90 5A 132 01011010 Z
91 5B 133 01011011 [
92 5C 134 01011100 \
93 5D 135 01011101 ]
94 5E 136 01011110 ^
95 5F 137 01011111 _
96 60 140 01100000 `
97 61 141 01100001 a
98 62 142 01100010 b
99 63 143 01100011 c
100 64 144 01100100 d
deci hex oct binary characters
101 65 145 01100101 e
102 66 146 01100110 f
103 67 147 01100111 g
104 68 150 01101000 h
105 69 151 01101001 i
106 6A 152 01101010 j
107 6B 153 01101011 k
108 6C 154 01101100 l
109 6D 155 01101101 m
110 6E 156 01101110 n
111 6F 157 01101111 o
112 70 160 01110000 p
113 71 161 01110001 q
114 72 162 01110010 r
115 73 163 01110011 s
116 74 164 01110100 t
117 75 165 01110101 u
118 76 166 01110110 v
119 77 167 01110111 w
120 78 170 01111000 x
deci hex oct binary characters
121 79 171 01111001 y
122 7A 172 01111010 z
123 7B 173 01111011 {
124 7C 174 01111100 |
125 7D 175 01111101 }
126 7E 176 01111110 ~
127 7F 177 01111111 ⌂
128 80 200 10000000 Ç
129 81 201 10000001 ü
130 82 202 10000010 é
131 83 203 10000011 â
132 84 204 10000100 ä
133 85 205 10000101 à
134 86 206 10000110 å
135 87 207 10000111 ç
136 88 210 10001000 ê
137 89 211 10001001 ë
138 8A 212 10001010 è
139 8B 213 10001011 ï
140 8C 214 10001100 î
deci hex oct binary characters
141 8D 215 10001101 ì
142 8E 216 10001110 Ä
143 8F 217 10001111 Å
144 90 220 10010000 É
145 91 221 10010001 æ
146 92 222 10010010 Æ
147 93 223 10010011 ô
148 94 224 10010100 ö
149 95 225 10010101 ò
150 96 226 10010110 û
151 97 227 10010111 ù
152 98 230 10011000 ÿ
153 99 231 10011001 Ö
154 9A 232 10011010 Ü
155 9B 233 10011011 ¢
156 9C 234 10011100 £
157 9D 235 10011101 ¥
158 9E 236 10011110 ₧
159 9F 237 10011111 ƒ
160 A0 240 10100000 á
deci hex oct binary characters
161 A1 241 10100001 í
162 A2 242 10100010 ó
163 A3 243 10100011 ú
164 A4 244 10100100 ñ
165 A5 245 10100101 Ñ
166 A6 246 10100110 ª
167 A7 247 10100111 º
168 A8 250 10101000 ¿
169 A9 251 10101001 ⌐
170 AA 252 10101010 ¬
171 AB 253 10101011 ½
172 AC 254 10101100 ¼
173 AD 255 10101101 ¡
174 AE 256 10101110 «
175 AF 257 10101111 »
176 B0 260 10110000 ░
177 B1 261 10110001 ▒
178 B2 262 10110010 ▓
179 B3 263 10110011 │
180 B4 264 10110100 ┤
deci hex oct binary characters
181 B5 265 10110101 ╡
182 B6 266 10110110 ╢
183 B7 267 10110111 ╖
184 B8 270 10111000 ╕
185 B9 271 10111001 ╣
186 BA 272 10111010 ║
187 BB 273 10111011 ╗
188 BC 274 10111100 ╝
189 BD 275 10111101 ╜
190 BE 276 10111110 ╛
191 BF 277 10111111 ┐
192 C0 300 11000000 └
193 C1 301 11000001 ┴
194 C2 302 11000010 ┬
195 C3 303 11000011 ├
196 C4 304 11000100 ─
197 C5 305 11000101 ┼
198 C6 306 11000110 ╞
199 C7 307 11000111 ╟
200 C8 310 11001000 ╚
deci hex oct binary characters
201 C9 311 11001001 ╔
202 CA 312 11001010 ╩
203 CB 313 11001011 ╦
204 CC 314 11001100 ╠
205 CD 315 11001101 ═
206 CE 316 11001110 ╬
207 CF 317 11001111 ╧
208 D0 320 11010000 ╨
209 D1 321 11010001 ╤
210 D2 322 11010010 ╥
211 D3 323 11010011 ╙
212 D4 324 11010100 ╘
213 D5 325 11010101 ╒
214 D6 326 11010110 ╓
215 D7 327 11010111 ╫
216 D8 330 11011000 ╪
217 D9 331 11011001 ┘
218 DA 332 11011010 ┌
219 DB 333 11011011 █
220 DC 334 11011100 ▄
deci hex oct binary characters
221 DD 335 11011101 ▌
222 DE 336 11011110 ▐
223 DF 337 11011111 ▀
224 E0 340 11100000 α
225 E1 341 11100001 ß
226 E2 342 11100010 Γ
227 E3 343 11100011 π
228 E4 344 11100100 Σ
229 E5 345 11100101 σ
230 E6 346 11100110 µ
231 E7 347 11100111 τ
232 E8 350 11101000 Φ
233 E9 351 11101001 Θ
234 EA 352 11101010 Ω
235 EB 353 11101011 δ
236 EC 354 11101100 ∞
237 ED 355 11101101 φ
238 EE 356 11101110 ε
239 EF 357 11101111 ∩
240 F0 360 11110000 ≡
deci hex oct binary characters
241 F1 361 11110001 ±
242 F2 362 11110010 ≥
243 F3 363 11110011 ≤
244 F4 364 11110100 ⌠
245 F5 365 11110101 ⌡
246 F6 366 11110110 ÷
247 F7 367 11110111 ≈
248 F8 370 11111000 °
249 F9 371 11111001 ∙
250 FA 372 11111010 ·
251 FB 373 11111011 √
252 FC 374 11111100 ⁿ
253 FD 375 11111101 ²
254 FE 376 11111110 ■
255 FF 377 11111111
This is working for me fine.
#include <stdio.h>
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
int main () {
unsigned char c = 0; /* for the character */
int n = 0; /* integer for the for loop Iteration */
printf("\nthis's ASCII table with all representations: \n");
for (c = 0; c <= 255; c++)
{
if (c%20 == 0)
printf("\n deci hex oct binary characters\n");
printf ("\n %3d %2X %03o \t", c, c, c); /* print Ascii using for loop with multi representations */
for (n = sizeof (c) * CHAR_BIT - 1; n >= 0; n--){ /* method to convert decimal to base 2 (binary) */
printf ("%d", ((c >> n) & 0x1) ? 1 : 0);
}
printf("%5c", c);
putchar ('\n');
if(c==255)
break; /* to stop in 255 and not making infinite loop (because of unsigned char) */
}
putchar ('\n');
return 0;
}
EDIT: I'm glad no one has spent any time pointing out that the actual text in line 6 and 7 has a different number than the input for their respective function calls. Eventually I'll be doing it for those two numbers (724 and 27), but for the sake of troubleshooting, I picked numbers with much smaller sequences. So, if anyone was wondering, that's why...
So, I've been learning Perl, and am relatively new to programming in general. My supervisor has a set of exercises for me to go through. The current one deals with Hailstone sequences, and she wants me to write a subroutine that prints the sequence for a given number.
The problem I'm running into is that, no matter what I've tried, if I call the function more than once, it will produce the sequence for the first number I call the function with, but the second time I call the function, it produces the sequence of the first call followed by the sequence of the second. So, this code:
#!usr/bin/perl
use strict;
use warnings;
print "\nThe hailstone sequence for 724 is:\n" . &hail(8) . "\n\n";
print "The hailstone sequence for 27 is:\n" . &hail(16) . "\n\n";
my $n;
my #seq;
sub hail {
no warnings 'recursion';
$n = $_[0];
if ($n > 1) {
push #seq, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
&hail($n);
} else {
push #seq, $n;
}
return "#seq";
}
produces:
The hailstone sequence for 724 is:
8 4 2 1
The hailstone sequence for 27 is:
8 4 2 1 16 8 4 2 1
I understand that this is most likely due to the fact that #seq doesn't get cleared out after each time the subroutine runs, but I've tried as many different ways as I can think of to clear it out so that each time I call the subroutine, it displays the sequence for -only- that number but they all either result in what I show here, or in showing nothing. How do I go about clearing the array each time?
Thanks very much.
You don't need recursion here. In my Fibonacci example in Mastering Perl, I show that it's easier to do it with iteration where you manage the queue yourself rather than using the call stack to do it.
Here's a general iterative solution that uses an array to keep track of the work left to do:
use strict;
use warnings;
use v5.10;
say "The hailstone sequence for 724 is:\n\t" .
join " ", hail(8);
say "The hailstone sequence for 27 is:\n\t" .
join " ", hail(16);
sub hail {
my #queue = ( $_[0] );
my #sequence = ();
while( my $next = shift #queue ) {
if( $next > 1 ) {
push #queue, do {
if( $next % 2 == 0 ) { $next / 2 }
else { 3*$next + 1 }
};
}
push #sequence, $next;
}
#sequence;
}
From there, I could add caching and other things so I can reuse sequences I've already generated (which works even without showing off some exciting new Perl features such as subroutine signatures and postfix dereferencing that I find quite fun):
use strict;
use warnings;
use v5.22;
use feature qw(signatures postderef);
no warnings qw(experimental::signatures experimental::postderef);
say "The hailstone sequence for 724 is:\n\t" .
join " ", hail(8)->#*;
say "The hailstone sequence for 27 is:\n\t" .
join " ", hail(16)->#*;
sub hail ( $n ) {
my #queue = ( $_[0] );
state $sequence = { 1 => [ 1 ] };
return $sequence->{$n} if exists $sequence->{$n};
my #sequence = ();
while( my $next = shift #queue ) {
say "Processing $next"; # to watch what happens
if( exists $sequence->{$next} ) {
push #sequence, $sequence->{$next}->#*;
next;
}
push #queue, do {
if( $next % 2 == 0 ) { $next / 2 }
else { 3*$next + 1 }
};
push #sequence, $next;
}
$sequence->{$n} = \#sequence;
}
I threw a say in there to show what I process. You can see that with 16, it doesn't have to go past 8 because it already knows that answer:
Processing 8
Processing 4
Processing 2
Processing 1
The hailstone sequence for 724 is:
8 4 2 1
Processing 16
Processing 8
The hailstone sequence for 27 is:
16 8 4 2 1
I was curious which numbers might cause a problem, so I slightly modified your example to return a list so I could easily count the number of elements. Several numbers produced sequences with over 100 numbers:
use strict;
use warnings;
use v5.10;
foreach my $n ( 0 .. 100 ) {
hail( $n, \my #seq );
say "$n [" . #seq . "] #seq";
}
sub hail {
my $n = $_[0];
my $s = $_[1];
if ($n > 1) {
push #$s, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
hail($n, $s);
} else {
push #$s, $n;
}
}
The output, without the deep recursion warnings (which should be the hint not to do it that way ;):
0 [1] 0
1 [1] 1
2 [2] 2 1
3 [8] 3 10 5 16 8 4 2 1
4 [3] 4 2 1
5 [6] 5 16 8 4 2 1
6 [9] 6 3 10 5 16 8 4 2 1
7 [17] 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
8 [4] 8 4 2 1
9 [20] 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
10 [7] 10 5 16 8 4 2 1
11 [15] 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
12 [10] 12 6 3 10 5 16 8 4 2 1
13 [10] 13 40 20 10 5 16 8 4 2 1
14 [18] 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
15 [18] 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
16 [5] 16 8 4 2 1
17 [13] 17 52 26 13 40 20 10 5 16 8 4 2 1
18 [21] 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
19 [21] 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
20 [8] 20 10 5 16 8 4 2 1
21 [8] 21 64 32 16 8 4 2 1
22 [16] 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
23 [16] 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
24 [11] 24 12 6 3 10 5 16 8 4 2 1
25 [24] 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
26 [11] 26 13 40 20 10 5 16 8 4 2 1
27 [112] 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
28 [19] 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
29 [19] 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
30 [19] 30 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
31 [107] 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
32 [6] 32 16 8 4 2 1
33 [27] 33 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
34 [14] 34 17 52 26 13 40 20 10 5 16 8 4 2 1
35 [14] 35 106 53 160 80 40 20 10 5 16 8 4 2 1
36 [22] 36 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
37 [22] 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
38 [22] 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
39 [35] 39 118 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
40 [9] 40 20 10 5 16 8 4 2 1
41 [110] 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
42 [9] 42 21 64 32 16 8 4 2 1
43 [30] 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
44 [17] 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
45 [17] 45 136 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
46 [17] 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
47 [105] 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
48 [12] 48 24 12 6 3 10 5 16 8 4 2 1
49 [25] 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
50 [25] 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
51 [25] 51 154 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
52 [12] 52 26 13 40 20 10 5 16 8 4 2 1
53 [12] 53 160 80 40 20 10 5 16 8 4 2 1
54 [113] 54 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
55 [113] 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
56 [20] 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
57 [33] 57 172 86 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
58 [20] 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
59 [33] 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
60 [20] 60 30 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
61 [20] 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
62 [108] 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
63 [108] 63 190 95 286 143 430 215 646 323 970 485 1456 728 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
64 [7] 64 32 16 8 4 2 1
65 [28] 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
66 [28] 66 33 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
67 [28] 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
68 [15] 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
69 [15] 69 208 104 52 26 13 40 20 10 5 16 8 4 2 1
70 [15] 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
71 [103] 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
72 [23] 72 36 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
73 [116] 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
74 [23] 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
75 [15] 75 226 113 340 170 85 256 128 64 32 16 8 4 2 1
76 [23] 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
77 [23] 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
78 [36] 78 39 118 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
79 [36] 79 238 119 358 179 538 269 808 404 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
80 [10] 80 40 20 10 5 16 8 4 2 1
81 [23] 81 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
82 [111] 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
83 [111] 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
84 [10] 84 42 21 64 32 16 8 4 2 1
85 [10] 85 256 128 64 32 16 8 4 2 1
86 [31] 86 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
87 [31] 87 262 131 394 197 592 296 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
88 [18] 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
89 [31] 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
90 [18] 90 45 136 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
91 [93] 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
92 [18] 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
93 [18] 93 280 140 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
94 [106] 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
95 [106] 95 286 143 430 215 646 323 970 485 1456 728 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
96 [13] 96 48 24 12 6 3 10 5 16 8 4 2 1
97 [119] 97 292 146 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
98 [26] 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
99 [26] 99 298 149 448 224 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
100 [26] 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Should you want a true function — one that doesn't have any side-effects, and thus none of the problems you are having — it would look as follows:
sub hail {
no warnings qw( recursion );
my ($n) = #_;
if ($n > 1) {
if ($n % 2 == 0) {
return $n, hail($n/2);
} else {
return $n, hail(3*$n + 1);
}
} else {
return $n;
}
}
Note that recursion is totally unneeded here, greatly slowing down your program and increasing its memory footprint. The following is such an iterative solution:
sub hail {
my ($n) = #_;
my #rv;
while (1) {
push #rv, $n;
last if $n <= 1;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = 3*$n + 1;
}
}
return #rv;
}
A question about the above code was asked in the comments. The following is the answer:
last jumps to the statement after the loop.
Another programmer might have written the following:
push #rv, $n;
while ($n > 1) {
...;
push #rv, $n;
}
But that contains duplicated code. Ideally, one seeks to avoid duplicated code. But since
while (EXPR) { STATEMENTS }
can be rewritten as
while (1) { last if !EXPR; STATEMENTS }
and since
push #rv, $n;
while ($n > 1) {
...;
push #rv, $n;
}
can be rewritten as
push #rv, $n;
while (1) {
last if $n <= 1;
...;
push #rv, $n;
}
we can remove the duplicated code as follows:
while (1) {
push #rv, $n;
last if $n <= 1;
...;
}
Not sure about perl, but in other languages I'd do
sub hail {
my #seq;
return hailRecursive(\#seq, $_[0])
}
And then implement hailRecursive in terms of array ref and $n
So, the full assignment was to print the sequence for any number < 100000 of my choosing, print the sequence of 27, and then find the number < 100000 with the longest sequence, and print the number of elements in the sequence (but not the sequence itself) I greatly appreciate the help everyone has provided about making a more effective subroutine, and I really will go through the different suggestions to learn the different tips and tricks from each one. I didn't modify my main subroutine code too much to keep it more in my (current) style for the sake of the exercise. (My supervisor won't care that I got help, but it still feels...-something-...to just copy somebody else's code without also knowing I could figure it out with my own hair-brained ideas, along with -her- suggestion to use a recursive subroutine, as well).
I took one person's advice to not return the array as a string, which helped with the part of the exercise I didn't originally mention, but then I had to rework how I printed the actual sequences which seemed easy enough. Beyond learning more effective subroutines, my main concern is still with clearing the array. Someone suggested just putting #seq = () after each instance, and that works. What I want to know is why what I actually have running in the code (the lines with ##### -after- the code) works to clear the array each time, but why it doesn't work to simply clear out the array after I return it in the subroutine, like I have with the commented out line. That still aggregates the sequence each time the subroutine is called.
#!usr/bin/perl
use strict;
use warnings;
my $num_win = 0;
my $elem_win = 0;
my #seq;
my $elements;
print "\nThe hailstone sequence for 724 is:\n";
&sequence(&hail(724));
#seq = (); #####
print "\n\nThe hailstone sequence for 27 is:\n";
&sequence(&hail(27));
#seq = (); #####
for (my $i=1; $i<100000; $i++) {
$elements = &hail($i);
if ($elements > $elem_win) {
$elem_win = $elements;
$num_win = $i;
}
#seq = (); #####
}
print "\n\nThe number with the largest sequence is: $num_win\n";
print "The number of elements in $num_win is: $elem_win\n\n";
my $n;
sub hail {
no warnings 'recursion';
$n = $_[0];
if ($n > 1) {
push #seq, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
&hail($n);
} else {
push #seq, $n;
}
return #seq;
##### #seq = ();
}
sub sequence {
my #hail_seq = #_;
foreach (#hail_seq) {
my $number = $_;
print "$number, ";
}
}
with the results:
The hailstone sequence for 724 is:
724, 362, 181, 544, 272, 136, 68, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1,
The hailstone sequence for 27 is:
27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1,
The number with the largest sequence is: 77031
The number of elements in 77031 is: 351
Change your 2nd else block to this and remove the return statement after that block.
else {
push #seq, $n;
my $seq = "#seq";
undef #seq;
return $seq;
}
You just need to initialize the array #seq before calling the hail subroutine a second time. Try this ...
my #seq;
print "\nThe hailstone sequence for 724 is:\n" . &hail(8) . "\n\n";
#seq = ();
print "The hailstone sequence for 27 is:\n" . &hail(16) . "\n\n";
my $n;
Similar to what others have posted.
sub hailstone {
my $n = shift;
return if $n == 1;
if ( $n % 2 == 0 ){
$n = $n / 2;
} else {
$n = ( $n * 3 ) + 1;
}
return $n."\n",hailstone($n)
}
Upon calling the subroutine:
say hailstone(5);
16
8
4
2
1
I'm trying to initialize a two-dimensional array from a text file containing a 20x20 grid of numbers (separated by spaces) using vb.NET in Visual Studio 2013. I've tweaked my loops to work correctly but the values I get from the streamreader are totally differently from what is in the file.
Here's the vb code
Imports System.IO
Module Module1
Dim grid(19, 19) As String
Sub Main()
Dim deskPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
Using upload As New StreamReader(deskPath & "\grid.txt")
'Console.WriteLine(upload.ReadToEnd)
For x As Integer = 0 To 19
For y As Integer = 0 To 19
grid(x, y) = upload.Read() '& upload.read()
upload.Read()
Next
Next
End Using
Using outfile As New StreamWriter(deskPath & "\Result1.txt")
For y As Integer = 0 To 19
For x As Integer = 0 To 19
outfile.Write(grid(x, y))
outfile.Write(" ")
Next
outfile.WriteLine()
Next
End Using
End Sub
End Module
This is the source file's content
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
And this is what the array is receiving
48 56 56 13 32 32 57 50 48 50 55 50 32 32 32 51 48 52 54 52
32 53 53 56 56 49 13 32 32 53 53 54 52 56 53 32 32 32 50 52
50 32 32 32 51 52 50 48 53 13 32 32 50 57 48 51 48 54 32 32
50 50 48 57 32 32 32 52 48 51 52 50 13 32 32 57 48 51 53 55
32 53 56 51 51 57 49 32 32 32 55 50 50 51 54 13 32 32 53 52
55 32 32 32 52 50 49 56 52 56 32 32 32 49 52 55 50 49 13 32
51 55 55 51 32 32 32 54 55 56 54 53 53 32 32 32 49 52 56 48
32 57 52 53 51 56 49 32 32 32 49 54 53 56 48 55 32 32 32 50
53 32 32 32 51 53 53 51 51 56 32 32 32 51 52 53 52 53 54 32
48 56 56 57 32 32 32 49 53 54 56 52 53 32 32 32 48 53 48 56
32 10 52 49 53 49 55 32 32 32 54 51 54 57 53 56 32 32 32 53
48 57 32 32 10 51 54 48 56 51 32 32 32 54 49 50 51 56 56 32
48 52 57 57 50 32 32 10 51 54 48 51 51 32 32 32 53 50 51 52
32 32 52 57 55 54 57 52 32 32 10 48 57 50 48 53 32 32 32 49
53 57 32 32 32 55 52 52 52 48 55 32 32 10 54 51 50 48 49 32
48 52 52 49 53 32 32 32 50 50 50 48 54 49 32 32 10 49 56 56
32 32 53 52 50 55 50 50 32 32 32 57 57 51 51 55 54 32 32 10
53 55 32 32 32 48 51 54 53 56 48 32 32 32 57 49 51 52 55 57
48 56 50 55 52 32 32 32 49 52 54 54 55 51 32 32 32 50 48 56
32 32 48 53 54 54 52 57 32 32 32 52 55 48 52 52 53 32 32 32
I've tried finding patterns if the values were being saved in the wrong element, changing the datatype of the array and even casting the streamreader into int (that just gave an error). As you may have noticed I concatenated two read() operations to initialize two digits in each element but that gave me totally different (unrelatable to the 2 digit wrong values) 4 digit values. These ones
4856 1052 1310 5313 5749 3256 5032 5452 3257 5732 5149 3253 5432 5349 3248 5732 5150 3251 5732 5649
4850 3252 4932 5350 1050 1310 4813 5548 3250 5132 5151 3253 5732 5352 3256 5132 5451 3252 5332 4954
5050 3257 5732 5548 3251 5232 5150 1054 1310 5013 5753 3257 5332 4955 3253 5532 5751 3254 5232 5051
5755 3252 4932 5753 3249 5532 5756 3250 5232 5049 1055 1310 5513 5356 3252 5632 5351 3255 5232 5355
5156 3249 5132 5051 3255 5032 5649 3250 5332 5154 3249 5432 5654 1049 1310 5413 5457 3251 5432 4853
4953 3256 5332 4852 3253 4832 5056 3254 5632 5051 3253 5732 5354 3256 5232 5656 1048 1310 5413 5352
4848 3249 5732 5448 3254 5732 5452 3248 5332 4857 3250 5332 4848 3256 5032 5154 3252 4832 5048 1048
5248 3253 5232 4949 3254 5132 5051 3254 5432 5553 3250 5032 5256 3254 5632 5456 3249 5732 5551 3255
4848 3254 5732 5250 3256 5332 5455 3249 5132 4848 3255 5432 5153 3248 5132 5655 3255 5432 5153 3253
5553 3256 5132 5457 3252 5032 4948 3250 5732 5554 3251 5332 5549 3257 5532 5355 3251 4932 5057 3255
4852 3249 4932 5052 3257 5232 5054 3257 5432 5252 3254 4932 5657 3252 5332 5450 3250 5032 5556 3256
4853 3252 4832 5456 3251 5332 5156 3254 5532 5048 3249 5532 4855 3254 5732 5048 3251 4832 5149 3253
4855 3257 5532 5354 3253 5132 5248 3257 5532 5253 3257 5332 4853 3250 5432 5550 3249 5132 5748 3253
5556 3252 5132 4849 3250 5132 5455 3251 5632 5153 3248 5632 5252 3255 5532 4851 3250 5632 4849 3254
5350 3254 5632 5150 3252 5632 5357 3254 5632 4952 3256 5632 5252 3257 5532 5254 3257 5232 5552 3249
4950 3252 4832 5354 3252 5432 5352 3248 5432 4848 3248 5232 5155 3249 5532 5151 3255 5032 5149 3257
5348 3248 5132 5549 3250 5232 5548 3252 5132 5449 3254 4832 5252 3256 5032 5455 3249 5732 5257 3251
5555 3253 5732 5155 3254 4832 5454 3257 5232 5151 3249 5532 5448 3253 5432 5254 3248 5732 5549 3252
5749 3254 5132 4850 3251 5332 4956 3254 5632 5755 3249 5232 5049 3249 5432 5353 3252 5032 5256 3254
4856 3248 5432 5154 3249 5532 5156 3252 5232 5152 3248 5232 5356 3255 5432 4950 3250 5532 5654 3252
When I tried printing the raw values from the streamreader directly on the console, it worked correctly but the same method didn't work for initializing the array.
And ideas, suggestions, anything is welcome.
Thank you for your time!
There are at least two issues you are seeing.
First, the order of the inner and outer loops is different in the code that you use to fill the array and the code you use to print the array contents. In the first case, x is varied in the outer loop and y is varied the inner loop. The second set up loops vary y in the outer loop and x in the inner loop. This would seem to explain part of the difference.
More importantly, I noted that that the values in the array as you printed appear to be decimal representations of the ASCII codes of the characters you read.
Be aware that StreamReader.Read() returns an integer value that corresponds to the ASCII value of the next character in the input stream. When you assign this to a string variable, vb.net converts the integer to a string (as if you used the integer ToString() method). In this case, the string will contain the decimal value of the ASCII code of the character you originally read instead of the actual character you read.
There are many alternate ways you could accomplish this task. One way I would suggest is reading the input file in a line at a time using a TextReader, and then using String.Split to get the string representation of each individual number.
Have you read the documentation for the StreamReader.Read method? It returns a single character as an Integer, i.e. the Unicode value for a Char. If you want to read the actual numbers in the file then you have to read the substrings and then convert them to Integer values yourself.
Something like this seems far more sensible:
Dim grid(19, 19) As Integer
Using reader As New StreamReader("file path here")
For i = 0 To grid.GetUpperBound(0)
Dim numbers = reader.ReadLine().Split()
For j = 0 To grid.GetUpperBound(1)
grid(i, j) = Convert.ToInt32(numbers(j))
Next
Next
End Using