forked from CMPT-295-SFU/C-Lab-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer.c
More file actions
375 lines (342 loc) · 11 KB
/
pointer.c
File metadata and controls
375 lines (342 loc) · 11 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
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
/*
Lab 2(Data Lab - Pointers)
*
* <PLEASE REPLACE THIS LINE WITH YOUR NAME AND STUDENT USERNAME>
*
* pointer.c - Source file with your solutions to the Lab.
* This is the file you will hand in to your instructor.
*
* WARNING: Do not include the <stdio.h> header; it confuses the dlc
* compiler. You can still use printf for debugging without including
* <stdio.h>. The included file, "common.c" contains a function declaration
* that should prevent a compiler warning. In general, it's not good practice
* to ignore compiler warnings, but in this case it's OK.
*/
#ifndef COMMON_H
#include "common.h"
#endif
/*
* Instructions to Students:
*
* STEP 1: Read the following instructions carefully.
*/
#if 0
You will provide your solution to this homework by
editing the collection of functions in this source file.
INTEGER CODING RULES:
Replace the "return" statement in each function with one
or more lines of C code that implements the function. Your code
must conform to the following style:
int Funct(arg1, arg2, ...) {
/* brief description of how your implementation works */
int var1 = Expr1;
...
int varM = ExprM;
varJ = ExprJ;
...
varN = ExprN;
return ExprR;
}
Each "Expr" is an expression using ONLY the following:
1. Integer constants 0 through 255 (0xFF), inclusive. You are
not allowed to use big constants such as 0xffffffff.
However you are allowed to add constants to values greater
than 255. e.g. 250+250 = 500.
2. Function arguments and local variables (no global variables).
3. For 1-3, only use the following:
Pointer operations '*' and '&'. (Note that these are only for
dereferencing and taking addresses, bitwise & is not allowed).
Binary integer operations - + *.
Unary integer operation !.
For the last three, you may also use shifts (<<, >>), ~, ==, and ^.
Some of the problems restrict the set of allowed operators even further.
Each "Expr" may consist of multiple operators. You are not restricted to
one operator per line.
You are expressly forbidden to:
1. Use any control constructs such as if, do, while, for, switch, etc.
2. Define or use any macros.
3. Define any additional functions in this file.
4. Call any functions.
5. Use any other operations, such as &&, ||, ?:, sizeof,
!= or binary (bitwise & or |)
6. Use the [] operator to index into arrays for reading or writing.
You may assume that your machine:
1. Uses 2s complement, 32-bit representations of integers.
2. Performs right shifts arithmetically.
3. Has *undefined* behavior when shifting by a negative amount or an amount
greater than or equal to the number of bits in the value being shifted.
e.g. For x >> n, shifts by n < 0 or n >= *size of x* are undefined
e.g. if x is a 32-bit int, shifts by >= 32 bits are undefined
Undefined means you do not know what result you will get from the operation.
#endif
/*
* STEP 2: Modify the following functions according the coding rules.
*
* Test the code below in your own 'main' program.
*
*/
/*
* STEP 2: Modify the following functions according the coding rules.
*/
/*
* Return the size of an integer in bytes.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *
* Unary integer operators: !
* Shorthand operators based on the above: ex. +=, *=, ++, --, eFDIStc.
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, <<, >>, ==, !=, ^, /, %
* Unary integer operators: ~, -
*/
int intSize() {
int intArray[10];
int *intPtr1;
int *intPtr2;
// Write code to compute size of an integer.
return 2;
}
/*
* Return the size of a double in bytes.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *
* Unary integer operators: !
* Shorthand operators based on the above: ex. +=, *=, ++, --, etc.
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, <<, >>, ==, !=, ^, /, %
* Unary integer operators: ~, -
*/
int doubleSize() {
double doubArray[10];
double *doubPtr1;
double *doubPtr2;
// Write code to compute size of a double.
return 2;
}
/*
* Return the size of a pointer in bytes.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *
* Unary integer operators: !
* Shorthand operators based on the above: ex. +=, *=, ++, --, etc.
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, <<, >>, ==, !=, ^, /, %
* Unary integer operators: ~, -
*/
int pointerSize() {
double *ptrArray[10];
double **ptrPtr1;
double **ptrPtr2;
// Write code to compute size of a pointer.
return 2;
}
/*
* Given pointers to two distinct variables write a function to swap the values
* of said variables.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *
* Unary integer operators: !
* Shorthand operators based on the above: ex. +=, *=, ++, --, etc.
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, <<, >>, ==, !=, ^, /, %
* Unary integer operators: ~, -
*/
void swapInts(int *ptr1, int *ptr2) {
// Your code here
}
/*
* Modify intArray[5] to be the value 295 using only intArray and pointer
* arithmetic.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *, <<, >>, ==, ^
* Unary integer operators: !, ~
* Shorthand operators based on the above: ex. +=, *=, ++, --, ~= etc.
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, !=, /, %
* Unary integer operators: -
*/
int changeValue() {
int intArray[10];
int *intPtr1 = intArray;
// Remember not to use constants greater than 255.
// Remember to use * to dereference. You cannot use '[<index>]' syntax.
return intArray[5];
}
/*
* Return 1 if the addresses stored in ptr1 and ptr2 are within the
* *same* 64-byte aligned block of memory. Check the spec for examples if you
* are confused about what this means. Return zero otherwise. Operators / and %
* and loops are NOT allowed.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *, <<, >>, ==, ^
* Unary integer operators: !, ~
* Shorthand operators based on the above: ex. <<=, *=, ++, --, etc.
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, !=, /, %
* Unary integer operators: -
*/
int withinSameBlock(int *ptr1, int *ptr2) {
// Your code here
return 2;
}
/*
* Return 1 if ptr points to an element within the specified intArray, 0
* otherwise. Pointing anywhere in the array is fair game, ptr does not have to
* point to the beginning of an element. Check the spec for examples if you are
* confused about what this method is determining.
* size is the size of intArray in number of ints. Can assume size != 0.
* Operators / and % and loops are NOT allowed.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *, <<, >>, ==, ^
* Unary integer operators: !, ~
* Shorthand operators based on the above: ex. <<=, *=, ++, --, etc.
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, !=, /, %
* Unary integer operators: -
*/
int withinArray(int *intArray, int size, int *ptr) {
// Your code here
return 2;
}
/*
* In C characters are are terminated by the null character ('\0')
* given a pointer to the start of the string return the length of this string.
* (The null character is not counted as part of the string length.)
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *, ==, !=, <, >
* Unary integer operators: !
* Shorthand operators based on the above: ex. <<=, *=, ++, --, etc.
* Control constructs: for, while
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <<, >>, ^, /, %
* Unary integer operators: ~, -
*/
int stringLength(char *s) {
// Your code here
return 2;
}
/*
* Change the value pointed to by ptr byte-by-byte so that when returned as an
* integer the value is 295295.
*
* Hint: Remember that an int is 4 bytes.
*
* Hint: Remember how little endian works for data storage, how is it different
* between an multiple bytes(int) and a single byte?
*
* Hint: It will be easiest to start convert 295295 into binary form and
* starting seeing how the endian works from there.
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *
* Shorthand operators based on the above: ex. +=, *=, ++, --, etc.
* Unary integer operators: !
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <, >, <<, >>, ==, !=, ^, /, %
* Unary integer operators: ~, -
*/
int endianExperiment(int *ptr) {
char *bytePtr;
// Your code here
return *ptr;
}
/**
* Swaps the values stored at the memory addresses pointed to by a and b.
*/
/**
* Performs an in-place selection sort on int array arr with length len.
*/
/*
* Selection sort is a sorting algorithim that works by partitioning the array
* into a sorted section and unsorted section. Then it repeatedly selects the
* minimum element from the unsorted section and moves it to the end of the
* sorted section.
*
* So the pseudo-code might look something like this:
* arr - an array
* n - the length of arr
*
* for i = 0 to n - 1
* minIndex = i
* for j = i + 1 to n - 1
* if arr[minIndex] > arr[j]
* minIndex = j
* end if
* end for
* Swap(arr[i], arr[minIndex])
* end for
*
* We have implemented selection sort below, it might be helpful to use the
* swapInts you defined earlier, and the to be written smallest_idx function now
* .
*
* ALLOWED:
* Pointer operators: *, &
* Binary integer operators: -, +, *, ==, !=, <, >
* Unary integer operators: !
* Shorthand operators based on the above: ex. +=, *=, ++, --, etc.
* Control constructs: for, while, if
* Function calls: swapInt()
*
* DISALLOWED:
* Pointer operators: [] (Array Indexing Operator)
* Binary integer operators: &, &&, |, ||, <<, >>, ^, /
* Unary integer operators: ~, -
*/
/*
Using your new knowledge of arrays in C, implement the smallest_idx function to
return the index of the smallest element in an array. Be careful not to step out
of bounds!
*/
/**
* Returns the index of the smallest element in int array arr with length len.
*/
int smallest_idx(int *arr, int len) {
int i;
int smallest_i = 0;
int smallest = arr[0];
// TODO: implement me using a for loop.
return smallest_i;
}
// This function will work, if you implement smallest_idx and swapInts.
void selectionSort(int *arr, int len) {
int i, swap_idx;
for (i = 0; i < len; i++) {
swap_idx = i + smallest_idx(arr + i, len - i);
swapInts(arr + i, arr + swap_idx);
}
}