numerical
valueslab03
by
typing:
mkdir lab03Change to this directory by typing:
cd lab03
Write a C program order3.c
using if statements
(no loops) that reads 3 integers and prints them from smallest to
largest.
Your program should behave exactly like this example:
./order3 Enter integer: 23 Enter integer: 5 Enter integer: 27 The integers in order are: 5 23 27 ./order3 Enter integer: 3 Enter integer: 6 Enter integer: 27 The integers in order are: 3 6 27 ./order3 Enter integer: 9 Enter integer: 8 Enter integer: 7 The integers in order are: 7 8 9You can assume the user supplies 3 integers. You do not have to check the return value from scanf.
1511 style order3.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest order3
give cs1511 lab03_order3 order3.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 24 June 17:00 to obtain the marks for this lab exercise.
Write a C program is_leap_year.c
that reads a year and
then prints whether that year is a
leap year.
Match the examples below exactly
Hint: you only need use the int type, modulus (%) and if statement(s).
For example:
dcc -o is_leap_year is_leap_year.c ./is_leap_year Enter year: 2017 2017 is not a leap year. ./is_leap_year Enter year: 2016 2016 is a leap year. ./is_leap_year Enter year: 2000 2000 is a leap year. ./is_leap_year Enter year: 3000 3000 is not a leap year.
1511 style is_leap_year.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest is_leap_year
give cs1511 lab03_is_leap_year is_leap_year.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 24 June 17:00 to obtain the marks for this lab exercise.
Write a C program percentage.c
to calculate the marks
that a student got in an exam.
Your program should scan in two integers: the total number of marks in the exam, and how many marks the student was awarded.
It should then print out what mark they got in the exam, i.e. what percentage of the marks the student was awarded for that exam, with no decimal places.
You are not permitted to use the math library for this question.
Your program should behave exactly like this example:
./percentage Enter the total number of marks in the exam: 10 Enter the number of marks the student was awarded: 5 The student scored 50% in this exam. ./percentage Enter the total number of marks in the exam: 10 Enter the number of marks the student was awarded: 1 The student scored 10% in this exam. ./percentage Enter the total number of marks in the exam: 5 Enter the number of marks the student was awarded: 2 The student scored 40% in this exam. ./percentage Enter the total number of marks in the exam: 1 Enter the number of marks the student was awarded: 1 The student scored 100% in this exam. ./percentage Enter the total number of marks in the exam: 100 Enter the number of marks the student was awarded: 5 The student scored 5% in this exam. ./percentage Enter the total number of marks in the exam: 3 Enter the number of marks the student was awarded: 1 The student scored 33% in this exam. ./percentage Enter the total number of marks in the exam: 20 Enter the number of marks the student was awarded: 0 The student scored 0% in this exam. ./percentage Enter the total number of marks in the exam: 3 Enter the number of marks the student was awarded: 2 The student scored 67% in this exam.
You can assume that the user supplies two integers. You do not have to check the return value from scanf.
You can assume that the user enters valid input. This means that the first integer will always be greater than or equal to the second integer (i.e. the student will never be awarded more marks than the exam was worth).
Hint: you can print out a percent symbol in printf using two percent
symbols, i.e. printf("100%%");
would print out
100%
.
Hint: you can change how many decimal points are printed out, e.g.
printf("%.2lf", 1.23456);
would print out
1.23
.
1511 style percentage.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest percentage
give cs1511 lab03_percentage percentage.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 24 June 17:00 to obtain the marks for this lab exercise.
We use loops in C to do things multiple times. Here is an example of a loop that prints all the numbers from 1 to 17 on a new line in ascending order:
//initialise counter to 1
int counter = 1;
// loop until not <= 17
while (counter <= 17) {
// print counter
printf("%d\n", counter);
// increment counter
counter = counter + 1;
}
In this exercise you will use a loop to print a countdown from 10 to
0. Start by creating a file called countdown.c
, and
copying the above code. Modify this code so that the loop counts
down from 10 until 0.
./countdown 10 9 8 7 6 5 4 3 2 1 0
1511 style countdown.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest countdown
give cs1511 lab03_countdown countdown.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 24 June 17:00 to obtain the marks for this lab exercise.
For example:
./three_five Enter number: 10 3 5 6 9 ./three_five Enter number: 30 3 5 6 9 10 12 15 18 20 21 24 25 27
1511 style three_five.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest three_five
give cs1511 lab03_three_five three_five.cNote, even though this is a pair exercise, you both must run give from your own account before Monday 24 June 17:00 to obtain the marks for this lab exercise.
Write a C program order3_challenge1.c
that reads 3 integers and prints them from smallest to largest.
You are not permitted to use if statements.
You are not permitted to use loops (e.g. while).
You are not permitted to call functions other than printf and scanf. For example, you are not permitted to use functions from the math library.
You are not permitted to use printf inside expressions - you can only use printf as a statement (the way it has been used in lectures).
You are not permitted to assign variables inside expressions - you can only assign variables as a statement (the way it has been done in lectures).
For example, both of these are invalid:
(a < b) && printf("a"); // invalid
(a < b) && (a = b); // invalid
You can use printf to print the value of an expression, in other words you can have an expression inside printf.
You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. For example, you are not permitted to use the ternary ?: operator. You are not permitted to define functions.
You should invent your own solution - don't just google or ask others how do it!
Your program should behave exactly like this example:
./order3_challenge1 Enter integer: 23 Enter integer: 5 Enter integer: 27 The integers in order are: 5 23 27 ./order3_challenge1 Enter integer: 3 Enter integer: 6 Enter integer: 27 The integers in order are: 3 6 27 ./order3_challenge1 Enter integer: 9 Enter integer: 8 Enter integer: 7 The integers in order are: 7 8 9This is more puzzle than a programming exercise.
Try to invent your own solution - don't google or ask others how do it.
Autotest is available to help you test your program - but it doesn't check that your code meets the above restrictions.
1511 style order3_challenge1.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest order3_challenge1
give cs1511 lab03_order3_challenge1 order3_challenge1.cYou must run give before Monday 24 June 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.
Write a C program order3_challenge2.c
that reads 3 integers and prints them from smallest to largest.
You are only permitted to have 3 variables in your program and they must be of type int.
The restrictions of the previous challenge exercise also apply.
You are not permitted to use if statements.
You are not permitted to use loops (e.g. while).
You are not permitted to call functions other than printf and scanf. For example, you are not permitted to use functions from the math library.
You are not permitted to use printf inside expressions - you can only use printf as a statement (the way it has been used in lectures).
You are not permitted to assign variables inside expressions - you can only assign variables as a statement (the way it has been done in lectures).
For example, both of these are invalid:
(a < b) && printf("a"); // invalid
(a < b) && (a = b); // invalid
You can use printf to print the value of an expression, in other words you can have an expression inside printf.
You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. For example, you are not permitted to use the ternary ?: operator. You are not permitted to define functions.
You should invent your own solution - don't just google or ask others how do it!
Your program should behave exactly like this example:
./order3_challenge2 Enter integer: 23 Enter integer: 5 Enter integer: 27 The integers in order are: 5 23 27 ./order3_challenge2 Enter integer: 3 Enter integer: 6 Enter integer: 27 The integers in order are: 3 6 27 ./order3_challenge2 Enter integer: 9 Enter integer: 8 Enter integer: 7 The integers in order are: 7 8 9This is much more puzzle than a programming exercise.
Try to invent your own solution - don't google or ask others how do it.
Autotest is available to help you test your program - but it doesn't check that your code meets the above restrictions.
1511 style order3_challenge2.c
When you think your program is working you can use
autotest
to run some simple automated tests:
1511 autotest order3_challenge2
give cs1511 lab03_order3_challenge2 order3_challenge2.cYou must run give before Monday 24 June 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 24 June 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.