Computer Science 3 - 2001 - Test on Prac 14 (This should take no longer than 15 minutes and is simply intended to probe whether you have understood the material in the prac questions. Answer on the questions sheet.) THURSDAY TEST: 2. A student has written a C++ program that should read and sum 10 integers. The relevant code reads int sum = 0, num; for (int i = 0; i < 10; i++) { scanf("%d%", num); sum + num; } printf("The total is %d\n", sum); To the student's dismay the code compiles happily, but does not execute correctly. Why not? (Simply indicate the correction(s) on the code above.) int sum = 0, num; for (int i = 0; i < 10; i++) { scanf("%d", &num); // two critical errors sum += num; // bad operator } printf("The total is %d\n", sum); 3. I have received many complaints this week. Students have muttered that Clang is far too restrictive - it does not have AND and OR operators, so while code like while (i < 10) and (list[i] <> item) do i := i + 1; seems to be what they want to write, they find they cannot do this and are having trouble solving the problem. But there are at least two ways of expressing that idea correctly in Clang, aren't there? Show me one of them! One way is to code up the condition into a function call: FUNCTION Continue(I, List[], Item); BEGIN IF I >= 10 THEN RETURN FALSE; IF List[I] = Item THEN RETURN FALSE; RETURN TRUE END; WHILE Continue(I, List, Item) = TRUE DO I := I + 1; where TRUE and FALSE are defined somewher as the appropriate CONST values. Other ways would require really kludgy code, for example Continue := TRUE; IF I >= 10 THEN Continue := FALSE; IF List[I] = Item THEN Continue := FALSE; WHILE Continue := 1 DO BEGIN I := I + 1; Continue := TRUE; IF I >= 10 THEN Continue := FALSE; IF List[I] = Item THEN Continue := FALSE; END; 4. Okay, okay. So many people complained that I've decided I should change the Clang system to allow AND and OR operations. One student, doubting my brilliance (or maybe one who was just curious), asked how difficult this is going to be. By way of explanation I sketched a few T diagrams to show what I would do, explaining that I could download the (Modula-2) sources for the present compiler from the WWW, and that I would do the job in the Braae lab. What did the T diagrams look like? Hack the Modula Source: The new Executable ---------------------------- ---------------------------- | CLN.MOD | | CLN.EXE | | CLN ----------> PCode | | CLN -----------> PCode | | | | | --------- ---------------------------- --------- | | M2.EXE | | | M2 | M2 --------> 8086 | 8086 | | | | | -------------------- -------------------- | | | 8086 | <--- The JPI compiler | | ------------ | | | 8086 | | | ------------ FRIDAY TEST: 2. A student tried converting the Sieve program from Modula-2 to C++. All was going well when she discovered that there were several calls to routines like WriteLn, WriteString and WriteCard. Being a lazy soul she decided to add code to misc.h that would just provide these routines. Being a lazier soul she has asked you to do this for her. How do you complete the following code using the scanf and printf functions in the stdio library? void WriteString (char *s) { } void WriteLn (void) { } void WriteCard (unsigned int x, int width) { } void ReadCard (unsigned int &x) { } These are all "one-liners": void WriteString (char *s) { printf("%s", s); } void WriteLn (void) { printf("\n"); } void WriteCard (unsigned int x, int width) { printf("%*u", width, x); } void ReadCard (unsigned int &x) { scanf("%u", &x); } 3. Another group in the class were fascinated by the discovery that they could write concurrent programs in Clang. One of their attempts read as follows program Concurrent; var counter; procedure countto10; var semaphore, i; begin wait(semaphore); i := 1; while i <= 10 do; begin counter := counter + 1; signal(semaphore); end; end; begin counter := 0; cobegin countto10; countto10; countto10 coend; write('Counter is now ', counter); end. They were pleased when this compiled, but furious when it did not seem to work as they had expected. Point out the follies of their ways. program Concurrent; var semaphore, (* must be global *) counter; procedure countto10; var (* semaphore, *) i; (* semaphore cannnot be local *) begin (* wait(semaphore); *) (* wrong place *) i := 1; while i <= 10 do (* ; *) (* classic silly mistake *) begin wait(semaphore); (* must wait here *) counter := counter + 1; signal(semaphore); end; end; begin counter := 0; semaphore := 1; (* must initialise semaphore *) cobegin countto10; countto10; countto10 coend; write('Counter is now ', counter); end. 4. Using XC is dead easy, right? Given an earth shaking program as HELLO.MOD MODULE Hello; IMPORT EasyIO; BEGIN EasyIO.WriteString("Hello Pat, you old fool") END Hello. all you have to do to be polite to your lecturer is to tell him to issue the following commands XC =m HELLO.MOD BCC -C HELLO.C BCC HELLO.OBJ EASYIO.OBJ X2C.OBJ HELLO Draw the T diagrams that correspond to each of these operations. ---------------------------- ---------------------------- | Hello.Mod | | Hello.C | | ------ > "hello" | | --------> "hello" | | | | | --------- ---------------------------- --------- | | XC.EXE | | | M2 | M2 --------> C | C | | | | | -------------------- -------------------- | | | 8086 | | | ------------ | | | 8086 | | | ------------ ---------------------------- ---------------------------- | Hello.C | | Hello.OBJ | | ------ > "hello" | | --------> "hello" | | | | | --------- ---------------------------- --------- | | BCC.EXE | | | C | C --------> OBJ | OBJ | | | (used as compiler) | | -------------------- -------------------- | | | 8086 | | | ------------ | | | 8086 | | | ------------ ---------------------------- ---------------------------- | Hello.OBJ | | Hello.EXE | | ------ > "hello" | | --------> "hello" | | + EASYIO.OBJ | | | --------- ---------------------------- --------- | | BCC.EXE | | | OBJ | OBJ --------> 8086 | 8086 | | | (used as linker) | | -------------------- -------------------- | | | | | 8086 | | 8086 | | | | | ------------ ------------ | | | 8086 | | | ------------