Workshop 1
Workshop 1
This week, we will be mainly assessing your knowledge of C and a bit of multithreading. There are
two sections, the first questions/tasks come with source code for you to get back into C programming
and making slight modifications. For those who want to get straight into building programs from
scratch, scroll down to the “main workshop.”
The tasks below are supported by some source code initially for you to run and modify. This was written by
Jeffrey Ting to help those who need support to get back into C programming from basics to multithreading.
The following code prints out the value of an int variable and a string (char *) :
#include <stdio.h>
1. Now modify the program so that it uses the command line arguments to supply name and age. i.e. it uses
the argc and argv arguments/parameters.
2. Now modify the program again so that it uses the scanf() function to get input from the user for the name
and age.
The following code counts the integer variable n from 0 to 9 and prints out “Odd” if n is even and just the
value of n if it is even:
#include <stdio.h>
0
1 is Odd
6CS005 High Performance Computing – Jeffrey Ting 1
2
3 is Odd
4
5 is Odd
6
7 is Odd
8
9 is Odd
3. Now modify the program so that it counts the variable n from 1 to 100 and, if n is a multiple of 2 ( eg. 2,
4, 6, etc), it would print out the word “Bish”, and if n is a multiple of 3 (eg. 3, 6, 9. 12 etc), it would print out
the word “Bash”, and if n is a multiple of 5 (eg. 5, 10, 15 etc), it would print out the word “Bosh”.
However, if n is a multiple of 2 and 3 (eg. 6), it would print out the words “BishBash”, and if n is a multiple
of 2 and 5 (eg. 10), it would print out the words “BishBosh”, and if n is a multiple of 3 and 5 (eg. 15), it
would print out the words “BashBosh”. Finally, if n is a multiple of 2, 3 and 5 (eg. 30), it would print out the
words “BishBashBosh”.
When you run the program, it will produce something like this:
1
Bish
Bash
Bish
Bosh
BishBash
7
Bish
Bash
BishBosh
11
BishBash
13
Bish
BashBosh
Bish
17
BishBash
19
BishBosh
Bash
Bish
23
BishBash
Bosh
Bish
Bash
Bish
29
BishBashBosh
31
Bish
Bash
The following code swaps the values of the two variables a and b ::
#include <stdio.h>
temp = a;
a = b;
b = temp;
4. Now write a function called swap() that would swap the values of the variables a and b, when you call the
swap() with the variables a and b as parameters. Please note, this exercise requires pointers.
The following program fills an int array of size 10 and fills it with random numbers and prints them out:
#include <stdio.h>
#include <stdlib.h>
5. Now modify it to will ask the user for a number between 1 and 50, and then use the C function
malloc() to allocate an int array of that size, fill it with random numbers and print out the value of each
element of that array.
The following code creates 2 threads in a program and counts to 10 in each thread :
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
6. Modify the program to accept a command line argument to specific the number of threads, and then create
that many threads dynamically to run.
Main Workshop
1. Create a function in C which computes the sigma function. The function should take in 2 parameters
which represent “i” and “n” as integers (see formula below). The function then should return a single
integer which sums all the values within these limits. These parameters should be inputted by the
user using command line arguments, in this case, argv[1] and argv[2] should be used. For example, if
i=10 and n=20, your function should add all the numbers from 10 to 20 and return this value. The
following shows the mathematical equation for this task.
2. Write a function which finds the mid-point coordinates of a line. The function should take in four
parameters ( (x1, y1) , (x2, y2) ).
3. Write a void function which solves simultaneous equations. Your program will take six parameters.
E.g. function(a, b, c, d, e, f){} By solving simultaneous equations, you are finding where the two
lines cross each other, so your function should print an x and y coordinate.
a = number in front of x of equation one
b = number in front of y of equation one
c = constant of equation one
d = number in front of x of equation two
e = number in front of y of equation two
f = constant of equation two
4. Write a program using multithreading to find all triangular from 0 to 1000000. The program should
work with any amount of threads (n number). The number of threads should be inputted via
command line arguments (argv[1]). You will need to use structs and dynamic slicing for this and so
may require additional research (use code from 5cs021 or may need to do some new research on how
to use structs). The output of these triangular numbers should be in “separate” text files depending on
how many threads are used.