Name:
ID#: X.500:
Nearby is a small C pro- gram which makes use of arrays, pointers, and func- tion calls. Fill in the tables associated with the ap- proximate memory layout of the running program at each position indicated. Assume the stack grows to lower memory addresses and that the sizes of C variable types correspond to common 64-bit systems.
Problem 1 (15 pts):
POSITION A SOLUTION
|——–+——–+———+——-|
| Frame | Symbol | Address | Value |
|——–+——–+———+——-|
#include
void flub(double *ap, double *bp){
|
|——–+——–+———+——-|
POSITION B SOLUTION
|——–+——–+———+——-|
| Frame | Symbol | Address | Value |
|——–+——–+———+——-|
1
2
3
4
5 6}
7 // POSITION B
8 return;
int c = 7;
if(*ap < c){
*ap = bp[1];
9 } |
10 int main(){ |
11 double x = 4.5;
12 double arr[2] = {3.5, 5.5};
13 double *ptr = arr+1;
14 // POSITION A
15 flub(&x, arr);
16 printf("%.1f\n",x);
17 for(int i=0; i<2; i++){
18 printf("%.1f\n",arr[i]);
19 }
20 return 0;
21 }
| arr[0] | #3048
Problem 2 (10 pts):
Fill in the following ta- ble of equivalent ways to write these 8 bit quan- tities. There are a to- tal of 9 blanks to fill in and the first column indi- cates which blanks occur in which lines. Assume two’s complement encod- ing for the signed decimal column.
|----------+-----------+------+-------+----------+---------|
| SOLUTION | | | | Unsigned | Signed |
| Blank #s | Binary | Hex | Octal | Decimal | Decimal |
|----------+-----------+------+-------+----------+---------|
CS 2021: Practice Exam 1 SOLUTION
Fall 2021 University of Minnesota
Exam period: 20 minutes Points available: 50
| main() |
|
|
| x | #3064 | arr[1] | #3056 | arr[0] | #3048 | ptr | #3040 |i |#3036
| 4.5 | | 5.5 | | 3.5 | | #3056 | | ?|
| main() | x | #3064
| | arr[1] | #3056
| 5.5 |
| 5.5 |
| 3.5 |
| #3056 |
| ptr | #3040
| |i|#3036|?| |--------+--------+---------+-------| | flub | ap | #3028 | #3064 | | | bp | #3020 | #3048 | | |c|#3016|7| |--------+--------+---------+-------| NOTES
- Both Pos A and B are before i is
assigned 0 so i remains undefined
|||| |#1#2#3|00011011|0x1B| |||| |#4#5#6|10100101|0xA5|
|~x+1 |01011011| | |#7#8#9|11000111|0xC7|
|~x+1 |00111001| | |----------+-----------+------+-------+----------+---------| NOTES
- Octal shows leading 0 which is not strictly necessary
- Typical twos’ complement conversion technique show below
binary representation: invert bits and add 1
|
0033 |
|
0245 |
|
0307 |
|
| 27| | 165 | | 199 | |
|
27 |
|
-91 |
|
-57 |
|
1/ 2
WRITE ON EVERY PAGE – Name: SOLUTION
Backgound: Write a short C code fragment (1-5 lines) using a C I/O function call to accomplish the stated task. Assume in each case there is a variable FILE *fh which has been opened appropriately for the I/O operation. Also assume that any variables mentioned have already been declared.
Problem 3 (5 pts): Read three text floating point values from fh formatted as standard decimal point values into double variables r,u,t. Check if the read fails due to reaching the end of file; if so print the message None left.
// SOLUTION
int res = fscanf(fh, "%lf %lf %lf", &r, &u, &t);
if(res == EOF){
printf("None left\n");
}
Problem 4 (5 pts): Write 8 characters from the beginning of array str to fh in binary format.
// SOLUTION
fwrite(str, sizeof(char), 8, fh);
#include
#include
// Struct to count positive/negative
// numbers in arrays.
typedef struct {
int poss, negs;
} pn_t;
pn_t *get_pn(int *arr, int len);
// Allocates a pn_t and initializes
// its field to zero. Then scans array
// arr increment poss for every 0 or
// positive value and negs for every
// negative value. Returns the pn_t
// with poss/negs fields set. If arr
// is NULL or len is less than 0,
// returns NULL.
int main(){
int arr1[5] = {3, 0, -1, 7, -4};
pn_t *pn1 = get_pn(arr1, 5);
// pn1: {.poss=3, .negs=2}
free(pn1);
int arr2[3] = {-1, -2, -4};
pn_t *pn2 = get_pn(arr2, 3);
// pn2: {.poss=0, .negs=3}
free(pn2);
int *arr3 = NULL;
pn_t *pn3 = get_pn(arr3, -1);
// pn3: NULL
return 0; }
Problem 5 (15 pts): Nearby is a main() function demon- strating the use of the function get pn(). Implement this func- tion according to the documentation given. My solution is about 12 lines plus some closing curly braces.
// SOLUTION
pn_t *get_pn(int *arr, int len){
1
2
3
4
5}
6 pn_t *pn = malloc(sizeof(pn_t)); 7 pn->negs = 0;
8 pn->poss = 0;
9 for(int i=0; i
12 }
13 else{
14 pn->poss++; 15 }
16 }
17 return pn;
18 }
if(arr==NULL || len < 0){
return NULL;
2/ 2