CSI 333 -- Fall 2011 -- Solutions to Midterm Exam ================================================= Question I: ----------- Part (a): --------- Converting 259 (decimal) to octal: 259/8 quotient = 32 remainder = 3 32/8 quotient = 4 remainder = 0 4/8 quotient = 0 remainder = 4 So, 259 (decimal) = 403 (octal) Part (b): --------- 123 in base 5 = 3 * 5^0 + 2 * 5^1 + 1 * 5^2 = 38 decimal. Converting 38 (decimal) to hexadecimal: 38/16 quotient = 2 remainder = 6 2/16 quotient = 0 remainder = 2 So, 123 (base 5) = 26 (hex) Part (b): --------- Converting 70 (decimal) to binary: 70/2 quotient = 35 remainder = 0 35/2 quotient = 17 remainder = 1 17/2 quotient = 8 remainder = 1 8/2 quotient = 4 remainder = 0 4/2 quotient = 2 remainder = 0 2/2 quotient = 1 remainder = 0 1/2 quotient = 0 remainder = 1 So, 70 (decimal) = 1000110 binary. Converting 0.15 decimal to binary: 0.15 x 2 = 0.3 bit = 0 0.3 x 2 = 0.6 bit = 0 0.6 x 2 = 1.2 bit = 1 0.2 x 2 = 0.4 bit = 0 0.4 x 2 = 0.8 bit = 0 0.8 x 2 = 1.6 bit = 1 Now, the bit pattern 1001 repeats indefinitely. ---- So, 0.15 (decimal) = 0.001001 binary. ---- So, 70.15 (decimal) = 1000110.001001 binary. Part (d): ---------- To convert +29 into binary: 29/2 quotient = 14 remainder = 1 14/2 quotient = 7 remainder = 0 7/2 quotient = 3 remainder = 1 3/2 quotient = 1 remainder = 1 1/2 quotient = 0 remainder = 1 Thus, 29 (decimal) = 11101 (binary) = 0001 1101 (8-bit binary) So, -29 (decimal) in 8-bit 2's complement form = 1110 0011 (using the visual method) Answer in hexadecimal form = E3. Part (e): --------- #define DIVISOR 10 int count_digits (int x) { /* Returns the number of digits in the decimal integer x. It is assumed that x is positive. Idea: Successive integer divisions of x by 10. Each division eliminates one digit of x. */ int count = 0; /* The number of digits in x -- to be computed. */ while (x > 0) { count++; x /= DIVISOR; } return count; } /* End of count_digits. */ ============================================================================ Question II: ------------- (a) Output: r = 5 y = 5 y = 6 r = 2 (b) Output: 21 21 31 -14 -14 -14 (c) Output for Part (i): 4 6 Output for Part (i): 2 5 ============================================================================ Question III: ------------- int check_list (PNODE head) { PNODE prev; /* Temporary. */ /* If the list is empty or has only one node, the function */ /* must return 0. */ if ((head == NULL) || (head->next = NULL)) return 0; /* The list has two or more nodes. Traverse the list and */ /* check if there are two successive nodes with negative */ /* key values. */ prev = head; head = head->next; while (head != NULL) { if ((prev->key < 0) && (head->key < 0)) /* Found two successive nodes with negative key values. */ return 1; else { prev = head; head = head->next; } } /* End of while */ /* If we reach this point, then the list is not special. */ return 0; } /* End of check_list. */ ============================================================================ Question V: ----------- #include #include #define DIVISOR 17 int main(void) { char *infname = "infile.dat"; /* The name of input file.*/ char *outfname = "outfile.dat"; /* The name of output file.*/ FILE *infp, *outfp; /* Input and output file pointers. */ int value; /* To read from input file. */ /* Open the input file for reading. */ if ((infp = fopen(infname, "r")) == NULL) { fprintf(stderr, "Couldn't open input file -- %s\n", infname); exit(1); } /* Open the output file for writing. */ if ((outfp = fopen(outfname, "w")) == NULL) { fprintf(stderr, "Couldn't open output file -- %s\n", outfname); exit(1); } /* Loop through all the integer values in the input file and write appropriate values to the output file. */ while (fscanf(infp, "%d", &value) != EOF) { /* We have an integer value. If it is negative or a multiple of 17, then write to the output file. */ if ((value < 0) || ((value % DIVISOR) == 0)) fprintf(outfp, "%d\n", value); } /* End of while. */ /* Close the input file. */ if (fclose(infp) == EOF) { /* Error in closing input file */ fprintf(stderr, "Error in closing file %s\n", infname); exit(1); } /* Close the output file. */ if (fclose(outfp) == EOF) { /* Error in closing output file */ fprintf(stderr, "Error in closing file %s\n", outfname); exit(1); } return 0; } /* End of main. */ ============================================================================