-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathch6_exercises.c
More file actions
225 lines (184 loc) · 3.73 KB
/
ch6_exercises.c
File metadata and controls
225 lines (184 loc) · 3.73 KB
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
/*
* ch6_exercises.c
*
* Created on: Mar 12, 2024
* Author: Mahmoud Hamdy
*/
// Q1
/**
*
* 1 2 4 8 16 32 64 128
*
*/
//----------------------------------
// Q2
/**
*
* 9384 938 93 9
*
*/
//----------------------------------
// Q3
/**
*
* Hint: For a comma expression, the return value of this expression
* is the value of last expression (after the last comma operator)
* Hence, the controlling expression depends only on the condition j > 0
*
* 5 4 3 2
*
*/
//----------------------------------
// Q4
/**
*
* (c) is not equivalent to (a) and (b) because unlike them, the value
* of i is incremented before the execution of the loop body
*
*/
//----------------------------------
// Q5
/**
*
* (c) is not equivalent to (a) and (b) because regardless the value
* of i, the loop body is guaranteed to be executed at least once
* (which is not be the case for (a) and (b) for values greater than
* or equal to 10)
*
*/
//----------------------------------
// Q6
/**
*
* for(i = 1; i <= 128; i *= 2)
* {
* printf("%d ", i);
* }
*
*/
//----------------------------------
// Q7
/**
*
* Because the question mentioned "a single" for statement, this
* solution is acceptable and will give the same output:
*
* for(i = 9384; i > 0; i /= 10)
* {
* printf("%d ", i);
* }
*
* If it's required to convert the do statement into a for statement
* given the value of i = n (general case), then the exact conversion
* that will give the exact output should look like this:
*
* i = n;
* printf("%d ", i);
*
* for(; i > 0; i /= 10)
* {
* printf("%d ", i);
* }
*
*/
//----------------------------------
// Q8
/**
*
* This for statement is an infinite loop that will print 10 5 3 2
* at first then when i reaches 1 this value will stay the same
* forever hence we don't exit the loop because i will always be >= 1
*
* 10 5 3 2 1 1 1 ...
*
*/
//----------------------------------
// Q9
/**
*
* i = 10;
*
* while(i >= 1)
* {
* printf("%d ", i++);
* i /= 2;
* }
*
*/
//----------------------------------
// Q10
/**
*
* Suppose we have a loop like this one:
* for( expr1; expr2; expr3)
* {
* if(condition)
* {
* continue;
* }
*
* // The rest of the loop body
*
* }
*
* As we know, if the condition is true, continue statement gets
* executed and the rest of the loop body is skipped because
* the control is actually transferred at the end of the loop body
*
* Hence we can achieve the same behavior using for statement and
* label at that point (let's call it, END_OF_LOOP_BODY):
*
* for(expr1; expr2; expr3)
* {
* if(condition)
* {
* goto END_OF_LOOP_BODY;
* }
*
* // The rest of the loop body
*
* END_OF_LOOP_BODY: ; // At least one statement is required here
*
* }
*
*
*/
//----------------------------------
// Q11
/**
*
* 20
*
* Hint: This program prints the sum of +ve even numbers less than 10
*
*/
//----------------------------------
// Q12
/**
*
* for(d = 2; d * d <= n; d++)
* if(n % d == 0)
* break;
*
*/
//----------------------------------
// Q13
/**
*
* for(n = 0; m > 0; m /= 2, n++);
*
*/
//----------------------------------
// Q14
/**
*
* The extra semicolon at the end of the if statement represents
* a null statement and hence belongs to the if statement
* and the printf statement becomes just another line that gets
* executed regardless the value of n is, so by removing that extra
* semicolon, the program is fixed.
*
* if(n % 2 == 0)
* printf("n is even\n");
*
*/