SOFT3410 Tutorial 1
Introduction to C
Question 1: Setting up
By now, you should have your C compiler set up and an access to a version of Linux, if you have not
done this or you have ran into some issues, please notify your tutor about your problems.
Question 2: Hello From C
As a simple starter question for the semester, you are tasked with writing a simple Hello World
program. You should utilise the printf or puts function for this task. Discuss with your tutor
about the following.
• How are strings represented in C?
• What is #include’s function?
• How does our program get access to puts or printf
Question 3: Maintain a running average
Write a program that can calculate the mean, variance and standard deviation for a given set of num-
bers. Your program should read the numbers from standard input until EOF. You can assume that the
input will always contain valid numbers, and ignore any invalid input. You should print all results to
4 decimal places.
Mean
m (X) =
∑
X
N
Variance
V ar (X) = 1
N
∑N
i=0 (xi −m)
2
Standard Deviation
σ (X) =
√
V ar (X)
1
SOFT3410 Introduction to C
Question 4: Pointer and Arrays
Given these pointer statements, can you provide an equivalent statement?
*p =
(p+10)[0] =
&r[20] =
&(g[0]) =
&*p =
p++ =
&((r[5])[5]) =
(&(p+10)[20])[1] =
What is the difference between pointers and arrays? Can we mix notation?
Question 5: Oh my grid!
Construct a path from a source to destination, you will display your map as a grid to standard output.
Your application will receive source and destination coordinates as command line arguments. The first
two arguments will relate to the source location and last two arguments will relate to the destination.
Your points should be within a 7×6 grid, if the coordinates are out of bounds, your program should
output the error Invalid Coordinates to standard output.
You should utilise Manhattan distance to solve this problem.
1 $ ./grid 1 1 3 4
1 | | | | | | | |
2 | |S| | | | | |
3 | | | | | | | |
4 | | | | | | | |
5 | | | |D| | | |
6 | | | | | | | |
Concurrency Page 2 of 4
SOFT3410 Introduction to C
Question 6: Flip Matrix
Given the following code segment, construct a few functions that can perform the following operations
on a matrix. You may assume the matrix is a fixed size.
int main() {
int matrix[3][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
//… horizontal flip, vertical flip, matrix reverse
/*
Horizontal Flip
3, 2, 1,
6, 5, 4,
9, 8, 7
Vertical Flip
7, 8, 9,
4, 5, 6,
1, 2, 3
Matrix Reverse
9, 8, 7,
6, 5, 4,
3, 2, 1
*/
return 0;
}
• Matrix Reverse
• Horizontal Flip
• Vertical Flip
Try not to create a second matrix to store the different values.
Afterwards, discuss characteristics of two-dimensional arrays with your peers and tutors.
• How is a two-dimensional array laid out in memory?
• How could we create a jagged array?
• Is the data laid out different between a jagged array and a two-dimensional array?
Concurrency Page 3 of 4
SOFT3410 Introduction to C
Question 7: Shortest Path in a grid
Using your solution from question 5, you will now modify it and now include a few different tiles
that will create a barrier between the source and destination. You will need to draw the pathway from
source to destination with these barriers in place.
• X tiles are walls and you will be unable to pass through this
• = tiles show the path between source and destination (You will draw this tile)
• After solving for the case where typical paths are blocked off, introduce a cost associated with
each tile (a number between 1-9), this should influence how the path is found
Example of a grid with walls.
1 | | | | | | | |
2 | |S| | | | | |
3 | | | |X| | | |
4 |X|X|X|X|X| | |
5 | | | |D| | | |
6 | | | | | | | |
A possible path from S to D.
1 | | | | | | | |
2 | |S|=|=|=| | |
3 | | | |X|=|=| |
4 |X|X|X|X|X|=| |
5 | | | |D|=|=| |
6 | | | | | | | |
Question 8: Sorting with a twist
You are required to implement sorting (try merge sort) using the following types and function param-
eters. This merge sort will have a twist with it.
You have been given the a set of structs that will contain x and y coordinates which will named
location. There will be an origin point given to your function that you will need to maintain.
What are we sorting? You are sorting using the distance between the origin and the set of locations.
struct location {
int x;
int y;
char* name;
};
void sort(int origin_x, int origin_y, struct location* locs,
unsigned len);
Concurrency Page 4 of 4