-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathch4_exercises.c
385 lines (346 loc) · 5.24 KB
/
ch4_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
/*
* ch4_exercises.c
*
* Created on: Apr 1, 2024
* Author: Mahmoud Hamdy
*/
// Q1
/**
*
* a) 1 2
*
* b) 0
*
* c) 1
*
* d) 0
*
*/
//----------------------------------
// Q2
/**
*
* For C89, no, as the C89 standard states that if either operand
* is negative, the result of a division can be rounded up or down.
*
* For example:
* Suppose i = 9, j = 7
* (-i)/j can be either -1 or -2 (implementation-defined behavior)
* -(i/j) is -1 (regardless the implementation)
*
* For C99, yes, as the C99 standard states that the result of a
* division is always truncated towards zero
*
* Foe example:
* Suppose i = 9, j = 7
* (-i)/j = -1
* -(i/j) = -1
*
*/
//----------------------------------
// Q3
/**
* a) 8 / 5 = 1
*
* b) -8 / 5 = -1 or -2
*
* c) 8 / -5 = -1 or -2
*
* d) -8 / -5 = 1 or 2
*
*/
//----------------------------------
// Q4
/**
*
* a) 8 / 5 = 1
*
* b) -8 / 5 = -1
*
* c) 8 / -5 = -1
*
* d) -8 / -5 = 1
*
*/
//----------------------------------
// Q5
/**
* a) 8 % 5 = 3
*
* b) -8 % 5 = -3 or 2
*
* c) 8 % -5 = 3 or -2
*
* d) -8 % -5 = -3 or 2
*
*/
//----------------------------------
// Q6
/**
* a) 8 % 5 = 3
*
* b) -8 % 5 = -3
*
* c) 8 % -5 = 3
*
* d) -8 % -5 = -3
*
*/
//----------------------------------
// Q7
/**
* Original Algorithm: 9 - [(Total - 1) % 10]
* Total - 1 ---->(1)
* (1) % 10 ----->(2)
* 9 - (2) ------>(3)
*
*
* Simplified Algorithm: 10 - [Total % 10]
* Total % 10 --->(1)
* 10 - (1) ----->(2)
*
* The simplified technique will not work simply because it is not
* equivalent to the original. The order of operations of changed
* and these operations are not cannot replace each other
* (Remainder from division cannot replace subtraction)
*
*/
//----------------------------------
// Q8
/**
*
* Programming approach:
* If we use printf on both equations (given the same total value),
* we will get the same result hence the program still works
*
* Math approach: Using modular arithmetic, we can prove that both
* equations are equivalent
*
*/
//----------------------------------
// Q9
/**
* a) i = 7, j = 8;
* i *= j + 1;
*
* j + 1 = 8 + 1 = 9 ----> (1)
* i = i * (1) = 7 * 9 = 63 ---> (2)
*
* Output:
* 63 8
*
*
* b) i = j = k = 1;
* i += j += k;
*
* k = 1 --->(1)
* j = j + (1) = 1 + 1 = 2 --->(2)
* i = i + (2) = 1 + 2 = 3 --->(3)
*
* Output:
* 3 2 1
*
*
* c) i = 1; j = 2; k = 3;
* i -= j -= k;
*
* k = 3 --->(1)
* j = j - (1) = 2 - 3 = -1 --->(2)
* i = i - (2) = 1 - (-1) = 2 --->(3)
*
* Output:
* 2 -1 3
*
* d) i = 2; j = 1; k = 0;
* i *= j *= k;
*
* k = 0 --->(1)
* j = j * (1) = 1 * 0 = 0 --->(2)
* i = i * (2) = 2 * 0 = 0 --->(3)
*
* Output:
* 0 0 0
*
*/
//----------------------------------
// Q10
/**
* a) i = 6;
* j = i += i;
*
* i = 6 --->(1)
* i = i + (1) = 6 + 6 = 12 --->(2)
* j = (2) = 12 --->(3)
*
* Output:
* 12 12
*
* b) i = 5;
* j = (i -= 2) + 1;
*
* (i -= 2) ---> i = i - 2 = 5 - 2 = 3 --->(1)
* j = (1) + 1 = 3 + 1 = 4 --->(2)
*
* Output:
* 3 4
*
* c) i = 7;
* j = 6 + (i = 2.5);
*
* (i = 2.5) ---> i = 2 --->(1)
* j = 6 + (1) = 6 + 2 = 8 --->(2)
*
* Output:
* 2 8
*
* d) i = 2; j = 8;
* j = (i = 6) + (j = 3);
*
* (i = 6) ---> i = 6 --->(1)
* (j = 3) ---> j = 3 --->(2)
* j = (1) + (2) = 6 + 3 = 9 --->(3)
*
* Output:
* 6 9
*
*/
//----------------------------------
// Q11
/**
*
* a) i = 1;
*
* i++ - 1 = 1 - 1 = 0 ->(1)
* i = 2 --->(2)
*
* Output:
* 0 2
*
*
* b) i = 10; j = 5;
*
* i++ - ++j = 10 - 6 = 4 --->(1)
* i = 11 --->(2)
*
* Output:
* 4 11 6
*
* c) i = 7; j = 8;
*
* i++ - --j = 7 - 7 = 0 --->(1)
* i = 8 --->(2)
*
* Output:
* 0 8 7
*
*
* d) i = 3; j = 4; k = 5;
*
* i++ - j++ + --k = 3 - 4 + 4 = 3 --->(1)
* i = 4 --->(2)
* j = 5 --->(3)
*
* Output:
* 3 4 5 4
*
*/
//----------------------------------
// Q12
/**
* a) i = 5;
* j = ++i * 3 - 2;
*
* ++i ---> i = 6 --->(1)
* j = (1) * 3 - 2 = 6 * 3 - 2 = 18 - 2 = 16 --->(2)
*
* Output:
* 6 16
*
*
* b) i = 5;
* j = 3 - 2 * i++;
*
* i = 5 --->(1)
* j = 3 - 2 * i++ = 3 - 2 * 5 = 3 - 10 = -7 --->(2)
*
* Output:
* 6 -7
*
*
* c) i = 7;
* j = 3 * i-- + 2;
*
* i = 7 --->(1)
* j = 3 * i-- + 2 = 3 * 7 + 2 = 21 + 2 = 23 --->(2)
*
* Output:
* 6 23
*
* d) i = 7;
* j = 3 + --i * 2;
*
* i = 7 --->(1)
* j = 3 + --i * 2 = 3 + 6 * 2 = 3 + 12 = 15 --->(2)
*
* Output:
* 6 15
*
*/
//----------------------------------
// Q13
/**
* ++i is exactly the same as (i += 1), in the sense that both
* will return the same expression value (i + 1), while i++ will
* only return the value of i before the increment.
*
*/
//----------------------------------
// Q14
/**
* a) a * b - c * d + e
*
* (((a * b) - (c * d)) + e)
*
*
* b) a / b % c / d
*
* (((a / b) % c) / d)
*
*
* c) - a - b + c - + d
*
* ((((-a) - b) + c) - (+d))
*
*
* d) a * - b / c - d
*
* (((a * (-b)) / c) - d)
*
*/
//----------------------------------
// Q15
/**
* Initially: i = 1, j = 2
*
* a) i += j;
*
* j = 2 --->(1)
* i += (1) ---> i = i + (1) = 1 + 2 = 3 --->(2)
*
* After execution: i = 3, j = 2
*
*
* b) i--;
*
* After execution: i = 0, j = 2
*
* c) i * j / i;
*
* After execution: i = 1, j = 2
*
*
* d) i % ++j;
*
* After execution: i = 1, j = 3
*
*/