Programming Fundamentals

Objectives

  • introduction to pointers
  • creating mallocd arrays
  • processing of characters and strings
  • use of functions
  • an introduction to encryption & decryption

Feedback Week!

In this week's lab, your tutors will go around the class and give you one on one feedback on some code you've written in a previous week.

This is also an opportunity for you to ask any questions you might have about the course content so far!

So, if you'd like, have a think about if there's any particular exercise you'd like to receive feedback for, or any particular content you'd like to ask about.

Reminder: Help sessions

Help sessions are running this week!

These are one of the best ways for you to get one on one help with a tutor for any course content (including Lab Exercises and Assignments).

For the dates and times of the help sessions, see the Help Session Timetable.

To join a help session, or for more information, see the COMP1511 Help Session Microsoft Teams.

For face-to-face help sessions, the lab map can be found Here.

Activities To Be Completed

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

Worth 1 mark(s) in total:

  • array_sum_prod
  • swap_pointers
  • my_scanf

Worth 1 mark(s) in total:

  • string_search
  • substitution

Worth 0.5 mark(s) in total:

  • frequency_analysis

For your interest, but not for marks:

  • word_search

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.

Videos

The following short videos may be of some help to get started on the lab exercises.

If you have watched the videos and are still unsure how to start the exercises, ask your tutor for help :)

Exercise
(●◌◌)
:

Calculate both the sum and product of the values in an array

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

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

Your task is to add code to this function in array_sum_prod.c:

// Calculates the sum and product of the array nums.
// Actually modifies the  variables that *sum and *product are pointing to
void array_sum_prod(int length, int nums[length], int *sum, int *product) {
    // TODO: Complete this function
}

The above file array_sum_prod.c contains a function array_sum_prod, which should find the sum and the product of the values stored in the array. It should write these values into the integers referenced by the pointers in the input to the function.

Unfortunately, the provided function doesn't actually work. For this lab exercise, your task is to complete this function.

Note: you must not modify the array within the array_sum_prod function. You should only read the values in the array, not change them.

Note: you will not be given an empty array as input, you can assume that you have at least 1 value.

The file also contains a main function which you can use to help test your array_sum_prod function. It has two simple test cases.

This main function will not be marked -- you must write all of your code in the array_sum_prod function. You may modify the main function if you wish (e.g. to add further tests), but only the array_sum_prod function will be marked.

Once your program is working, the output from the two provided tests in the main function should be:

dcc -o array_sum_prod array_sum_prod.c 
./array_sum_prod 
Sum: 20, Product: 360
Sum: 10, Product: 24
You can run an automated code style checker using the following command:
1511 style array_sum_prod.c

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

1511 autotest array_sum_prod

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

give cs1511 lab07_array_sum_prod array_sum_prod.c

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

Exercise
(●◌◌)
:

Using pointers and a function to swap number values

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

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

Your task is to add code to this function in swap_pointers.c:

// swap the values in two integers, given as pointers
void swap_pointers(int *a, int *b) {
    // PUT YOUR CODE HERE
}
swap_pointers should take two pointers to integers as input and swap the values stored in those two integers.

For example if the integers are:

int first = 1;
int second = 2;

After your function runs, first should be 2 and second should be 1.

Assumptions/Restrictions/Clarifications.

swap_pointers is a void function. It cannot return any values.

swap_pointers should not call scanf (or getchar or fgets).

swap_pointers should not print anything. It should not call printf.

Your submitted file may contain a main function. It will not be tested or marked.

You can run an automated code style checker using the following command:
1511 style swap_pointers.c

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

1511 autotest swap_pointers

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

give cs1511 lab07_swap_pointers swap_pointers.c

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

Exercise
(●◌◌)
:

My Scanf

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

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

Ben Briant has decided to learn how scanf() works, and to help with his understanding, he has decided to write his own simple scanf functions!

They are much simpler than a regular scanf, as they only allow for scanning in a single integer or a single double (depending on if my_scanf_int() or my_scanf_double() is being called) at a time.

Both of these functions should:

  • take in a pointer to a variable that we want to scan our value into,
  • read a value from the user (using regular scanf), then
  • set the value of the input to be the value read from the user.

Unfortunately, Ben got overwhelmed with all the pointer syntax (* and &), so decided to just avoid writing any of it! Your job is to put all the * and & in the correct spots in the code.

You will have to edit both the main() function, my_scanf_int() and my_scanf_double(), But you can't change any part of the code aside from adding * or &.

Once your program is working, it should work like this:

dcc -o my_scanf my_scanf.c 
./my_scanf 
Enter the amount of study you need to do this week (in decimal): 7.5
Enter the number of days you have free: 3
You have on average 2.50 hour(s) each free day to do homework.

Testing

When testing your program, we are checking more than that it simply produces the correct output. We are also testing that the functions work correctly individually (by replacing your main function with one of our own).
You can run an automated code style checker using the following command:
1511 style my_scanf.c

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

1511 autotest my_scanf

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

give cs1511 lab07_my_scanf my_scanf.c

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

Exercise
(●●◌)
:

String Search

Your job is to write a program called string_search.c which lets us count the number of times we see any our of "search terms" in a list of words.

The search terms will be provided to our program via command line arguments. For example:

./string_search hello there

would run the program with 2 search terms - hello and there.

TASK 1: Edit the arguments to your main function so that it can take in command line arguments (aka, the search terms). Try printing out the values of argc and argv and changing the values you type after ./string_search to see what they contain!

TASK 2: Scan in words from standard input until Ctrl-D is pressed.

TASK 3: Count the number of times the search terms appear in the input.

Your program should work like the following:

dcc -o string_search string_search.c 
./string_search same
Enter list of words:
same
sand
same
send
shade
same
shadow

There were 3 occurrence(s) in the input.
./string_search many search terms
Enter list of words:
terms
many
same
many
shade
search
research

There were 4 occurrence(s) in the input.

Assumptions/Restrictions/Hints

  • You can assume that each word will be no longer than 128 characters long
  • You can assume that there will only be 1 word per line.
  • You can assume that the search terms will never have repeats in them.
You can run an automated code style checker using the following command:
1511 style string_search.c

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

1511 autotest string_search

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

give cs1511 lab07_string_search string_search.c

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

Exercise
(●●◌)
:

Encrypting Text with a Substitution Cipher

Write a C program substitution.c which reads characters from its input and writes the characters to its output encrypted with a Substitution cipher.

A Substitution cipher maps each letter to another letter.

The mapping will be given to your program as the first input (no spaces between any characters). This is all on one line. This input will contain 26 characters: an ordering of the letters 'a'..'z'.

Characters other than letters should not be encrypted.

Your program should stop only at the end of input.

Your program should contain at least one function other than main.

For example:

./substitution
Enter mapping:
qwertyuiopasdfghjklzxcvbnm
Enter text:
I was scared of dentists and the dark
O vql leqktr gy rtfzolzl qfr zit rqka
I was scared of pretty girls and starting conversations
O vql leqktr gy hktzzn uoksl qfr lzqkzofu egfctklqzogfl

./substitution
Enter mapping:
abcdefghijklmnopqrstuvwxyz
Enter text:
The identity cipher!!!
The identity cipher!!!

./substitution
Enter mapping:
bcdefghijklmnopqrstuvwxyza
Enter text:
The Caesar cipher is a subset of the substitution cipher!
Uif Dbftbs djqifs jt b tvctfu pg uif tvctujuvujpo djqifs!

Your program will only be tested with an appropriate and valid mapping input for marking - but a good programmer would check the input is present and appropriate.
You can run an automated code style checker using the following command:
1511 style substitution.c

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

1511 autotest substitution

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

give cs1511 lab07_substitution substitution.c

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

Exercise
(●●●)
:

Frequency Analysis

Write a C program frequency_analysis.c which reads characters from its input until end of input.

It should then print the occurrence frequency for each of the 26 letters 'a'..'z'.

The frequency should be printed as a decimal value and an absolute number in exactly the format below.

Note upper and lower case letters are counted together.

For example:

./frequency_analysis
Hello and goodbye.

'a' 0.066667 1
'b' 0.066667 1
'c' 0.000000 0
'd' 0.133333 2
'e' 0.133333 2
'f' 0.000000 0
'g' 0.066667 1
'h' 0.066667 1
'i' 0.000000 0
'j' 0.000000 0
'k' 0.000000 0
'l' 0.133333 2
'm' 0.000000 0
'n' 0.066667 1
'o' 0.200000 3
'p' 0.000000 0
'q' 0.000000 0
'r' 0.000000 0
's' 0.000000 0
't' 0.000000 0
'u' 0.000000 0
'v' 0.000000 0
'w' 0.000000 0
'x' 0.000000 0
'y' 0.066667 1
'z' 0.000000 0
./frequency_analysis
Hey! Hey! Hey!
I don't like walking around this old and empty house
So hold my hand, I'll walk with you my dear

'a' 0.072289 6
'b' 0.000000 0
'c' 0.000000 0
'd' 0.084337 7
'e' 0.084337 7
'f' 0.000000 0
'g' 0.012048 1
'h' 0.096386 8
'i' 0.072289 6
'j' 0.000000 0
'k' 0.036145 3
'l' 0.084337 7
'm' 0.036145 3
'n' 0.060241 5
'o' 0.084337 7
'p' 0.012048 1
'q' 0.000000 0
'r' 0.024096 2
's' 0.036145 3
't' 0.048193 4
'u' 0.036145 3
'v' 0.000000 0
'w' 0.036145 3
'x' 0.000000 0
'y' 0.084337 7
'z' 0.000000 0

Hint: use an array to store counts of each letter.

Hint: make sure you understand this example program which counts integers from the range 0..99.

Manually Cracking a Substitution Cipher

This English text was encrypted with a substitution cipher.
Di jd, vdl'ht xtqa dh O qn
Vdl rdlwk O'ss wdkith htqromu omkd ok
O fhdwqwsv xdm'k
Styk kd nv dxm rtzoetj
Wlk kiqk'j kit royythtmet om dlh dfomodmj

Vdl'ht q ndlkiyls
Kiqk qndlmkj ydh qmdkith xtta dm nv dxm
Mdx O'n q mdzts nqrt htjdlhetyls
O jkqhk q eiqom xoki nv kidluik

Kqsa oj eitqf, nv rqhsomu
Xitm vdl'ht yttsomu houik qk idnt
O xqmmq nqat vdl ndzt xoki edmyortmet
O xqmmq wt xoki vdl qsdmt
What was the original text?

You can run an automated code style checker using the following command:
1511 style frequency_analysis.c

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

1511 autotest frequency_analysis

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

give cs1511 lab07_frequency_analysis frequency_analysis.c

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

Exercise
(☠)
:

Solving a 3D word search

Write a C program word_search.c which takes in as input from terminal:
  • A number n followed by any number of words
  • An nxnxn cube of characters

3D Arrays

Note

3D arrays are outside the scope of this course. The use of them in this exercise is simply to allow those that are interested to use/explore them.

Reminder: This exercise is not worth any marks, so do not feel obliged to learn things that aren't in this course!

Before reading any further, it is important to note that you will be expected to use 3D arrays in this exercise (although it is possible without them). A small snippet on how to create a 3d array is shown below.

#include <stdio.h>

#define X_SIZE 5
#define Y_SIZE 3
#define Z_SIZE 7

int main(void) {
    // Declaring a 3D array of integers and initialising all elements to 0
    int cube[X_SIZE][Y_SIZE][Z_SIZE] = {0};

    // Filling element at index (2, 1, 5) with value '6'
    cube[2][1][5] = 6;

    // Printing this value
    printf("%d", cube[2][1][6]);
}

A 3D array can be thought of in a couple of ways

  • A Rectangular prism made up of small cubes (each array element)
  • An array of 2D arrays (array of grids)
  • An array of arrays of arrays (Rolls right off the tongue)
  • A CPU's nightmare

In this exercise, there is a cube which represents a 3D word search grid. This cube is filled in by from values scanned in when the program is actually run. Your goal is to determine whether each of the provided words in the given input arguments can be found within this cube.

The rules for finding a word are as follows:

  • Words can only appear in straight lines in the cube
  • Words will only be going through one dimension of the cube, this means if a word is found through the x dimension, all characters in that word will have the same y and z positions
  • Words cannot exist as diagonals
  • Words will not appear backwards. This means that words can only be found when going through each dimension. In terms of how this would work with a 3D array, you will never need to go backwards from an index when searching for a word
Here are some examples of what the programming is expected to do:
./word_search
Please enter cube length: 2
Please enter grid 0:
oa
np
Please enter grid 1:
on
ai

on
on found!
an
an found!
po
po not found!
pi
pi found!
ai
ai found!
pa
pa not found!

./word_search
Please enter cube length: 3
Please enter grid 0:
daa
ood
gwd
Please enter grid 1:
opb
pda
pod
Please enter grid 2:
opo
edm
sup

dog
dog found!
doge
doge not found!
cat
cat not found!
ood
ood found!
add
add found!
friend
friend not found!
app
app found!
op
op found!
man
man not found!
bad
bad found!
long
long not found!
dam
dam found!
supercalifragilisticexpialidocious
supercalifragilisticexpialidocious not found!
low
low not found!
woo
woo not found!
moo
moo not found!
pod
pod found!
lip
lip not found!

Assumptions

  • You will always be given an integer as the first input.
  • All inputs will be valid.
  • The cube size that is input will be in the range [1, 32] inclusive.
You can run an automated code style checker using the following command:
1511 style word_search.c

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

1511 autotest word_search

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

give cs1511 lab07_word_search word_search.c

Note, even though this is a pair exercise, you both must run give from your own account before Monday 03 April 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 8 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