while loop unexpected (character to int conversion) - c

#include<stdio.h>
main()
char i=13;
while(i)
{
i++;
}
printf("%d",i);
}
the out put turns out to be zero . how come does it happen
#include<stdio.h>
main()
char i=48;
if(i)
{
printf("%d",i);
}
}
this program run sucessfully and prints 48. isn't i considered a character o for which it is supposed to fail .How is i stored in the memory as a character or a number

You're missing a bracket after 'main()'. Your first code actually keeps counting up from 13, until it hits 127 (if, on your system, chars are represented as signed integers) because this is the largest positive integer for a char on such a system; then flips to -128 and keeps counting up to 0. At 0, you exit your loop, and print the result. To visualize what happens, copy this and try it out:
#include <stdio.h>
int main()
{
char i=13;
while(i){
i++;
printf("%d ", i);
}
printf("\n%d",i);
}
Output:
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 -128 -127 -126 -125 -124 -123 -122 -121 -120 -119 -118 -117 -116 -115 -114 -113 -112 -111 -110 -109 -108 -107 -106 -105 -104 -103 -102 -101 -100 -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 -88 -87 -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76 -75 -74 -73 -72 -71 -70 -69 -68 -67 -66 -65 -64 -63 -62 -61 -60 -59 -58 -57 -56 -55 -54 -53 -52 -51 -50 -49 -48 -47 -46 -45 -44 -43 -42 -41 -40 -39 -38 -37 -36 -35 -34 -33 -32 -31 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
The positive to negative change has to do with the IEEE representation of integers on computers. You can find many related questions on SO if you google for them.

An if statement compares the numeric value, not the ASCII character that it happens to represent. Only the numeric value 0 will result in the if failing to execute in your second example.

Related

How to reprint a statement every number of rows

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

How can I use SVG code as a source for <img src={...} /> element?

I was using svg code directly in a React component:
export const AdventurerToken = (props) => {
return (
<div style={props.style}>
<svg width="3vw" height="3vw" viewBox="0 0 99 119">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g transform="translate(0.000000,119.000000) scale(0.100000,-0.100000)"
fill={props.color} stroke="none">
<path d="M336 1174 c-10 -9 -16 -33 -16 -62 0 -26 -4 -52 -8 -59 -4 -6 -35 -17 -69 -23 -35 -7 -68 -19 -74 -26 -23 -28 50 -108 134 -145 20 -9 27 -19 27
-40 0 -23 -5 -30 -27 -35 -98 -21 -212 -74 -265 -122 -36 -32 -38 -38 -38 -92 0 -84 11 -92 134 -105 32 -3 62 -8 67 -11 11 -6 -12 -69 -50 -133 -77 -129
-114 -215 -115 -266 l-1 -50 168 -3 168 -2 57 90 c33 52 63 89 71 88 8 -2 35 -41 61 -88 l47 -85 172 -3 171 -2 0 43 c0 56 -35 151 -90 247 -86 149 -86 148
-74 160 6 6 43 14 83 17 98 8 121 26 121 97 0 62 -17 89 -84 133 -42 28 -157 74 -221 88 -29 6 -28 7 23 24 79 27 122 76 122 139 0 47 -72 102 -135 102 -18
0 -25 7 -30 33 -3 17 -13 49 -22 70 l-16 37 -138 0 c-112 0 -141 -3 -153 -16z"/>
</g>
</svg>
</div>
)
};
However Firefox doesn't accept "vw" units in sve width and height properties. Workaround would be using the svg as a source for an image element:
export const AdventurerToken = (props) => {
const color = props.color;
const adventurerSVG =
<svg width="100" height="100" viewBox="0 0 99 119">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g transform="translate(0.000000,119.000000) scale(0.100000,-0.100000)" fill={color} stroke="none">
<path d="M336 1174 c-10 -9 -16 -33 -16 -62 0 -26 -4 -52 -8 -59 -4 -6 -35 -17 -69 -23 -35 -7 -68 -19 -74 -26 -23 -28 50 -108 134 -145 20 -9 27 -19 27
-40 0 -23 -5 -30 -27 -35 -98 -21 -212 -74 -265 -122 -36 -32 -38 -38 -38 -92 0 -84 11 -92 134 -105 32 -3 62 -8 67 -11 11 -6 -12 -69 -50 -133 -77 -129
-114 -215 -115 -266 l-1 -50 168 -3 168 -2 57 90 c33 52 63 89 71 88 8 -2 35 -41 61 -88 l47 -85 172 -3 171 -2 0 43 c0 56 -35 151 -90 247 -86 149 -86 148
-74 160 6 6 43 14 83 17 98 8 121 26 121 97 0 62 -17 89 -84 133 -42 28 -157 74 -221 88 -29 6 -28 7 23 24 79 27 122 76 122 139 0 47 -72 102 -135 102 -18
0 -25 7 -30 33 -3 17 -13 49 -22 70 l-16 37 -138 0 c-112 0 -141 -3 -153 -16z"/>
</g>
</svg>
return (
<img style={{width: "10vw"}} src={adventurerSVG}/>
)
};
This does not work, as evidenced by https://codesandbox.io/s/dazzling-fire-m560j?file=/src/App.js - direct import works, import as a source of element does not. What am I doing wrong? Why is the image not loaded?
Workaround: use <div> instead of <img>.
Put svg code in a variable with 100% width and height, put variable inside with correct width / height:
export const AdventurerToken = (props) => {
const color = props.color;
const adventurerSVG =
<svg width="100%" height="100%" viewBox="0 0 99 119">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g transform="translate(0.000000,119.000000) scale(0.100000,-0.100000)" fill={color} stroke="none">
<path d="M336 1174 c-10 -9 -16 -33 -16 -62 0 -26 -4 -52 -8 -59 -4 -6 -35 -17 -69 -23 -35 -7 -68 -19 -74 -26 -23 -28 50 -108 134 -145 20 -9 27 -19 27
-40 0 -23 -5 -30 -27 -35 -98 -21 -212 -74 -265 -122 -36 -32 -38 -38 -38 -92 0 -84 11 -92 134 -105 32 -3 62 -8 67 -11 11 -6 -12 -69 -50 -133 -77 -129
-114 -215 -115 -266 l-1 -50 168 -3 168 -2 57 90 c33 52 63 89 71 88 8 -2 35 -41 61 -88 l47 -85 172 -3 171 -2 0 43 c0 56 -35 151 -90 247 -86 149 -86 148
-74 160 6 6 43 14 83 17 98 8 121 26 121 97 0 62 -17 89 -84 133 -42 28 -157 74 -221 88 -29 6 -28 7 23 24 79 27 122 76 122 139 0 47 -72 102 -135 102 -18
0 -25 7 -30 33 -3 17 -13 49 -22 70 l-16 37 -138 0 c-112 0 -141 -3 -153 -16z"/>
</g>
</svg>
return (
<div style={{width: "3vw"}}>
{adventurerSVG}
</div>
)
};
Works both in Firefox and Opera.
There are a couple of problems with your code
the SVG is not valid for use as an image because it's missing the SVG namespace declaration i.e. xmlns="http://www.w3.org/2000/svg"
you need to give the img element a data URI, not just a Node
So a working example looks like this...
import React from "react";
import ReactDOMServer from "react-dom/server";
import "./styles.css";
export default function App() {
return (
<div className="App">
{adventurerSVG}
<img src={'data:image/svg+xml,' + ReactDOMServer.renderToString(adventurerSVG)}/>
</div>
);
}
const adventurerSVG = (
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 99 119">
<metadata>
Created by potrace 1.15, written by Peter Selinger 2001-2017
</metadata>
<g
transform="translate(0.000000,119.000000) scale(0.100000,-0.100000)"
fill="red"
stroke="none"
>
<path
d="M336 1174 c-10 -9 -16 -33 -16 -62 0 -26 -4 -52 -8 -59 -4 -6 -35 -17 -69 -23 -35 -7 -68 -19 -74 -26 -23 -28 50 -108 134 -145 20 -9 27 -19 27
-40 0 -23 -5 -30 -27 -35 -98 -21 -212 -74 -265 -122 -36 -32 -38 -38 -38 -92 0 -84 11 -92 134 -105 32 -3 62 -8 67 -11 11 -6 -12 -69 -50 -133 -77 -129
-114 -215 -115 -266 l-1 -50 168 -3 168 -2 57 90 c33 52 63 89 71 88 8 -2 35 -41 61 -88 l47 -85 172 -3 171 -2 0 43 c0 56 -35 151 -90 247 -86 149 -86 148
-74 160 6 6 43 14 83 17 98 8 121 26 121 97 0 62 -17 89 -84 133 -42 28 -157 74 -221 88 -29 6 -28 7 23 24 79 27 122 76 122 139 0 47 -72 102 -135 102 -18
0 -25 7 -30 33 -3 17 -13 49 -22 70 l-16 37 -138 0 c-112 0 -141 -3 -153 -16z"
/>
</g>
</svg>
);

How to properly align triangular number patterns

In C, I need to print the number pattern in the below manner in right alignment, ie: when the double digit comes the upper single digit should adjust itself to right and so on.
int main() {
long int k = 1;
int i, j, n;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("%ld ", k);
k++;
}
printf("\n");
}
return 0;
}
required output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55 `
output I get:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
output required:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
output I get:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
You should calculate the length of the last outputted number and use the value as the field width in a printf call..
Here you are.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
unsigned int upper = n * ( n + 1 ) / 2;
int size = 0;
do { ++size; } while ( upper /= 10 );
putchar( '\n' );
for ( unsigned int i = 0, value = 0; i < n; i++ )
{
do
{
printf( "%*u ", size, ++value );
} while ( value != ( i + 1 ) * ( i + 2 ) / 2 );
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output is
Enter a non-negative number (0 - exit): 10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
Enter a non-negative number (0 - exit): 20
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
Enter a non-negative number (0 - exit): 0
I will not help you write a code because zero effort is shown, but I can give hints via a pseudo code.
cin >> n; //desired number of rows
for (int i = 0; i < n; i++){
//generate triangle numbers
}//store the first n triangle numbers, given by n(n-1)/2
for (int i = 0; i < n; i++){
cout << i;
if (i==any triangle number){
cout << "\n"; //note that an endline is printed only at triangle numbers
}
}
Here is a simple solution that handles end of lines correctly too:
#include <stdio.h>
int main(void) {
int i, j, k, n, len;
if (scanf("%d", &n) == 1 && n > 0) {
/* compute the width of the last number */
//len = snprintf(NULL, 0, "%d", n * (n + 1) / 2);
//char buf[48]; /* large enough for 128-bit integers */
//len = sprintf(buf, "%d", n * (n + 1) / 2);
for (len = 1, k = n * (n + 1) / 2; k > 9; k /= 10)
len++;
for (i = k = 1; i <= n; i++) {
for (j = 1; j < i; j++) {
printf("%*d ", len, k++);
}
/* print the last number on the line without a trailing space */
printf("%*d\n", len, k++);
}
}
return 0;
}

How I can insert a column-file in a hash with a key as an array? [duplicate]

This question already has answers here:
Hash key as array to obtain the minimum and maximum numbers if a columns values are equal
(3 answers)
Closed 8 years ago.
I have the next file:
-38 miRNA18 8 44 dvex109349 6618 6580
35 miRNA5 21 57 dvex110330 1917 1952
26 miRNA2 27 54 dvex110362 1092 1118
-30 miRNA43 60 90 dvex110558 464 434
30 miRNA2 31 63 dvex111097 1359 1389
-30 miRNA31 43 73 dvex111146 4337 4307
-29 miRNA32 32 63 dvex111322 5680 5651
35 miRNA43 60 95 dvex111435 5612 5647
-26 miRNA43 55 80 dvex111770 723 697
-39 miRNA43 21 58 dvex112127 4928 4889
-32 miRNA2 70 102 dvex112254 1554 1522
33 miRNA17 56 89 dvex113799 2985 3018
38 miRNA17 26 64 dvex113799 2985 3023
40 miRNA17 30 70 dvex113799 2985 3025
I need to insert it into a hash, but with these parameters: the column 5 (dvex####) must be the key, and the others will be the values to this key. The idea, is group it by equal keys, and obtain the lower value of the column 6 and the maximun value of the column 7.
I think that is convenient create a hash with keys as an array, then organize it with that parameters.
The output should be:
-38 miRNA18 8 44 dvex109349 6618 6618
35 miRNA5 21 57 dvex110330 1917 1952
-38 miRNA18 8 44 dvex109349 6618 6580
35 miRNA5 21 57 dvex110330 1917 1952
26 miRNA2 27 54 dvex110362 1092 1118
-30 miRNA43 60 90 dvex110558 464 434
30 miRNA2 31 63 dvex111097 1359 1389
-30 miRNA31 43 73 dvex111146 4337 4307
-29 miRNA32 32 63 dvex111322 5680 5651
35 miRNA43 60 95 dvex111435 5612 5647
-26 miRNA43 55 80 dvex111770 723 697
-39 miRNA43 21 58 dvex112127 4928 4889
-32 miRNA2 70 102 dvex112254 1554 1522
33 miRNA17 26 70 dvex113799 2985 3025 #note this result is a group.
I'm very interesting because the solution is based in a file with different columns...
using a regex or split extract columns 6 (and maybe 6,7) to a scalar.
store the line into a nested hash.
$data1{$v5}{$v6}=$_;
$data2{$v5}{$v7}=$_;
List::Util will help with max/min
use List::Util qw[min max];
foreach $v5 (sort keys %data) {
$val6=min(keys $data1{$v5});
$val7=max(keys $data2{$v5});
now generate your output line as you desire.
}

ScatterViewItem Custom Shape

I have a ScatterViewItem which contains a Canvas
<Ellipse x:Name="Outer_Ellipse" Fill="White" Width="200" Height="200"></Ellipse>
<Ellipse Fill="Red" Canvas.Top ="15" Canvas.Left="15" Canvas.Right="15" Canvas.Bottom="15" Width="170" Height="170" ></Ellipse>
</Canvas>
</s:ScatterViewItem>
Id like to provide a Custom Shape so that the default rectangle shape is not show (here is a picture of my current implementation .
I followed this example here link text and have consulted the Puzzle that comes with the SDK but I am unable to get it working, my ScatterViewItem is blank.
I defined a path in the SurfaceWindow.Resources
<Path x:Key="ScatterShape" Fill="Blue">
<Path.Data>
<EllipseGeometry
RadiusX="200"
RadiusY="200">
</EllipseGeometry>
</Path.Data>
</Path>
And copied the style attributes from the link above. I created my CustomShape.cs as instructed and then created the ScatterViewItem.
System.Windows.Shapes.Path path;
path = (System.Windows.Shapes.Path)Resources["ScatterShape"];
CustomShape poly = new CustomShape(path.Data);
ScatterViewItem item = new ScatterViewItem();
item.Content = poly;
item.CanScale = false;
Binding binding = new Binding();
binding.Source = poly;
item.SetBinding(ScatterViewItem.DataContextProperty, binding);
scatter.Items.Add(item)
Im slightly confused with the above code since my understanding with the line
item.Content = poly
would overwrite the content of the ScatterViewItem (i.e in my case the Canvas or in another case say an Image). For the time being I don't need to move or scale the ScatterView item so no shadows are neccessary I just simply want to remove the rectangular box.
You can achieve this by modifying the ControlTemplate for the ScatterViewItem.
If you want to remove all the visual features of the scatterview then I guess you could get away with an empty template:
<Style TargetType="{x:Type s:ScatterViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type s:ScatterViewItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The above template will change all scatterview items to have a blank template, but you could give it a x:Key="YourStyleName" and set the ItemContainerStyle of the ScatterView in question to only affect that scatter view.
Please note that if you use Expression Blend, you may need to add a reference to Microsoft.Surface.Presentation.Generic.dll to do this or Blend may crash when editing the template.
You can also remove the shadow, so that the ScatterViewItem is no longer visible.
Assuming that item is your ScatterViewItem:
item.ApplyTemplate();
item.Background = new SolidColorBrush(Colors.Transparent);
item.ShowsActivationEffects = false;
Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome ssc;
ssc = item.Template.FindName("shadow", item) as Microsoft.Surface.Presentation.Generic.SurfaceShadowChrome;
ssc.Visibility = Visibility.Hidden;
I am having similar Problem.
Here is my code.
<s:ScatterView>
<s:ScatterView.Items>
<s:ScatterViewItem Height="1721" Width="2169">
<Canvas >
<Path Data="M0 2728 l0 -1012 28 26 c23 21 32 24 61 17 31 -6 40 -3 75 27 38 32 99 114 92 122 -6 5 -46 -21 -46 -30 0 -15 -56 -58 -75 -58 -29 0 -105 74 -105 102 0 12 14 37 32 55 31 32 32 34 20 78 -20 74 -17 82 29 89 22 4 44 4 49 1 13 -8 23 54 11 69 -8 10 -6 17 8 30 12 11 16 25 13 41 -4 24 -3 25 24 15 25 -10 31 -9 43 9 7 12 24 21 37 21 25 0 30 10 14 26 -5 5 -7 20 -3 32 7 29 34 25 48 -6 12 -27 20 -28 38 -1 13 18 24 20 74 18 32 -2 173 -2 314 -1 l256 2 12 113 c6 61 12 191 12 287 1 96 5 182 10 191 14 27 76 61 98 54 24 -7 29 -20 37 -103 10 -106 29 -115 65 -33 24 57 70 104 95 99 10 -1 29 -25 43 -53 37 -72 66 -79 141 -38 78 44 123 50 180 25 88 -38 122 -38 197 0 64 31 75 33 193 36 77 3 145 -1 177 -9 45 -10 61 -10 115 5 135 37 121 41 253 -81 64 -59 124 -111 133 -114 27 -10 473 0 515 12 47 12 84 47 92 86 22 99 71 155 143 159 27 2 79 17 118 34 46 21 97 35 153 41 64 7 100 18 150 44 36 19 86 37 111 41 25 4 65 17 90 29 52 26 113 39 140 30 29 -9 25 -49 -10 -89 -25 -28 -29 -38 -21 -59 5 -14 15 -28 21 -32 12 -8 3 -73 -13 -94 -16 -19 17 -43 103 -76 66 -25 80 -27 188 -23 109 4 121 6 176 36 32 18 67 32 77 32 25 0 24 12 -1 35 -13 12 -20 29 -18 44 2 21 10 27 38 32 39 6 102 40 140 75 23 21 30 22 64 13 52 -14 82 -5 94 30 8 22 18 30 45 35 37 7 73 38 63 55 -9 14 -94 40 -158 47 l-58 7 0 87 c0 48 -3 90 -7 93 -3 4 -5 35 -4 70 3 73 24 102 105 142 l50 25 -2592 0 -2592 0 0 -1012z" Stroke="Black"></Path>
....
....
</Canvas>
</s:ScatterViewItem>
</s:ScatterView.Items>
</s:ScatterView>
There are so many shapes in the canvas and the size of the canvas is also much higher.
so when i run the application, The canvas doesn't get re size when i re size the ScatterViewItem. And Event the Canvas i shown outside of the ScatterViewItem.

Resources