CSI 333 -- Fall 2010 -- Solutions to Midterm Exam ================================================= Question I: ----------- (a) 36 in base 7 = 6 * 7^0 + 3 * 7^1 = 6 + 21 = 27 decimal. (b) In general, we cannot comment out C code using "/*" and "*/" since the code may itself contain comments that use "/*" and "*/". (Comments using "/*" and "*/" can't be nested.) (c) In C, parameters are passed by value. Since we want the variables to be changed by the call to scanf (i.e., achieve the effect of call by reference), we need to pass pointers to variables. (d) The assignment is not valid since the declaration makes only the variable xx a pointer. (Variable yy is of type int; thus xx and yy are not of the same type.) (e) The assignment is not valid since the type of variable x is struct employee. (x is NOT a pointer.) We can use the "->" construct only with pointer variables. ============================================================================ Question II: ------------ Part (a): --------- 210 in base 4 = 1 * 4^1 + 2 * 4^2 = 36 decimal. 0.3 in base 4 = 3 * 4^(-1) = 3/4 = 0.75 decimal. So, 210.3 in base 4 = 36.75 decimal Converting 36 (decimal) to binary: 36/2 quotient = 18 remainder = 0 18/2 quotient = 9 remainder = 0 9/2 quotient = 4 remainder = 1 4/2 quotient = 2 remainder = 0 2/2 quotient = 1 remainder = 0 1/2 quotient = 0 remainder = 1 So, 36 (decimal) = 100100 binary. Converting 0.75 decimal to binary: 0.75 x 2 = 1.5 bit = 1 0.5 x 2 = 1.0 bit = 1 So, 0.75 (decimal) = 0.11 binary. So, 210.3 (base 4) = 100100.11 binary. Part (b): ---------- To convert +47 into binary: 47/2 quotient = 23 remainder = 1 23/2 quotient = 11 remainder = 1 11/2 quotient = 5 remainder = 1 5/2 quotient = 2 remainder = 1 2/2 quotient = 1 remainder = 0 1/2 quotient = 0 remainder = 1 Thus, 47 (decimal) = 00101111 (8-bit binary) So, -47 (decimal) in 8-bit 2's complement form = 11010001 (using the visual method) Answer in hexadecimal form = D1. Part (c): --------- When we convert a base 5 real number into decimal, each digit to the right of the radix point is multiplied by an appropriate positive power of the fraction 1/5. Since the fraction 1/5 = 0.2 has a finite representation in decimal, all positive powers of 1/5 also have finite representations. ============================================================================ Question III: ------------- (a) The C program is given below. #include int main(void) { printf("%d\n", sizeof(unsigned long)); return 0; } (b) Output: 8 3 8 10 (c) Output: 4 a.out two three one ============================================================================ Question IV: ------------ int check_first_last (PNODE head) { PNODE tail; /* To find the last node. */ /* If the list is empty, the function must return -1. */ if (head == NULL) return -1; /* The list is not empty. So, find the last node. */ tail = head; /* The last node has its next field value NULL. */ while (tail->next != NULL) { tail = tail->next; } /* End of while */ /* Now, head and tail point to the first and the last nodes of the list respectively. Compare the key values. */ if (head->key == tail->key) return 1; else return 0; } /* End of check_first_last. */ ============================================================================ Question V: ----------- #include #include int main(void) { char *input_file = "values.dat"; /* The name of input file.*/ FILE *inf; /* Input file pointer. */ int count; /* To find the number of integers in the file. */ int sum; /* To find the sum of all the integers in the file. */ int max; /* To find the maximum over all the integers in the file. */ int value; /* To read from input file. */ /* Open the input file for reading. */ if ((inf = fopen(input_file, "r")) == NULL) { fprintf(stderr, "Couldn't open input file -- %s\n", input_file); exit(1); } /* Initialization. */ count = sum = 0; max = 0; /* This is ok since all the values in the input file are non-negative. */ /* Loop through all the integer values in the input file to find the count, sum and max values. */ while (fscanf(inf, "%d", &value) != EOF) { /* We have an integer value. Update count, sum and max. */ count++; sum += value; if (value > max) max = value; } /* End of while. */ /* Close the input file. */ if (fclose(inf) == EOF) { /* Error in closing input file */ fprintf(stderr, "Error in closing file %s\n", input_file); exit(1); } /* Output values. (Since the file is guaranteed to have at least one integer, there is no need to check whether count is 0.) */ printf("Maximum value = %d\n", max); printf("Average value = %f\n", (float)sum/(float)count); return 0; } /* End of main. */ ============================================================================