Favourite Number 2

This is a warmup exercise. It is not compulsory, and may be completed individually or with your lab partner.

Last week we looked at my favourite number, 17. This week we are going to write some functions to examine the properties of the user’s favourite number. Copy the program favourite2.c from the course account to your directory by typing (make sure you type the dot at the end):

$ cp /web/cs1511/17s2/week03/files/favourite2.c .

The dot ‘.’ is a shorthand for the current directory and there is a space between favourite2.c and the next dot. You can check that the file has been copied by typing:

$ ls
favourite2.c

This file already has a main function that is complete, do not change anything in it.

Functions to Complete

There are three function prototypes for you to implement. These are:

int square (int n);
void printOddOrEven (int n);
int multiply (int a, int b);

The void printOddOrEven (int n) function takes in an integer, n, and prints out whether the number is odd or even, in this format: “n is odd.” It does not return a value.

Both the square and multiply functions return a value. The int square (int n) function takes in an integer, n, and should return n squared. The int multiply (int a, int b) function takes in two integers, a and b, and returns a times b.

Some examples

$ ./favourite2
Enter your favourite number: 4
You entered 4.
4 doubled is 8.
4 squared is 16.
4 x 17 is 68.
4 is even.
$ ./favourite2
Enter your favourite number: -1
You entered -1.
-1 doubled is -2.
-1 squared is 1.
-1 x 17 is -17.
-1 is odd.

To run some simple automated tests:

$ 1511 autotest favourite2

To run Styl-o-matic:

$ 1511 stylomatic favourite2.c
Looks good!

You’ll get advice if you need to make changes to your code.

Submit your work with the give command, like so:

$ give cs1511 wk03_favourite2

Or, if you are working from home, upload the relevant file(s) to the wk03_favourite2 activity on Give Online.