I am trying to write a program in C using for loops which will output a pattern similar to this:
1
2 3
4 5 6
7 8 9 10
As you can see the pattern is a right angled triangle with each row with the same amount of numbers as the first number in each row. I am a beginner in C and chose to go about solving to practice my use of for loops and done this step by step so i chose to first find out how i can print the first column. Here is my code so far:
#include <stdio.h>
int main()
{
int n;
int p = 1;
printf("enter number of rows: ");
scanf("%d", &n);
for (int i= 1; i <= n; i += p++){
printf("%d\n", i);
}
return 0;
}
What i found was that the first number of each column was being increased by 1 when broken down for example (1 + 1 = 2, 2 + 2 = 4, 4 + 3 = 7) As you can see each number i have made bold makes up the first column. My problem is that when i ask the use to enter a value to make up a specific number of rows, such as for example 5, the for statement prints out only 4 rows as you can see do below:
enter number of rows: 5
1
2
4
I am guessing this is due to the statement i <= n but how do i specify the amount of rows without disrupting the calculation of the first column?
Here you have a triangle printing program.
int ndigits(unsigned long long x)
{
int ndg = 0;
while(x)
{
ndg++;
x /= 10;
}
return ndg;
}
void triangle(int rows)
{
int count = 1;
int ndg = ndigits(rows * (rows + 1) /2);
for(int row = 1; row <= rows; row++)
{
for(int column = 1; column <= row; column++)
{
printf("%0*d ", ndg, count++);
}
printf("\n");
}
}
int main(void)
{
triangle(30);
}
the function ndigits calculates number of decimal digits of the number. It is needed to print real triangle as all numbers have to have the same size (some will be padded by zeroes). Otherwise the trangle will not look like a triangle.
the rows * (rows + 1) /2 formula calculates the result of the 1+2+3+4+5... showing the largest number in the triangle
three row triangle will look:
1
2 3
4 5 6
ten rows triangle:
01
02 03
04 05 06
07 08 09 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
and 20 rows:
001
002 003
004 005 006
007 008 009 010
011 012 013 014 015
016 017 018 019 020 021
022 023 024 025 026 027 028
029 030 031 032 033 034 035 036
037 038 039 040 041 042 043 044 045
046 047 048 049 050 051 052 053 054 055
056 057 058 059 060 061 062 063 064 065 066
067 068 069 070 071 072 073 074 075 076 077 078
079 080 081 082 083 084 085 086 087 088 089 090 091
092 093 094 095 096 097 098 099 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 can try it yourself here: https://godbolt.org/z/7EWWso
You can use This Simple Code:
#include <stdio.h>
int main(){
int count=0;
for (int i = 01; i < 11; ++i) {
for (int j = 01; j <=i; ++j) {
count+=01;
printf("%d ",count);
}
printf("\n");
}
}
Related
My program takes a string input from the user, looks through the string, finds a dash and prints any letters in between the two letters beside the dash (exercise 3-3 in "The C Programing Language"). My program always either adds too many letters or not enough.
Here is the code that I think is responsible for the bug:
for (i=0; i<(strlen(stringin));i++, w++){
c = stringin[i];
/*finds a - and logs the letters before*/
if(c == '-'){
printf("found - ");
logpre = stringin[i-1];
printf("%d ", logpre);
logpost = stringin[i+1];
printf("%d\n", logpost);
/*logs newly generlated letters to string*/
for(o=logpre+1; logpre <= logpost; w++, o++){stringout[w]=o;}
}
else{
/*logs unused letters to string*/
stringout[w]=c;
printf("%d\t%d\n", c, w);
}
}
print statements are there because who knows how to debug without them?
didn't know what to try, but this is the current input/output:
a-z 0-9
String Length- 7
97 0
found - 97 122
211 7005
212 7006
213 7007
214 7008
55255 7009
14155992 7010
-654311207 7011
218 7012
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
-
Input =
�����������������������������������������������
Output =
abcdefghijklmnopqrstuvwxyz{|}\~��������������������������������������������������������������������������������������������������������������������������������
this is what I want it to look like:
a-z 0-9
String Length- 3
97 0
found - 97 122
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 31 48 49 50 51 52 53 54 55 56 57
-
Input = a-z 0-9
Output = abcdefghijklmnopqrstuvwxyz 0123456789
for (i=0; i<(strlen(stringin));i++, w++)
w++ here is wrong. It should be only incremented when you actually add to the output string. You also do not terminate you output string.
Do it much more simple way:
{
size_t outPos = 0;
if(str && *str && buff)
{
for(size_t pos = str[1] == '-'; str[pos]; pos++)
{
if(str[pos] == '-' && str[pos + 1])
{
for(char ch = str[pos - 1]; ch <= str[pos + 1]; ch++)
buff[outPos++] = ch;
pos++;
}
else buff[outPos++] = str[pos];
}
buff[outPos++] = 0;
}
return buff;
}
int main(void)
{
char buf[500];
printf("\"%s\"\n", print("asa a-z hello 0-3 wrewertyrt 5-W", buf));
}
I have a 20x20 array randomly generated. I need a function to scan through the array, find all the prime numbers within the array, and add them to a new array. The array was generated like this:
#define TOTAL_ROWS 20
#define TOTAL_COLUMNS 20
void fillMatrix(int A[TOTAL_ROWS][TOTAL_COLUMNS], int *set_rows, int *set_columns)
{
int rows = 20, columns = 20;
for(int i = 0; i < rows; i++)
for(int j = 0; j < columns; j++)
A[i][j] = rand() % 500;
*set_rows = rows;
*set_columns = columns;
}
I also have it sorted, with this function:
void sortMatrix(int A[TOTAL_ROWS][TOTAL_COLUMNS], int rowsize, int colsize)
{
for(int r = 0; r < rowsize; r++)
qsort(A[r], colsize, sizeof(int), compare);
}
There is also a display function. Here is main which calls the functions:
int main(void)
{
int array[TOTAL_ROWS][TOTAL_COLUMNS] = { 0 };
int rows, columns;
fillMatrix(array, &rows, &columns);
sortMatrix(array, rows, columns);
displayArray(array, rows, columns);
return 0;
}
An example of the generated array:
0 41 145 169 205 224 281 327 327 334 358 436 442 461 462 464 467 478 491 495
35 38 104 153 167 216 218 226 271 292 299 369 382 391 394 395 402 412 421 447
29 37 47 141 144 162 164 173 203 211 223 241 253 257 278 311 322 333 359 368
6 35 40 48 101 106 148 190 229 264 288 305 316 342 350 370 390 393 442 446
37 38 41 82 84 118 123 126 129 256 308 323 340 376 429 431 439 444 454 466
12 21 72 73 97 115 139 158 173 204 245 270 277 306 329 333 386 424 430 477
7 31 52 74 107 136 150 155 161 191 224 267 290 337 350 355 430 441 466 486
6 30 88 91 155 168 209 221 253 258 262 287 383 400 409 413 422 445 446 457
20 21 37 41 48 95 96 102 124 168 199 291 336 348 350 359 374 410 483 484
14 53 117 127 148 228 234 281 288 307 310 313 393 400 418 421 438 467 483 499
8 19 56 93 100 109 116 195 202 224 249 298 303 309 343 344 435 451 485 489
3 9 23 38 80 87 89 118 122 157 200 281 292 296 298 298 314 448 458 472
11 38 55 134 146 156 157 179 190 191 202 272 315 328 362 375 386 388 433 458
3 10 21 57 75 100 142 151 199 212 322 344 369 369 381 389 392 416 476 498
2 85 88 117 154 169 182 188 189 221 255 257 285 289 332 361 401 423 426 432
12 29 49 55 60 139 145 187 192 218 253 279 329 368 423 425 434 441 476 496
49 105 114 171 193 195 201 234 263 282 286 286 297 316 366 416 437 449 455 488
53 58 129 144 146 161 185 196 222 256 308 313 313 321 332 355 412 445 481 482
10 18 22 24 35 44 149 154 159 168 173 186 245 253 292 313 439 450 466 474
18 70 125 159 177 202 287 297 314 324 333 334 372 374 391 405 414 458 477 487
I need an isPrime() function to go in main that will find each prime in the arrays and add them to a new array. I'm just not sure how to.
You can use this.
int is_prime(int num)
{
if (num <= 1) return 0;
if (num % 2 == 0 && num > 2) return 0;
for(int i = 3; i < num / 2; i+= 2)
{
if (num % i == 0)
return 0;
}
return 1;
}
int prime_numbers_array[10000];
int array_length = 0;
int i,j;
for(i = 0; i < rows; i++){
for(j = 0 ;j < cols; j++){
if(is_prime(array[i][j])){
prime_numbers_array[array_length++] = array[i][j]
}
}
}
The fastest way is to run a sieve of Eratosthenes:
bool prime[max_number + 1];
void sieve()
{
memset(prime, true, sizeof(prime));
for (int i=2; i*i<=max_number; i++)
{
if (prime[i] == true)
{
for (int j=i*i; j<=max_number; j += i)
prime[j] = false;
}
}
}
And use the prime array as a lookup to check if a given number p is prime in constant time just by checking if prime[p] == true. Here max_number should be the maximum number that you will be having as an input.
This question already has answers here:
Convert int to double
(3 answers)
Closed 4 years ago.
I am learning C from a text. An example code provided by the author is:
#include <math.h>
main()
{ int i;
printf("\t Number \t\t Square Root of Number\n\n");
for (i=0; i<=360; ++i)
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
}
which on my computer produces an incorrect output I don't understand:
Number Square Root of Number
0 259
1 515
2 771
3 1027
4 1283
5 1539
6 1795
7 2051
8 2307
9 2563
10 2819
11 3075
12 3331
13 3587
14 3843
15 4099
16 4355
17 4611
18 4867
19 5123
20 5379
21 5635
22 5891
23 6147
24 6403
25 6659
26 6915
27 7171
28 7427
29 7683
30 7939
31 8195
32 8451
33 8707
34 8963
35 9219
36 9475
37 9731
38 9987
39 10243
40 10499
41 10755
42 11011
43 11267
44 11523
45 11779
46 12035
47 12291
48 12547
49 12803
50 13059
51 13315
52 13571
53 13827
54 14083
55 14339
56 14595
57 14851
58 15107
59 15363
60 15619
61 15875
62 16131
63 16387
64 16643
65 16899
66 17155
67 17411
68 17667
69 17923
70 18179
71 18435
72 18691
73 18947
74 19203
75 19459
76 19715
77 19971
78 20227
79 20483
80 20739
81 20995
82 21251
83 21507
84 21763
85 22019
86 22275
87 22531
88 22787
89 23043
90 23299
91 23555
92 23811
93 24067
94 24323
95 24579
96 24835
97 25091
98 25347
99 25603
100 25859
101 26115
102 26371
103 26627
104 26883
105 27139
106 27395
107 27651
108 27907
109 28163
110 28419
111 28675
112 28931
113 29187
114 29443
115 29699
116 29955
117 30211
118 30467
119 30723
120 30979
121 31235
122 31491
123 31747
124 32003
125 32259
126 32515
127 32771
128 33027
129 33283
130 33539
131 33795
132 34051
133 34307
134 34563
135 34819
136 35075
137 35331
138 35587
139 35843
140 36099
141 36355
142 36611
143 36867
144 37123
145 37379
146 37635
147 37891
148 38147
149 38403
150 38659
151 38915
152 39171
153 39427
154 39683
155 39939
156 40195
157 40451
158 40707
159 40963
160 41219
161 41475
162 41731
163 41987
164 42243
165 42499
166 42755
167 43011
168 43267
169 43523
170 43779
171 44035
172 44291
173 44547
174 44803
175 45059
176 45315
177 45571
178 45827
179 46083
180 46339
181 46595
182 46851
183 47107
184 47363
185 47619
186 47875
187 48131
188 48387
189 48643
190 48899
191 49155
192 49411
193 49667
194 49923
195 50179
196 50435
197 50691
198 50947
199 51203
200 51459
201 51715
202 51971
203 52227
204 52483
205 52739
206 52995
207 53251
208 53507
209 53763
210 54019
211 54275
212 54531
213 54787
214 55043
215 55299
216 55555
217 55811
218 56067
219 56323
220 56579
221 56835
222 57091
223 57347
224 57603
225 57859
226 58115
227 58371
228 58627
229 58883
230 59139
231 59395
232 59651
233 59907
234 60163
235 60419
236 60675
237 60931
238 61187
239 61443
240 61699
241 61955
242 62211
243 62467
244 62723
245 62979
246 63235
247 63491
248 63747
249 64003
250 64259
251 64515
252 64771
253 65027
254 65283
255 65539
256 65795
257 66051
258 66307
259 66563
260 66819
261 67075
262 67331
263 67587
264 67843
265 68099
266 68355
267 68611
268 68867
269 69123
270 69379
271 69635
272 69891
273 70147
274 70403
275 70659
276 70915
277 71171
278 71427
279 71683
280 71939
281 72195
282 72451
283 72707
284 72963
285 73219
286 73475
287 73731
288 73987
289 74243
290 74499
291 74755
292 75011
293 75267
294 75523
295 75779
296 76035
297 76291
298 76547
299 76803
300 77059
301 77315
302 77571
303 77827
304 78083
305 78339
306 78595
307 78851
308 79107
309 79363
310 79619
311 79875
312 80131
313 80387
314 80643
315 80899
316 81155
317 81411
318 81667
319 81923
320 82179
321 82435
322 82691
323 82947
324 83203
325 83459
326 83715
327 83971
328 84227
329 84483
330 84739
331 84995
332 85251
333 85507
334 85763
335 86019
336 86275
337 86531
338 86787
339 87043
340 87299
341 87555
342 87811
343 88067
344 88323
345 88579
346 88835
347 89091
348 89347
349 89603
350 89859
351 90115
352 90371
353 90627
354 90883
355 91139
356 91395
357 91651
358 91907
359 92163
360 92419
Any idea or clue as to why? If its obvious maybe point to a reference that will point out some stupid mistake I have made?
Read up on this: printf
And use %f for double not %d which is for int
You're using the wrong format specifier here:
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
Make sure to use %f for variables of type double (what a function like sqrt() returns):
printf("\t %d \t\t\t %f \n",i, sqrt((double) i));
You have used a wrong format specifier.
Try,
printf("\t %d \t\t\t %f \n" ,i , sqrt((double) i));
%f for double. Good luck.
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;
}
I have searched for how to calculate the execution time of a program, and i have done exactly in describe of these topic
Execution time of C program
Calculating time of execution with time() function
but my code didn't count the time execution and i don't know why.
here are my code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
time_t begin, end;
begin = clock();
int c = 0;
for (int i = 0; i < 255; i++) {
printf("%d ",c++);
}
end = clock();
printf("\n");
double timeTaken = ((double) (end - begin)) / CLOCKS_PER_SEC;
printf("time taken: %lf", timeTaken);
return (EXIT_SUCCESS);
}
here are what it printed
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
time taken: 0.000000
I have checked many times for error, but it still print "time taken: 0.000000". Can you guys help me out? Thanks a lot.
Actually, you are iterating loop for a small number of times. Try iterating the loop for large number of times. Eg. iterate for 1e9 times. Then you will get noticeable time period. Modern processors are very fast, such that they have frequency upto 2.9 GHz or more, which mean that they can execute around 2 billion instructions, if available, within a second.