-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathch9_exercises.c
644 lines (485 loc) · 8.54 KB
/
ch9_exercises.c
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* ch9_exercises.c
*
* Created on: Oct 21, 2019
* Author: SuperMoudy
*/
// Q1
// Mistakes:
// 1) (double base, height) --> illegal
// should be (double base, double height)
// 2) double product; --> this line should be included between the
// braces (where the function's body is)
// After correction:
/*
double triangle_area(double base, double height)
{
double product;
product = base * height;
return product / 2;
}
*/
//-----------------------------------
// Q2
/*
int check(int x, int y, int n)
{
if(x >= 0 && x <= n && y >= 0 && y <= n)
return 1;
return 0;
}
// testing script
#include <stdio.h>
int main(void)
{
int x, y, n;
printf("Enter x: ");
scanf("%d", &x);
printf("Enter y: ");
scanf("%d", &y);
printf("Enter n: ");
scanf("%d", &n);
printf("Checking returns: %d", check(x, y, n));
return 0;
}
*/
//-----------------------------------------
// Q3
/*
int gcd(int m, int n)
{
if(n <= 1)
return m;
return gcd(n, m % n);
}
// test script
#include <stdio.h>
int main(void)
{
int m, n;
printf("Enter m: ");
scanf("%d", &m);
printf("Enter n: ");
scanf("%d", &n);
printf("gcd of %d & %d is %d\n", m, n, gcd(m, n));
return 0;
}
*/
//-------------------------------
// Q4
/*
int day_of_year(int month, int day, int year)
{
int days_per_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// from days
int total = day;
// from months
// (excluding the current month as its days are already counted)
for(int i = 0; i < month - 1; i++)
{
total += days_per_month[i];
}
// detecting leap year
if(month > 2)
{
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
total += 1;
}
}
return total;
}
// test script
#include <stdio.h>
int main(void)
{
int month, day, year;
printf("Enter month: ");
scanf("%d", &month);
printf("Enter day: ");
scanf("%d", &day);
printf("Enter year: ");
scanf("%d", &year);
printf("Day of the year is: %d", day_of_year(month, day, year));
return 0;
}
*/
//---------------------------------
// Q5
/*
int num_digits(unsigned int n)
{
int digits_count = 0;
if(n == 0)
{
return 1;
}
while(n > 0)
{
n /= 10;
digits_count++;
}
return digits_count;
}
// test script
#include <stdio.h>
int main(void)
{
unsigned int n;
printf("Enter n: ");
scanf("%u", &n);
printf("Number of digits in n: %d\n", num_digits(n));
return 0;
}
*/
//---------------------------------
// Q6
/*
int digit(unsigned int n, int k)
{
int step_count = 0, rem;
if(sizeof(unsigned int) * 8 < k)
return 0;
while(step_count < k)
{
rem = n % 10;
n /= 10;
step_count++;
}
return rem;
}
// test script
#include <stdio.h>
int main(void)
{
unsigned int n;
int k;
printf("Enter n: ");
scanf("%u", &n);
printf("Enter k: ");
scanf("%d", &k);
printf("Digit number %d from right in %u is %d\n", k, n, digit(n, k));
return 0;
}
*/
//---------------------------------
// Q7
// int f(int a, int b){...}
// i --> int, x --> double
// i = f(83, 12);
// x = f(83, 12);
// i = f(3.15, 9.28);
// x = f(3.15, 9.28);
// f(83, 12);
//
// All are valid by the effect of Implicit conversion
// (Type conversion in assignment)
//---------------------------------
// Q8
// (a) void f(double x); ---> valid
// (b) void f(double); ---> valid and used for good documentation
// (c) void f(x); ---> invalid
// (d) f(double x); ---> In C99, it's invalid but
// in C98, it's presumed to return an int
//---------------------------------
// Q9
// Output:
// i = 1, j = 2
// (yes, no change)
//---------------------------------
// Q10
/*
// (a) largest element of a
int largest_element(int a[], int n)
{
int max_element = a[0];
for(int i = 1; i < n; i++)
{
if(a[i] > max_element)
max_element = a[i];
}
return max_element;
}
// (b) average of all elements in a
int avg_element(int a[], int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
{
sum += a[i];
}
return sum / n;
}
// (c) number of positive elements in a
int num_positive_element(int a[], int n)
{
int pos_count = 0;
for(int i = 0; i < n; i++)
{
if(a[i] > 0)
pos_count++;
}
return pos_count;
}
// test script
#include <stdio.h>
int main(void)
{
int a[8] = {-2, 1, 8, 3, 5, 10, -12, 4};
printf("Largest element in a = %d\n", largest_element(a, 8));
printf("Average element in a = %d\n", avg_element(a, 8));
printf("Number of positive elements in a = %d\n", num_positive_element(a,8));
return 0;
}
*/
//---------------------------------
// Q11
/*
float compute_GPA(char grades[], int n)
{
float total_grade = 0.0f;
for(int i = 0; i < n; i++)
{
if(grades[i] == 'A' || grades[i] == 'a')
{
total_grade += 4.0f;
}
else if(grades[i] == 'B' || grades[i] == 'b')
{
total_grade += 3.0f;
}
else if(grades[i] == 'C' || grades[i] == 'c')
{
total_grade += 2.0f;
}
else if(grades[i] == 'D' || grades[i] == 'd')
{
total_grade += 1.0f;
}
}
return (total_grade / n);
}
// test script
#include <stdio.h>
int main(void)
{
int n;
printf("Enter the number of grades: ");
scanf("%d", &n);
char x_grades[n];
for(int i = 0; i < n; i++)
{
printf("Enter grade number %d: ", i + 1);
(stdout);
scanf("%c", &x_grades[i]);
}
printf("Average grade of x = %f", compute_GPA(x_grades, n));
return 0;
}
*/
//---------------------------------
// Q12
/*
double inner_product(double a[], double b[], int n)
{
double total = 0.0;
for(int i = 0; i < n; i++)
{
total += a[i] * b [i];
}
return total;
}
// test script
#include <stdio.h>
int main(void)
{
int n;
printf("Enter the arrays length: ");
scanf("%d", &n);
double a[n], b[n];
for(int i = 0; i < n; i++)
{
printf("Enter element %d in a: ", i + 1);
scanf("%lf", &a[i]);
printf("Enter element %d in b: ", i + 1);
scanf("%lf", &b[i]);
}
printf("Inner product of a & b is: %lf", inner_product(a, b, n));
return 0;
}
*/
//---------------------------------
// Q13
/*
int evaluate_position(char board[8][8])
{
int white_weight = 0, black_weight = 0;
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
switch(board[i][j])
{
case 'Q':
white_weight += 9;
break;
case 'R':
white_weight += 5;
break;
case 'B':
white_weight += 3;
break;
case 'N':
white_weight += 3;
break;
case 'P':
white_weight += 1;
break;
case 'q':
black_weight += 9;
break;
case 'r':
black_weight += 5;
break;
case 'b':
black_weight += 3;
break;
case 'n':
black_weight += 3;
break;
case 'p':
black_weight += 1;
break;
}
}
}
return (white_weight - black_weight);
}
*/
//---------------------------------
// Q14
/*
// Correct Implementation
#include <stdbool.h>
bool has_zero(int a[], int n)
{
for(int i = 0; i < n; i++)
{
if(a[i] == 0)
return true;
}
return false;
}
// test script
#include <stdio.h>
int main(void)
{
int n;
printf("Enter n: ");
scanf("%d", &n);
int a[n];
for(int i = 0; i < n; i++)
{
printf("a[%d] = ", i);
scanf("%d", &a[i]);
}
if(has_zero(a, n))
printf("array a has zero(s)");
else
printf("array a does not have any zeros");
return 0;
}
*/
//---------------------------------
// Q15
/*
// Correct Implementation
double median(double x, double y, double z)
{
double median_val;
if((x <= y && x >= z) || (x <= z && x >= y))
{
median_val = x;
}
else if((y <= x && y >= z) || (y <= z && y >= x))
{
median_val = y;
}
else
{
median_val = z;
}
return median_val;
}
// test script
#include <stdio.h>
int main(void)
{
double x, y, z;
printf("Enter x: ");
scanf("%lf", &x);
printf("Enter y: ");
scanf("%lf", &y);
printf("Enter z: ");
scanf("%lf", &z);
printf("The median of %lf, %lf, %lf = %lf", x, y, z, median(x, y, z));
return 0;
}
*/
//---------------------------------
// Q16
// Condensed fact
int fact(int n)
{
return (n <= 1) ? 1 : n * fact(n - 1);
}
//---------------------------------
// Q17
/*
// No recursive fact
int fact(int n)
{
int result = 1;
while(n > 1)
{
result *= n;
n -= 1;
}
return result;
}
*/
//---------------------------------
// Q18
/*
int gcd(int m, int n)
{
if(n == 0) //As specified by the exercise but could be n <= 1 instead.
return m;
return gcd(n, m % n);
}
*/
//---------------------------------
// Q19
/*
// Mystery function
#include <stdio.h>
void pb(int n)
{
if(n != 0)
{
pb(n / 2);
putchar('0' + n % 2);
}
}
int main(void)
{
int n;
printf("Enter n: ");
scanf("%d", &n);
pb(n);
return 0;
}
// From the output shown:
// Mystery function is actually a function that converts
// a number from the decimal form to its binary form.
*/