Name: ID#:
X.500:
Problem 1 (15 pts): 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.
POSITION A
|——–+——–+———+——-|
| Frame | Symbol | Address | Value |
|——–+——–+———+——-|
#include
void flub(double *ap, double *bp){
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 }
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.
| arr[0] | ||
CS 2021: Practice Exam 1
Fall 2021 University of Minnesota
Exam period: 20 minutes Points available: 50
| ptr | || ||i||?| |--------+--------+---------+-------| | flub() | | | | ||||| ||||7| |--------+--------+---------+-------|
|----------+-----------+------+-------+----------+---------| Fill in the following ta- | | | | | Unsigned | Signed |
Problem 2 (10 pts):
|
|
|
| ||i||?| |--------+--------+---------+-------| POSITION B |--------+--------+---------+-------| | Frame | Symbol | Address | Value | |--------+--------+---------+-------| | main() | x | #3064 | | | | arr[1] | #3056 | |
main() | | | |
x |
arr[1] |
arr[0] |
ptr |
#3064 | | #3056 | | || ||
| Blank #s | Binary | Hex | Octal | Decimal | Decimal |
|----------+-----------+------+-------+----------+---------|
||||
|#1#2#3|00011011| | ||||
|#4#5#6| |0xA5| ||||
|#7#8#9| |0xC7| |----------+-----------+------+-------+----------+---------|
|
0033 |
|
0245 |
|
|
| |
| |
| |
| |
| |
| -57 |
1/ 2
WRITE ON EVERY PAGE – Name:
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.
Problem 4 (5 pts):
#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; }
Write 8 characters from the beginning of array str to fh in binary format.
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.
2/ 2