Download Presentation
## ecs30 Summer 2014 midterm

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**ecs30 Summer 2014 midterm**Name: Student ID: Email: Close book and totally 6 questions (20% totally, you need to answer all questions), 10 pages. Please write precise and clean answers. But, please do not leave it blank there as I will give partial credits. Please READ the questions VERY CAREFULLY before putting down the final answer. And, also please mark your answer clearly. You can use the back of the pages. Every page of this exam book needs to be returned back. If we suspect any cheating behavior, we will pass the case to the academic committee immediately. Score: ___/20 ecs30 summer 2014, midterm**Q-01**#include <stdio.h> int main(void) { int x; int *xp; printf("size [int, int *, char] = [%d, %d, %d]\n", (int) sizeof(int), (int) sizeof(int *), (int) sizeof(char)); xp = &x; printf("&x = 0x%x\n", (int) &x); fprintf(stderr, "Please enter the value of x >"); scanf("%x", xp); printf("[01] %x\n", (int) xp); printf("[02] %x\n", (int) (*((int *) (*xp)))); printf("[03] %x\n", (int) (((int *)(xp)) + 1)); printf("[04] %x\n", (int) (((char *)(xp)) + 1)); return 0; } ecs30 summer 2014, midterm**Q-01**Assuming we are running the above program on a 32-bits address machine. The following is the execution of the program until the statement of “scanf”: PC13:/Users/wu% PC13:/Users/wu% gcc -Wall -Wstrict-prototypes y.c PC13:/Users/wu% ./a.out size [int, int *, char] = [4, 4, 1] &x = 0xbffffa08 Please enter the value of x > bffffa08(your input) ecs30 summer 2014, midterm**Q-01**(Program Execution and its Outputs, 4%) Based on the program and the input of “bffffa08”, FOUR more lines of outputs should be printed (i.e., there are FOUR printf lines after the scanf). Please write down those four lines of outputs. [01] [02] [03] [04] ecs30 summer 2014, midterm**Q-02**(Ordering Four Numbers, 5%) Given the following function call Order_2, please finish the function Order_4, to order/sort four numbers. Please note that you can ONLY use FIVE lines of Order_2 and nothing else. void Order_2(int *smp, int *lgp) { int temp; if (*smp > *lgp) { temp = *smp; *smp = *lgp; *lgp = temp; } } ecs30 summer 2014, midterm**Q-00**(Ordering Four Numbers, 5%) Given the following function call Order_2, please finish the function Order_4, to order/sort four numbers. Please note that you can ONLY use FIVE lines of Order_2 and nothing else. void Order_2(char *smp, char *lgp) { char *temp = malloc(sizeof(char) * (strlen(smp) + strlen(lgp)); if (strcmp(smp, lgp) > 0) { strcpy(temp, smp); strcpy(smp, lgp); strcpy(lgp, temp); } free(temp); } ecs30 summer 2014, midterm**void**Order_4(int *argentina, int *brazil, int *germany, *netherlands) { } Order_2( , ); Order_2( , ); Order_2( , ); Order_2( , ); Order_2( , ); ecs30 summer 2014, midterm**Q-03**(Ordering Four Numbers, continue, 2%) Following Q2, please explain/argue why it is impossible to sort four numbers, in general, with less than FIVE Order_2 calls. *** IMPORTANT ***{ Please note that this is a NEW question for ecs30 students, and you will learn how to construct a rigorous mathematical proof at a later course such as ecs122a. Therefore, I am not expecting you to have a complete solution. In fact, I merely want you to try to figure out an answer, might not be rigorous but with a flavor of computational thinking toward the ultimate correct solution. If you write down anything a bit meaningful, you will get 1% at least. So, YES, I want you to try and not give up. If you go beyond that basic effort and I view some creativity/excellent thoughts (even incorrect partially), you will get the full 2%.} ecs30 summer 2014, midterm**Q-04**(Understanding the Selection/Decision Making Process: Condition and Program Control, 4%) What would be printed for each of the given input of x? #include <stdio.h> #include <stdlib.h> int main(void) { int x; char y; scanf(“%d”, &x); y = ‘A’; if (x < 60) y = ‘F’; else if (x > 70) { if (++x > 80) y = ‘B’; else y = ‘C’; } else y = ‘D’; printf(“grade = %c\n”, y); } Input of X / Output: (x == 0) Output: y = (x == 65) Output: y = (x == 80) Output: y = (x == 1023) Output: y = ecs30 summer 2014, midterm**Q-05**(Binary/Decimal/Hexadecimal, Number System Transformation, 4%) 114 (decimal) = (binary) = _________________(hexadecimal) 0x114 (hexadecimal) = (binary) = (decimal) ecs30 summer 2014, midterm**Q-06**Please write down the output for the following segment of a program. (1%) int m, n; double x, y; m = 3; n = 2; x = m / n; y = m / (double) n; printf(“x = %lf, y = %lf\n”, x,y); ecs30 summer 2014, midterm