COMP1511 19T2
COMP1511 19T2

Objectives

  • input and output of numerical values
  • implementing simple numerical calculations
  • using complex if statements to control program execution
  • using while loops for repitition

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 lab03 by typing:
mkdir lab03
Change to this directory by typing:
cd lab03

Exercise: Ordering Three Integers (pair)

This is a pair exercise to complete with your lab partner.

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 9
You can assume the user supplies 3 integers. You do not have to check the return value from scanf.
New! You can run an automated code style checker using the following command:
1511 style order3.c

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

1511 autotest order3

Autotest Results

97% of 513 students who have autotested order3.c so far, passed all autotest tests.
  • 99% passed test order3_0
  • 98% passed test order3_1
  • 98% passed test order3_2
  • 98% passed test order3_3
  • 98% passed test order3_4
  • 99% passed test order3_5
  • 98% passed test order3_6
  • 98% passed test order3_7
  • 98% passed test order3_8
  • 98% passed test order3_9
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab03_order3 order3.c
Note, 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.

Exercise: Is this a leap year? (pair)

This is a pair exercise to complete with your lab partner.

Write a C program is_leap_year.c that reads a year and then prints whether that year is a leap year.

Make sure you click on the link above to learn how to determine leap years!

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.
New! You can run an automated code style checker using the following command:
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

Autotest Results

98% of 495 students who have autotested is_leap_year.c so far, passed all autotest tests.
  • 99% passed test is_leap_year_0
  • 99% passed test is_leap_year_1
  • 99% passed test is_leap_year_2
  • 99% passed test is_leap_year_3
  • 99% passed test is_leap_year_4
  • 99% passed test is_leap_year_5
  • 99% passed test is_leap_year_6
  • 99% passed test is_leap_year_7
  • 99% passed test is_leap_year_8
  • 99% passed test is_leap_year_9
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab03_is_leap_year is_leap_year.c
Note, 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.

Exercise: Calculating Exam Marks (pair)

This is a pair exercise to complete with your lab partner.

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.

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

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

1511 autotest percentage

Autotest Results

97% of 492 students who have autotested percentage.c so far, passed all autotest tests.
  • 98% passed test percentage_0
  • 98% passed test percentage_1
  • 98% passed test percentage_2
  • 98% passed test percentage_3
  • 98% passed test percentage_4
  • 98% passed test percentage_5
  • 97% passed test percentage_6
  • 98% passed test percentage_7
  • 97% passed test percentage_8
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab03_percentage percentage.c
Note, 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.

Exercise: Countdown (pair)

This is a pair exercise to complete with your lab partner.

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.

Example

./countdown
10
9
8
7
6
5
4
3
2
1
0
New! You can run an automated code style checker using the following command:
1511 style countdown.c

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

1511 autotest countdown

Autotest Results

100% of 490 students who have autotested countdown.c so far, passed the autotest test.
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab03_countdown countdown.c
Note, 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.

Exercise: Three or Five (pair)

This is a pair exercise to complete with your lab partner.
Write a program that three_five.c that reads a positive integer n and print all the positive integers < n divisible by 3 or 5.

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
New! You can run an automated code style checker using the following command:
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

Autotest Results

98% of 495 students who have autotested three_five.c so far, passed all autotest tests.
  • 99% passed test 0
  • 98% passed test 1
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 lab03_three_five three_five.c
Note, 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.

Challenge Exercise: Ordering Three Integers Without If Statements (individual)

This is an individual exercise to complete by yourself.

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 9
This 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.

New! You can run an automated code style checker using the following command:
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

Autotest Results

83% of 211 students who have autotested order3_challenge1.c so far, passed all autotest tests.
  • 92% passed test order3_0
  • 91% passed test order3_1
  • 91% passed test order3_2
  • 91% passed test order3_3
  • 91% passed test order3_4
  • 91% passed test order3_5
  • 85% passed test order3_6
  • 84% passed test order3_7
  • 85% passed test order3_8
  • 84% passed test order3_9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab03_order3_challenge1 order3_challenge1.c
You 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.

Challenge Exercise: Ordering Three Integers Without If Statements and With Only 3 Variables (individual)

This is an individual exercise to complete by yourself.

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 9
This 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.

New! You can run an automated code style checker using the following command:
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

Autotest Results

90% of 151 students who have autotested order3_challenge2.c so far, passed all autotest tests.
  • 96% passed test order3_0
  • 95% passed test order3_1
  • 95% passed test order3_2
  • 96% passed test order3_3
  • 95% passed test order3_4
  • 94% passed test order3_5
  • 93% passed test order3_6
  • 92% passed test order3_7
  • 92% passed test order3_8
  • 93% passed test order3_9
When you are finished working on this exercise you must submit your work by running give:
give cs1511 lab03_order3_challenge2 order3_challenge2.c
You 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.

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 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

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.