Programming Fundamentals

Objectives

  • defining and calling procedures
  • using C input/output (I/O) facilities
  • creating simple arithmetic expressions
  • programming with if statements
  • creating relational expressions
  • displaying varying strings

Activities To Be Completed

The following is a list of all the activities available to complete this week...

Worth 1 mark(s) in total:

  • print_mouse
  • addition

Worth 1 mark(s) in total:

  • get_letter
  • snacking_calculator

Worth 0.5 mark(s) in total:

  • message_cipher

Problem sets are capped at 15 marks (there are 4 possible bonus marks from the three-dot exercises that can bring you up to a total of 15 if you missed out on any other marks in the one- or two-dot exercises).

Completing just the one and two-dot exercises every week can give you the full 15 marks needed in this component.

For more details, see the course outline.

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

When attempting the following exercises, make sure to read the whole exercise, including any hints and assumptions that may make the exercise easier.

Exercise
(●◌◌)
:

Print Mouse

Download print_mouse.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/print_mouse/print_mouse.c .

Complete the C program, print_mouse.c.

The main function has already been written for you.

Your job is to write a procedure called print_mouse which prints out an ascii mouse.

You can NOT put your code in main for this exercise, main must remain unmodified.

Your procedure must be named print_mouse.

The output from your program should look exactly like this:

dcc print_mouse.c -o print_mouse
./print_mouse
 _  _
(o)(o)--. 
 \../ (  )
 m\/m--m'`---
You can run an automated code style checker using the following command:
1511 style print_mouse.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest print_mouse

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_print_mouse print_mouse.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 February 20:00 to obtain the marks for this lab exercise.

Exercise
(●◌◌)
:

Addition

Create a program called addition.c, which will add together the number of students and tutors in a class.

This program should ask for two integers using the message Please enter the number of students and tutors: and then display the sum of the integers as students + tutors = sum.

Note: You can assume that the values entered will always be 2 whole numbers (i.e integers).

Make sure to replace students and tutors with the numbers entered in the same order and the sum with the sum of the two numbers.

Some Examples

./addition
Please enter the number of students and tutors: 2 5
2 + 5 = 7
./addition
Please enter the number of students and tutors: 3 5
3 + 5 = 8
./addition
Please enter the number of students and tutors: -1 5
-1 + 5 = 4
You can run an automated code style checker using the following command:
1511 style addition.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest addition

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_addition addition.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 February 20:00 to obtain the marks for this lab exercise.

Exercise
(●●◌)
:

Print Letters, Given Their Numbers

Download get_letter.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/get_letter/get_letter.c .

Write a C program get_letter.c that prints a letter from the alphabet in either uppercase or lowercase, given its position.

First, scan in a character. This character will determine whether the letter should be printed in uppercase or lowercase.

The character entered will be either 'y' or 'n', i.e. 'yes' or 'no'.

If the character entered is 'y', then the letter should be printed in uppercase.

If the character entered is 'n', then the letter should be printed in lowercase.

Second, scan in an integer. This integer is the 'index' that we will use to determine which letter of the alphabet to print.

The integer entered will be a number between 0 and 25 (inclusive).

This means that index 0 corresponds to 'A', index 1 corresponds to 'B' and so on till we reach index 25 which corresponds to 'Z'.

./get_letter
Uppercase: y
Index: 4
The letter is E
./get_letter
Uppercase: n
Index: 17
The letter is r

Assumptions/Restrictions/Hints

  • You may assume you will always be given a character first.
  • You may assume you will always be given a whole number second.
  • No error checking is required, i.e. The character entered will always be 'y' or 'n', and the integer entered will always be between 0 and 25 (inclusive).
You can run an automated code style checker using the following command:
1511 style get_letter.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest get_letter

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_get_letter get_letter.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 February 20:00 to obtain the marks for this lab exercise.

Exercise
(●●◌)
:

Snacking Calculator

Download snacking_calculator.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/snacking_calculator/snacking_calculator.c .

Fix the C program snacking_calculator.c.

The program has already been written for you, but unfortunately there are a few mistakes that need fixing.

First, your program should ask how many slices of bread you have.

Second, it should calculate and print out how many slices you can snack on while cooking and how many how many sandwiches you will end up making.

You only feel like a snack of 1 or 2 slices of bread, so it will decide if you can snack on 1 or 2 slices so that you are not left with any excess bread. So if you have an even number of slices, you can eat 2 slces of bread, and if you have an odd number of slices, you can eat 1 slice of bread.

Your fixed program should behave like this:

dcc snacking_calculator.c -o snacking_calculator
./snacking_calculator
How many slices of bread do you have? 5
You can snack on 1 slices of bread and make 2 sandwiches!
./snacking_calculator
How many slices of bread do you have? 8
You can snack on 2 slices of bread and make 3 sandwiches!

Assumptions/Restrictions/Clarifications

  • You can assume that the number of slices will always be a whole positive integer.
You can run an automated code style checker using the following command:
1511 style snacking_calculator.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest snacking_calculator

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_snacking_calculator snacking_calculator.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 February 20:00 to obtain the marks for this lab exercise.

Exercise
(●●●)
:

Message Cipher

Download message_cipher.c here, or copy it to your CSE account using the following command:

cp -n /web/cs1511/23T1/activities/message_cipher/message_cipher.c .

Complete the C program message_cipher.c, which scans in a message and either enciphers or deciphers it.

Each message will consist of a total of 4 letters. To encipher a message, we will scan in 4 numbers to add to each of these letters.

For example, if we have the message

"comp"

And, we scanned in the following 4 numbers:

5 2 7 -2

Then, the message is enciphered to

"hqtn"

This is because the numbers 5, 2, 7, -2 are added to the letters 'c', 'o', 'm', 'p' respectively.

Pull up an ascii table if you want to double check this!

Now, we also would like to decipher enciphered messages. Consider we have the enciphered message from the previous example:

"hqtn"

Now, if we input the same numbers as before:

5 2 7 -2

Then, deciphering this message with these numbers should give us the original message:

"comp"

So, we say that deciphering a message instead subtracts these numbers from the letters rather than adding them.

In this exercise, you will add the ability to both encipher/decipher a given message. You will only be given letters as input (can be lower or uppercase!) and will need to follow the rules above.

There are 3 important notes on our ciphering technique:

  • Input numbers can be negative, for example if we want to encipher 'g' with the number -5, then we get 'b'.
  • If an enciphered/deciphered letter goes outside the range of "a-z" then it must wrap around. This means that if an input letter is 'y' and the corresponding input number is 4, then the enciphered letter is 'c'
  • There is no limit on an input number. This means that we could have an input letter like 'h' with a corresponding input number 150 and the corresponding enciphered letter would be 'b'

Here are are some examples for how your program should run:

dcc message_cipher.c -o message_cipher
./message_cipher
Message: comp
Would you like to encipher or decipher this message (e/d)? e
Enter numbers to encipher by: 5 2 0 3
hqms
./message_cipher
Message: hqms
Would you like to encipher or decipher this message (e/d)? d
Enter numbers to decipher by: 5 2 0 3
comp
./message_cipher
Message: tree
Would you like to encipher or decipher this message (e/d)? e
Enter numbers to encipher by: 0 0 -1 1
trdf
./message_cipher
Message: Vosc
Would you like to encipher or decipher this message (e/d)? d
Enter numbers to decipher by: -1 0 1 -1
Word
./message_cipher
Message: CoLd
Would you like to encipher or decipher this message (e/d)? e
Enter numbers to encipher by: 29 -100 40 -3
FsZa
./message_cipher
Message: FsZa
Would you like to encipher or decipher this message (e/d)? d
Enter numbers to decipher by: 29 -100 40 -3
CoLd

Assumptions/Restrictions/Clarifications

  • You may assume that either 'e' or 'd' is entered when prompted - no other characters
  • You may assume that the 4 letters input will always be letters that appear in the alphabet
You can run an automated code style checker using the following command:
1511 style message_cipher.c

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest message_cipher

When you are finished working on this exercise, you and your lab partner must both submit your work by running give:

give cs1511 lab02_message_cipher message_cipher.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 27 February 20:00 to obtain the marks for this lab exercise.

Formatif Feedback (Optional)

Every week, you have the opportunity to submit a piece of code to formatif, and receive feedback on your work! This is entirely optional, but will provide a valuable opportunity to develop your understanding of C, and get advice on problem solving/algorithmic choices.

To get feedback, go to formatif, and upload the code that you'd like a tutor to look at (Note: This cannot be code from assignments - it must be from labs, or code written for your own practice). Once the feedback is ready, you'll receive an email with instructions on how to collect the feedback!

Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Week 3 Monday 20:00 to submit your work.

You cannot obtain marks by e-mailing your code to tutors or lecturers.

You check the files you have submitted here.

Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.

Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1511 classrun -sturec