lab05
by
typing:
mkdir lab05Change to this directory by typing:
cd lab05
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
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
give cs1511 lab05_circle_facts circle_facts.cNote, 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.
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
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
give cs1511 lab05_array_maximum array_maximum.cNote, 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.
vector_distance.c
that:
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.
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.
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
give cs1511 lab05_vector_distance vector_distance.cNote, 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.
vector_permutation.c
that:
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
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.
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
give cs1511 lab05_vector_permutation vector_permutation.cNote, 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.
vector_best_permutation.c
that:
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
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.
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
give cs1511 lab05_vector_best_permutation vector_best_permutation.cYou 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.
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
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 -sturecThe 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.