COMP1511 18s1 (webcms)
COMP1511 18s1 (flask)

Objectives

  • input and output of numerical values
  • implementing simple numerical calculations
  • using complex if statements to control program execution
  • creating simple functions

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples. You should also have read the lab assessment guidelines.

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.

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

1511 autotest order3
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk03_order3 order3.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 19 March 23:59:59 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.

Match the examples below exactly

Hint: you only need use the int type, modulus (%) and if statement(s).

Hint: if you are clever with && and || it just needs one if statement.

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.

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

1511 autotest is_leap_year
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk03_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 19 March 23:59:59 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 leap_year_function.c that reads a year and then uses a function to calculate whether that year is a leap year. The function prototype must be int isLeapYear(int year).

Your function should return 0 if it is not a leap year, and 1 if it is a leap year.

Your leap year function must not print anything.

You should call this function from your main function, which is where you print the result.

Your function must be named isLeapYear.
It must exactly match the function prototype given above.

Match the examples below exactly

Hint: copy the logic from your is_leap_year.c and put it into a function

For example:

dcc -o leap_year_function leap_year_function.c
./leap_year_function
Enter year: 2017
2017 is not a leap year.
./leap_year_function
Enter year: 2016
2016 is a leap year.
./leap_year_function
Enter year: 2000
2000 is a leap year.
./leap_year_function
Enter year: 3000
3000 is not a leap year.

Beware: autotest will not use your main function. It will call your isLeapYear function here.

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

1511 autotest leap_year_function
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk03_leap_year_function leap_year_function.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 19 March 23:59:59 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.

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.

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.

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

1511 autotest percentage
When you are finished on this exercise you and your lab partner must both submit your work by running give:
give cs1511 wk03_percentage percentage.c
Note, even though this is a pair exercise, you both must run give from your own account before Monday 19 March 23:59:59 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.

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

1511 autotest order3_challenge1
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk03_order3_challenge1 order3_challenge1.c
You must run give before Monday 19 March 23:59:59 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.

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

1511 autotest order3_challenge2
When you are finished working on this exercise you must submit your work by running give:
give cs1511 wk03_order3_challenge2 order3_challenge2.c
You must run give before Monday 19 March 23:59:59 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 19 March 23:59:59 to submit your work.

Automarking will be run several days after the submission deadline for the test. When complete you can view automarking here and you can view the the resulting mark via give's web interface

You can read more about lab assessment here