0% found this document useful (0 votes)
66 views5 pages

Workshop 1

This document provides instructions for a workshop on C programming and multithreading. It includes 6 tasks: 1. Modifying sample C programs to take command line arguments or user input for variables. 2. Writing functions to calculate the sum of integers within a range, find the midpoint of a line, and solve simultaneous equations. 3. Creating a multithreaded program to calculate triangular numbers from 0 to 1,000,000 using any number of threads. 4. Writing a multithreaded program to count the occurrences of words in a text file using a specified number of threads. The program must load words from a provided file and output results to separate files depending on the number of threads.

Uploaded by

Tanminder Maan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views5 pages

Workshop 1

This document provides instructions for a workshop on C programming and multithreading. It includes 6 tasks: 1. Modifying sample C programs to take command line arguments or user input for variables. 2. Writing functions to calculate the sum of integers within a range, find the midpoint of a line, and solve simultaneous equations. 3. Creating a multithreaded program to calculate triangular numbers from 0 to 1,000,000 using any number of threads. 4. Writing a multithreaded program to count the occurrences of words in a text file using a specified number of threads. The program must load words from a provided file and output results to separate files depending on the number of threads.

Uploaded by

Tanminder Maan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

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>

void main(int argc, char *argv[])


{
int age = 10;
char *name = "Jeffrey";
printf("Hello %s, you are %d years old.", name, age);
}

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.

When you run it, it should produce the following:

./myprog Ali 100

Hello Ali, you are 100 years old.

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>

void main(int argc, char *argv[])


{
for(int n =0; n <10; n++){
if(n % 2 == 1){
printf("%d is Odd\n", n);
}
else{
printf("%d\n", n);
}
}
}
When you run the program, it should output the following:

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>

6CS005 High Performance Computing – Jeffrey Ting 2


void main(int argc, char *argv[])
{
int a = 3;
int b = 4;
int temp = 0;

printf("a is %d and b is %d\n", a, b);

temp = a;
a = b;
b = temp;

printf("a is now %d and b is now %d", a, b);


}

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>

void main(int argc, char *argv[])


{
int numbers[10];

for (int i=0; i < 10; i++){


numbers[i] = rand();
printf("%d is %d\n", i, numbers[i]);
}
}

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>

void *threadA(void *p){


for(int i=0; i<10; i++){
printf("Thread ID %ld: i=%d\n", pthread_self(), i);
usleep(1000);
}
}
void *threadB(void *p){
for(int i=0; i<10; i++){
printf("Thread ID %ld: i=%d\n", pthread_self(), i);
usleep(1000);
6CS005 High Performance Computing – Jeffrey Ting 3
}
}
void main(){
pthread_t thrID1, thrID2;
pthread_create(&thrID1, NULL, threadA, NULL);
pthread_create(&thrID2, NULL, threadB, NULL);
pthread_join(thrID1, NULL);
pthread_join(thrID2, NULL);
}

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.

6CS005 High Performance Computing – Jeffrey Ting 4


5. Write a program to count occurrence of words in a text file. You are required to use multithreading
for this task. There is a file on canvas (in week 1) called listofwords.txt, your program should load in
this file and count the occurrence of each word by using “n” number of threads, again, this will be
specified using argv[1]. You will need to do some research on how to read the words and come up
with a strategy on how you will make use of multithreading to speed up this task.

6CS005 High Performance Computing – Jeffrey Ting 5

You might also like