CS计算机代考程序代写 data structure compiler Some Answers to DT131B Exam 2019.06.03 — C programming part

Some Answers to DT131B Exam 2019.06.03 — C programming part
1. Basic C (10 points)
a) Do long variable names affect program execution time? Explain why or why not? (1p)
Ans: variable names are translated into memory addresses by the compiler, and therefore long names will not affect the program execution time.
b) What values do the following expressions have? How many bytes are used to represent each of them in a 32-bit system? (1.5p)
‘2’ “2” 2
Ans: ‘2’ has a value of ASCII code for character ‘2’, 1 byte is used.
“2” is a string and it is represented by the ASCII code of character ‘2’ and followed by a terminating null character. 2 bytes are used.
2 is an integer. At least 4 bytes are used for modern processors and compilers.
c) Suppose that variables i and j have values 3 and 7 before each of the following expression, respectively. What value does each of the following expression have? (2p) Show how you get the result.
2+ j % i
i= j
(++ i < 3)? !j : j i && i – 3 Ans: you can run the program exam-question-1.c to observe the running results. 2+ j % i = 2 + 7 % 3 = 2 + 1 = 3 i = ji is assigned with the value of j, so i get 7. So this expression has a value of 7. (++ i < 3)? !j : j this expression has a conditional operator. First we do the increment on i. so i has a value of 4, which makes the condition i<3 false. Therefore the expression has value of j, that is 7. (note: !j ? j has value of 7, which means true in C. then ! j has a vlue of false, and it is 0 in C). i && i – 3: since subtraction has higher preceedence than &&, we do i-3 = 3 – 3 = 0 first. A value of zero means false; i has a value of 3, which means true, so true&&false, the result is false, that is 0, in C. d) Suppose n is an int of value 1011011 in binary. What does n & ~07 do? What value does this expression have? (1p) Ans: the constant 07 means it is an int and is given in octal. Uniary operator ~ is bit-wise negation, so the least significant 3 bits are 0, while all other bits are 1’s. the operator & is bit-wise AND, so the expression n & ~07 mask out the least significant 3 bits. Therefore the value of n & ~07 is 1011000 in binary, or 11 * 8 = 88. e) Explain your understanding on text file and binary file. Give one function that writes data to a text file and one function that writes data to a binary file. (1p) Ans: check the book and slides. f) What is a union in C? Explain its usage. (1p) Ans: check the book and slides. g) Explain what the buffer overflow bug is and give a simple example. (1p) Ans: check the book and slides. h) What is the general structure of a for-loop in C. Re-write it in while-do loop. (1.5p) Ans: check the book and slides. 2. Program Reading (5 points) a) Read the following C program carefully. What are the output when the program is executed. #include
int confusion1 (int x, int y) {
x = 2 * x + y;
return x; }
int confusion2 (int *x, int *y) {
int tmp;
tmp = *x + 2 * *y; return tmp;
}
int main (void) {
int x = 2, y = 5;
y = confusion1 (y, x);
x = confusion1 (y, x); printf (“%d %d\n”, x, y); y = confusion2 (&y, &x); x = confusion2 (&y, &x); printf (“%d %d\n”, x, y); return 0;
}
(2 points)

Ans: 26 12 116 64
b) C is a concise language. Read the following C function: void func ( char *s, char *t){
while (*s++ = *t++); }
(1 point)
What does this function do? Give an example to call this function.
Ans: the func( ) does the string copying operation. An example of its usage is given below:
char name [10]; // allocate space for keeping a string of at most 9 chars. func( name, “eric”);
c) Given the following C function: void myFunc( int array[], int n){
int c, d, t, i;
for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d-1] > array[d]) { t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d–; }
for (i=0 ; i <5; i ++) printf("%d ", array[i]); printf("\n"); } (2 points) } Suppose that you have the following data. How to call the above function to work with the data: int myData[ ] = { 10, 8, 7, 5, 2}; What does this function do? Ans: calling the function with myData myFunc( myData, 5); the output from the printf( ) in the function calling: 8 10 7 5 2 7 8 10 5 2 5 7 8 10 2 2 5 7 8 10 2 5 7 8 10 This function is to sort the array of data into increasing order. 3. Data Structures (5 points) Develop a C program that manages books. Information on a book should include the followings:  Authors  Title  Number of pages  Publisher  Year of publication  Edition  Price Do the following tasks: a) Design a data structure to keep information about a book. b) Count by hand the number of bytes used to represent a book c) Develop a C function to read in a book from the keyboard d) Develop the main( ) function to allocate space for 10 books, to read in 10 books and find the cheapest and most expensive books. (2 points) Ans: a) A data structure can be defined as below: struct Book { char authors[50]; // 50 bytes char title[50]; // 50 int pages; // 4 char publisher[20]; // 20 }; int year; char edition; float price; // 4 //1 // 4 b) If no padding is considered, and suppose that the integer is represented by 4 bytes, the total number of bytes required is at least 50 + 50 + 4 +20 +4 + 1 + 4 = 133 c) To read in data on a book, we can have the following prototype. The pointer argument is used to improve the time performance. void inputBook ( struct Book * b); d) See the file exam-question-3.c for the source code. (1 point) (1 point) (1 point)