CS 136 Assignment 3

Note: float and double and long are prohibited.

Due Wednesday, Oct. 3 at 11:59 am sharp. Late submissions will receive half marks if submitted before Dec. 3 at 11:59 AM sharp.

All work must be submitted to the Marmoset submission system. See the preamble of assignment 1 for more information.

For this assignment, you will submit C program files to Marmoset for grading. Future assignments will require that you submit C, Racket, written work, or some combination.

Your C submissions will be compiled on Ubuntu Linux using the LLVM compiler. They will be executed in the clang safe execution environment. Marmoset will award a nonzero grade when all of these conditions are met:

You should develop and test your programs prior to submission using the gedit tool on Ubuntu Linux with the CS136 RunC plugin installed. Instructors and TAs will decline to provide assistance unless the program output and clang output are both available.

Hand Marking:

When writing your C code, you should follow the guidelines and principles within the C Style and Documentation Guide found HERE.

There is no hand marking on this assignment but style and documentation in your C code will be marked in later assignments.

Assignment 3 Problem 1a. 10 Marks, File: galaxy.compile.err

Use gedit to create a new folder with a name you choose, and enter the C program below into a file named galaxy.c . Run your program by typing Control-R. Submit the file galaxy.compile.err that is created in the same folder.
#include <stdio.h>
int main(void){
   int x;
   printf("The answer is %d\n",x);
   return 0;
}
Note: If you are not running VirtualBox, your program may not produce this error. In this case, create a file called "galaxy.compile.err" containing the line (without quotations): "I did not get the error!". Also, be aware that the behaviour of your setup will be different from that of marmoset and this may make errors difficult to trace in future assignments.

Assignment 3 Problem 1b. 10 Marks, File: galaxy.c

The program in 1a is supposed to print 42 ("The answer is 42" should be on the output) (The Answer to the Ultimate Question of Life, the Universe, and Everything). Fix the program. Test it. Submit the corrected galaxy.c program file.

Assignment 3 Problem 2a. 10 Marks, File: lcm.c

Create the following three files (don't forget to save them), and run the main program. Then create an equivalent set of files, lcmui.c, lcm.h and lcm.c that compute least common multiple. Your implementation of the function lcm(a,b) should work for any non-negative a and b whose least common multiple is less than 2,147,483,648 (2^31). You may include gcd.h . You may not use long or long long. Submit only lcm.c .

Assignment 3 Problem 2b. 10 Marks, File: gcd.c

Modify gcd.c so that it does not use recursion. Marmoset will perform a cursory check; if you defeat this check your mark will be adjusted manually. If you feel falsely accused by Marmoset of using recursion, let your tutor or instructor know.

Assignment 3 Problem 3a. 5 Marks, File: playnewgame.c

You are to implement a C program that automatically plays a simple guessing game as described below. The Marmoset testing environment will supply an implementation of the game whose interface is specified in newgame.h, shown below. Your solution must implement the interface specified in playnewgame.h, shown below. Your solution must use the game specified in newgame.h. Your solution to this part of Problem 3 should be recursive.
   // file: newgame.h
   // Provides:  startgame, guess

   void startgame(int n);
   // starts a new game in which the
   //    game [this module] chooses a secret integer
   //    between 1 and n, and you [the client module]
   //    tries to guess it

   char guess(int x); 
   //     consumes an integer x and produces
   //     one of 'r' or 'h' or 'l', depending on 
   //     whether x is equal to, greater than, or less than
   //     the secret integer
Your automatic player should conform to the interface defined by playnewgame.h as follows:
   // Player for ordered guessing game
   // Provides:  playgame

   int playgame(int n);
   //              uses "newgame.h" to play an entire
   //              game of size n, and produces m,
   //              the total number of guesses actually
   //              used by playgame to produce 'r'
A sample implementation of newgame.c is provided here, but it may not be the one we use for testing. It is strongly recommended that you use this interface and the "input file" and "expected output file" features of RunC for testing. You may not use long or long long.

You may assume that n<2^31. You may not use long or long long.

Assignment 3 Problem 3b. 5 Marks, File: playnewgame.c

Modify your solution to problem 3a to avoid using recursion. You may not use long or long long.