COMP1511 19T2
COMP1511 19T2

Objectives

  • creating functions
  • manipulating arrays
  • using a while loop for repetition

Preparation

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

Getting Started

Create a new directory for this lab called lab05 by typing:
mkdir lab05
Change to this directory by typing:
cd lab05

Exercise: Fun facts about Circles (pair)

This is a pair exercise to complete with your lab partner.
In this exercises you will add code to a program to calculate fun facts about circles. Download circle_facts.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/19T2/activities/circle_facts/circle_facts.c .
Your task is to add code to these functions in circle_facts.c:
// Calculate the area of a circle, given its radius.
double area(double radius) {
    // TODO: complete this function.
    return M_PI; // TODO: change this to the correct return value.
}

// Calculate the circumference of a circle, given its radius.
double circumference(double radius) {
    // TODO: complete this function.
    return M_PI; // TODO: change this to the correct return value.
}

// Calculate the diameter of a circle, given its radius.
double diameter(double radius) {
    // TODO: complete this function.
    return M_PI; // TODO: change this to the correct return value.
}

Its main function is complete. Do not change the main function. Complete these three functions:

double area(double radius);
double circumference(double radius);
double diameter(double radius);

Hint use the constant M_PI defined in math.h

dcc -o circle_facts  circle_facts.c 
./circle_facts 
Enter circle radius: 1
Area          = 3.141593
Circumference = 6.283185
Diameter      = 2.000000
./circle_facts
Enter circle radius: 17
Area          = 907.920277
Circumference = 106.814150
Diameter      = 34.000000
./circle_facts
Enter circle radius: 0.0125
Area          = 0.000491
Circumference = 0.078540
Diameter      = 0.025000
New! You can run an automated code style checker using the following command:
1511 style circle_facts.c

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

1511 autotest circle_facts

Autotest Results

100% of 491 students who have autotested circle_facts.c so far, passed all autotest tests.
  • 100% passed test 1
  • 100% passed test 2
  • 100% passed test 3
  • 100% passed test 4
  • 100% passed test 5
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab05_circle_facts circle_facts.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 08 July 17:00 to obtain the marks for this lab exercise.

Exercise: Find the Maximum Value in an Array of Integers (pair)

This is a pair exercise to complete with your lab partner.
Download array_maximum.c here, or copy it to your CSE account using the following command:
cp -n /web/cs1511/19T2/activities/array_maximum/array_maximum.c .
Your task is to add code to this function in array_maximum.c:
// Returns the maximum value from an array of `size` integers.
int array_maximum(int size, int array[size]) {

    int max = 42; // TODO: change this line!

    // TODO: complete this function.

    return max;
}

The above file array_maximum.c contains a function array_maximum, which should find the maximum value in the array.

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

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

This main function will not be marked -- you must write all of your code in the array_maximum function. You may modify the main function if you wish (e.g. to add further tests), but only the array_maximum 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_maximum array_maximum.c 
./array_maximum 
The largest value from array1 is: 15
The largest value from array2 is: 512
New! You can run an automated code style checker using the following command:
1511 style array_maximum.c

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

1511 autotest array_maximum

Autotest Results

97% of 480 students who have autotested array_maximum.c so far, passed all autotest tests.
  • 97% passed test 1
  • 97% passed test 2
  • 97% passed test 3
  • 97% passed test 4
  • 97% passed test 5
  • 97% passed test 6
  • 97% passed test 7
  • 97% passed test 8
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab05_array_maximum array_maximum.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 08 July 17:00 to obtain the marks for this lab exercise.

Exercise: Compute the Euclidean Distance Between Two Vectors of Integers (pair)

This is a pair exercise to complete with your lab partner.
Write a program vector_distance.c that:
  • reads a positive integer n
  • reads vector1 of n integers p0 p1 ... pn - 1
  • reads vector2 of n integers q0 q1 ... qn - 1
  • prints the Euclidean Distance between vector1 and vector2
Match the example output below EXACTLY:
 dcc -o vector_distance vector_distance.c
 ./vector_distance
Enter vector length: 4
Enter vector 1: 3 6 0 1
Enter vector 2: 2 7 1 2
Euclidean distance = 2.000000
 ./vector_distance
Enter vector length: 5
Enter vector 1: 42 43 44 42 42
Enter vector 2: 42 43 44 42 42
Euclidean distance = 0.000000
 ./vector_distance
Enter vector length: 1
Enter vector 1: 20
Enter vector 2: 10
Euclidean distance =  10.000000
 ./vector_distance
Enter vector length: 3
Enter vector 1: 1 2 3
Enter vector 2: 3 2 1
Euclidean distance =  2.828427
 ./vector_distance
Enter vector length: 6
Enter vector 1: 1 2 3 5 7 11
Enter vector 2: 31 29 23 19 17 13
Euclidean distance =  48.259714

Hint: use two arrays to store the vectors

Hint: use a while loop and scanf to read the integers into each array

Hint: you will need #include <math.h> at the top of vector_distance.c so you can call C's sqrt function.

Assumptions/Restrictions/Clarifications.

No error checking is necessary.

You can assume n is a positive integer less than 1000.

You can assume you are given two vectors of n integers.

You can assume your input contains nothing but integers.

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

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

1511 autotest vector_distance

Autotest Results

95% of 471 students who have autotested vector_distance.c so far, passed all autotest tests.
  • 96% passed test 1
  • 97% passed test 2
  • 96% passed test 3
  • 95% passed test 4
  • 95% passed test 5
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab05_vector_distance vector_distance.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 08 July 17:00 to obtain the marks for this lab exercise.

Exercise: Rearrange a Vector of Integers (pair)

This is a pair exercise to complete with your lab partner.
Write a program vector_permutation.c that:
  • reads a positive integer n
  • reads n integers p0 p1 ... pn - 1
  • reads n integers q0 q1 ... qn - 1
  • checks q0 ... qn - 1 are non-negative integers less than n
  • prints pq0, pq1 ... pqn - 1

In other words the first n integers, p0 p1 ... pn-1 specify a vector.

The second n integers, q0 q1 ... qn-1, specify how that vector should rearranged.

So for example if the vector has 5 elements and q0 q1 ... qn-1 are 0 1 2 3 4 then the vector will be unchanged.

And if q0 q1 ... qn - 1 are 4 3 2 1 0 then the vector will be reversed.

Match the example output below EXACTLY:

dcc -o vector_permutation vector_permutation.c
./vector_permutation
Enter vector length: 4
Enter vector: 42 43 44 45
Enter permutation: 3 1 2 0
45 43 44 42
./vector_permutation
Enter vector length: 6
Enter vector: 40 41 42 43 44 45
Enter permutation: 0 1 2 3 4 5
40 41 42 43 44 45
./vector_permutation
Enter vector length: 6
Enter vector: 40 41 42 43 44 45
Enter permutation: 5 4 3 2 1 0
45 44 43 42 41 40
./vector_permutation
Enter vector length: 1
Enter vector: 1024
Enter permutation: 0
1024
./vector_permutation
Enter vector length: 5
Enter vector: 48 36 48 24 12
Enter permutation: 4 3 1 0 2
12 24 36 48 48

In all the examples above, q0 q1 ... qn is a permutation of the integers 0..n-1.

You don't have to check this is the case.

However you do have to to check q1 ... qn are all >= 0 and < n.

Match the example output below EXACTLY:

./vector_permutation
Enter vector length: 5
Enter vector: 48 36 48 24 12
Enter permutation: 4 3 5 0 2
Invalid permutation

Hint: use an arrays to store the vector and an array to store the permutation

Hint: use the values in permutation arrays as indices for the vector array after checking they are valid

Assumptions/Restrictions/Clarifications.

You can assume n is a positive integer less than 1000.

You can assume you are given two vectors of n integers.

You can assume your input contains nothing but integers.

You may print an extra space at the end of the line when printing the permutation.

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

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

1511 autotest vector_permutation

Autotest Results

89% of 450 students who have autotested vector_permutation.c so far, passed all autotest tests.
  • 97% passed test 1
  • 97% passed test 2
  • 97% passed test 3
  • 97% passed test 4
  • 95% passed test 5
  • 91% passed test 6
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab05_vector_permutation vector_permutation.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 08 July 17:00 to obtain the marks for this lab exercise.

Challenge Exercise: Compute the Permutation Which Minimizes the Euclidean Distance Between Two Vectors (individual)

This is an individual exercise to complete by yourself.
Write a program vector_best_permutation.c that:
  • reads a positive integer n
  • reads vector1 of n integers p0 p1 ... pn - 1
  • reads vector2 of n integers q0 q1 ... qn - 1
  • prints the permutation of vector1 that minimizes the Euclidean Distance with vector2
  • prints the Euclidean Distance between the permuted vector1 and vector2
Match the example output below EXACTLY:
dcc -o vector_best_permutation vector_best_permutation.c
./vector_best_permutation
Enter vector length: 4
Enter vector 1: 6 8 15 11
Enter vector 2: 42 43 17 3
Optimal permutation: 3 2 1 0
Euclidean distance = 42.836900
./vector_best_permutation
Enter vector length: 6
Enter vector 1: 18 16 19 11 42 32
Enter vector 2: 77 64 11 99 21 42
Optimal permutation: 5 2 3 4 1 0
Euclidean distance = 88.881944
./vector_best_permutation
Enter vector length: 32
Enter vector 1: 34 56 42 44 35 38 39 50 62 61 58 43 45 32 33 48 46 52 63 57 55 37 53 49 59 41 51 47 40 54 60 36
Enter vector 2: 23 30 13 16 14 33 25 41 15 32 35 26 28 40 31 38 39 11 19 21 37 18 27 12 36 17 24 34 22 20 10 29
Optimal permutation: 12 17 4 5 31 20 27 18 21 29 19 15 7 8 22 30 9 14 25 11 24 28 23 0 10 6 16 1 3 2 13 26
Euclidean distance = 124.450793

Assumptions/Restrictions/Clarifications.

The only C library functions you are permitted to use are printf, scanf and sqrt.

If there are multiple permutations of equal minimum Euclidean distance, you may print any of them.

You program must take less than 20 seconds to compute the answer on a CSE lab machine when compiled with dcc.

No error checking is necessary.

You can assume n is a positive integer less than 100.

You can assume you are given two vectors of n integers.

You can assume your input contains nothing but integers.

You may print an extra space at the end of the line when printing the permutation.

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

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

1511 autotest vector_best_permutation

Autotest Results

68% of 60 students who have autotested vector_best_permutation.c so far, passed all autotest tests.
  • 78% passed test 1
  • 70% passed test 2
  • 68% passed test 3
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab05_vector_best_permutation vector_best_permutation.c
You must run give before Monday 08 July 17:00 to obtain the marks for this lab exercise. Note, this is an individual exercise, the work you submit with give must be entirely your own.

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 Monday 08 July 17:00 to submit your work.

You cannot obtain marks by e-mailing lab work 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 for the test, using test cases that you haven't seen: different to the test cases autotest runs for you.

(Hint: do your own testing as well as running autotest)

After automarking is run by the lecturer you can view it here the resulting mark will also be available via 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
The lab exercises for each week are worth in total 2 marks.

The best 8 of your 9 lab marks for weeks 2-10 will be summed to give you a mark out of 13. If their sum exceeds 13 - your mark will be capped at 13.

  • You can obtain full marks for the labs without completing challenge exercises
  • You can miss 1 lab without affecting your mark.