Week 02 Laboratory Sample Solutions
Objectives
- using C input/output (I/O) facilities
- creating simple arithmetic expressions
- programming with
if
statements - creating relational expressions
- displaying varying strings
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
Exercise — in pairs:
Don't Be So Negative!
Now create and open a new file called negative.c
for this exercise.
gedit negative.c &
Write a program that uses scanf
to get a number from a user and prints
"Don't be so negative!" if they entered a negative number.
If the number is positive, the program should print "You have entered a positive number."
If the user enters the number 0, the program should print
"You have entered zero."
Note: you can assume that the number will always be a whole number (i.e. an integer)
Your program should behave as follows:
dcc -o negative negative.c ./negative 3 You have entered a positive number. ./negative -3 Don't be so negative! ./negative 0 You have entered zero.
1511 style negative.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest negative
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_negative negative.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Friday 01 March 20:00
to obtain the marks for this lab exercise.
negative.c
// Read in a number, and determine whether it is negative.
// If the number is positive, print "You have entered a positive number"
// If the number is a zero, print "You have entered zero".
// If the number is negative, print "Don't be so negative!"
// Sample solution.
#include <stdio.h>
int main(void) {
int num;
// Read in a number.
scanf("%d", &num);
// Print the relevant message.
if (num > 0) {
printf("You have entered a positive number.\n");
} else if (num == 0) {
printf("You have entered zero.\n");
} else {
printf("Don't be so negative!\n");
}
return 0;
}
Exercise — in pairs:
Addition
Create a program called addition.c
.
This program should ask for two integers
using the message
Please enter two integers:
and then display the sum of the integers as
a + b = sum
.
Make sure to replace the a and b with the numbers entered in the same order and the sum with the sum of the two numbers.
Some Examples
./addition Please enter two integers: 2 5 2 + 5 = 7
./addition Please enter two integers: 3 5 3 + 5 = 8
./addition Please enter two integers: -1 5 -1 + 5 = 4
1511 style addition.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest addition
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_addition addition.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Friday 01 March 20:00
to obtain the marks for this lab exercise.
addition.c
// A program to calculate the sum of two integers.
// Sample solution.
#include <stdio.h>
int main(void) {
// Make two integers to store the scanned-in values.
int x, y;
// Get the two numbers from the user.
printf("Please enter two integers: ");
scanf("%d %d", &x, &y);
// Calculate the sum of the two numbers, and store it in a variable.
int sum = x + y;
// Print out the sum of the two numbers.
printf("%d + %d = %d\n", x, y, sum);
return 0;
}
Exercise — in pairs:
Numbers to Words
Make a program called numberWords.c
.
This program will ask for a number
with the message
Please enter an integer:
.
For numbers between 1 and 5,
display the number as a word in the message
You entered number
.
For numbers less than 1,
display the message
You entered a number less than one.
For numbers greater than 5,
display the message
You entered a number greater than five.
Some Examples
./numberWords Please enter an integer: 2 You entered two.
./numberWords Please enter an integer: 5 You entered five.
./numberWords Please enter an integer: 0 You entered a number less than one.
./numberWords Please enter an integer: 1000 You entered a number greater than five.
1511 style numberWords.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest numberWords
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_numberWords numberWords.c
Note, even though this is a pair exercise,
you both must run give
from your own account
before Friday 01 March 20:00
to obtain the marks for this lab exercise.
numberWords.c
// Print out the written form of a given number between 1 and 5.
// A sample solution.
#include <stdio.h>
int main(void) {
int num;
// Read in the number.
printf("Please enter an integer: ");
scanf("%d", &num);
// Start by printing "You entered ", since all of the variations
// start with that.
printf("You entered ");
// Print out the appropriate number or message.
if (num < 1) {
printf("a number less than one");
} else if (num == 1) {
printf("one");
} else if (num == 2) {
printf("two");
} else if (num == 3) {
printf("three");
} else if (num == 4) {
printf("four");
} else if (num == 5) {
printf("five");
} else {
printf("a number greater than five");
}
// Print the full stop at the end.
printf(".\n");
return 0;
}
Exercise — in pairs:
Leap Year Calculator
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
When you are finished working on this exercise,
you and your lab partner must both
submit your work by running give
:
give cs1511 lab02_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 Friday 01 March 20:00
to obtain the marks for this lab exercise.
is_leap_year.c
// Modified 3/3/2017 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511
// Test if a year is leap year
// https://en.wikipedia.org/wiki/Leap_year
#include <stdio.h>
int main(void) {
int year;
printf("Enter year: ");
if (scanf("%d", &year) != 1) {
return 1;
}
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
is_leap_year.c
// Modified by Finbar Berkon (finbar.berkon@unsw.edu.au)
// from https://en.wikipedia.org/wiki/Leap_year#Algorithm
// on 16/6/2019
// Test if a year is a leap year with a simple series of if statements
#include <stdio.h>
int main(void) {
int year;
printf("Enter year: ");
if (scanf("%d", &year) != 1) {
return 1;
}
if (year % 4 != 0) {
printf("%d is not a leap year.\n", year);
} else if (year % 100 != 0) {
printf("%d is a leap year.\n", year);
} else if (year % 400 != 0) {
printf("%d is not a leap year.\n", year);
} else {
printf("%d is a leap year.\n", year);
}
}
Challenge Exercise — individual:
Dice Range
Write a C program dice_range.c
that reads the number of sides on a set of dice and how many of them are being rolled.
It then outputs the range of possible totals that these dice can produce as well as the average value.
Hint: use the examples below to clarify the expected behaviour of your program.
For example:
dcc -o dice_range dice_range.c ./dice_range Enter the number of sides on your dice: 6 Enter the number of dice being rolled: 2 Your dice range is 2 to 12. The average value is 7.000000 ./dice_range Enter the number of sides on your dice: 8 Enter the number of dice being rolled: 3 Your dice range is 3 to 24. The average value is 13.500000 ./dice_range Enter the number of sides on your dice: 20 Enter the number of dice being rolled: 4 Your dice range is 4 to 80. The average value is 42.000000You'll also need to check for invalid dice or situations where the range is empty. Those situations should produce these results:
./dice_range Enter the number of sides on your dice: -5 Enter the number of dice being rolled: 4 These dice will not produce a range. ./dice_range Enter the number of sides on your dice: 6 Enter the number of dice being rolled: 0 These dice will not produce a range.
1511 style dice_range.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest dice_range
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_dice_range dice_range.c
You must run give
before Friday 01 March 20:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
dice_range.c
// Show the dice range of a set of dice as well
// as their average roll
// Created 7/6/2019 by Marc Chee (marc.chee@unsw.edu.au)
// as a lab challenge
// Note this program doesn't check whether
// the scanf call successfully reads a number.
// This is good practice but was not requested in the lab exercise
// because this isn't covered until later in the course.
#include <stdio.h>
int main(void) {
int diceSize;
int numDice;
int lowest;
int highest;
double average;
// scan in dice stats
printf("Enter the number of sides on your dice: ");
scanf("%d", &diceSize);
printf("Enter the number of dice being rolled: ");
scanf("%d", &numDice);
// calculate lowest and highest values
lowest = numDice;
highest = numDice * diceSize;
// calculate average. The 2.0 makes the result a double
average = (lowest + highest) / 2.0;
if (diceSize <= 0 || highest <= 0) {
printf("These dice will not produce a range.\n");
} else {
printf("Your dice range is %d to %d.\n", lowest, highest);
printf("The average value is %lf\n", average);
}
return 0;
}
Challenge Exercise — individual:
Word Addition
Create a program called wordAddition.c
.
For this challenge exercise, you can only use the topics that we've covered in the course thus far (if/elseif/else, printf, scanf, ints).
This program should ask for two integers
using the message
Please enter two integers:
and then display the sum of the integers as
n + n = sum
.
Any numbers that are between zero and ten should appear as words. This also applies to negative numbers between negative ten and zero. All other numbers should appear as decimal integers.
Make sure to replace the n with the numbers entered in the same order and the sum with the sum of the two numbers.
Some Examples
./wordAddition Please enter two integers: 2 5 two + five = seven
./wordAddition Please enter two integers: 3 0 three + zero = three
./wordAddition Please enter two integers: -1 5 negative one + five = four
./wordAddition Please enter two integers: 10 5 ten + five = 15
1511 style wordAddition.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest wordAddition
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_wordAddition wordAddition.c
You must run give
before Friday 01 March 20:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
wordAddition.c
// Word Addition -- a sample solution.
// Add two numbers and print them out, as words.
#include <stdio.h>
int main(void) {
int num1, num2;
printf("Please enter two integers: ");
scanf("%d %d", &num1, &num2);
int sum = num1 + num2;
// First, deal with num1.
// Is it between -10 and 10?
if (num1 >= -10 && num1 <= 10) {
// Is it negative?
if (num1 < 0) {
printf("negative ");
num1 *= -1;
}
// Print out whatever the digit is.
if (num1 == 0) {
printf("zero");
}
if (num1 == 1) {
printf("one");
}
if (num1 == 2) {
printf("two");
}
if (num1 == 3) {
printf("three");
}
if (num1 == 4) {
printf("four");
}
if (num1 == 5) {
printf("five");
}
if (num1 == 6) {
printf("six");
}
if (num1 == 7) {
printf("seven");
}
if (num1 == 8) {
printf("eight");
}
if (num1 == 9) {
printf("nine");
}
if (num1 == 10) {
printf("ten");
}
} else {
// It wasn't between -10 and 10, so print out the number as is.
printf("%d", num1);
}
// Now, print out the plus sign...
printf(" + ");
// Now, deal with num2
// Is it between -10 and 10?
if (num2 >= -10 && num2 <= 10) {
// Is it negative?
if (num2 < 0) {
printf("negative ");
num2 *= -1;
}
// Print out whatever the digit is
if (num2 == 0) {
printf("zero");
}
if (num2 == 1) {
printf("one");
}
if (num2 == 2) {
printf("two");
}
if (num2 == 3) {
printf("three");
}
if (num2 == 4) {
printf("four");
}
if (num2 == 5) {
printf("five");
}
if (num2 == 6) {
printf("six");
}
if (num2 == 7) {
printf("seven");
}
if (num2 == 8) {
printf("eight");
}
if (num2 == 9) {
printf("nine");
}
if (num2 == 10) {
printf("ten");
}
} else {
// It wasn't between -10 and 10, so print out the number as is.
printf("%d", num2);
}
// Now, print the equals sign.
printf(" = ");
// Now, deal with the sum
// Is it between -10 and 10?
if (sum >= -10 && sum <= 10) {
// Is it negative?
if (sum < 0) {
printf("negative ");
sum *= -1;
}
// Print out whatever the digit is.
if (sum == 0) {
printf("zero\n");
}
if (sum == 1) {
printf("one\n");
}
if (sum == 2) {
printf("two\n");
}
if (sum == 3) {
printf("three\n");
}
if (sum == 4) {
printf("four\n");
}
if (sum == 5) {
printf("five\n");
}
if (sum == 6) {
printf("six\n");
}
if (sum == 7) {
printf("seven\n");
}
if (sum == 8) {
printf("eight\n");
}
if (sum == 9) {
printf("nine\n");
}
if (sum == 10) {
printf("ten\n");
}
} else {
// It wasn't between -10 and 10, so print out the number as is.
printf("%d\n", sum);
}
return 0;
}
Challenge Exercise — individual:
Easter
easter.c
which allows the user to enter a year,
then calculates the date of Easter Sunday for that year.
Use the formula developed in 1876 by
Samuel Butcher, Bishop of Meath,.
Follow the output format in in the example below exactly:
dcc easter.c -o easter ./easter Enter year: 2017 Easter is April 16 in 2017. ./easter Enter year: 2018 Easter is April 1 in 2018. ./easter Enter year: 2019 Easter is April 21 in 2019.
Hints
Cut-and-paste the formula from the above Web site and then fill in the C program around it.Make sure every variable is declared.
Make sure every statement ends with a semicolon.
Note that the original proposal of this formula had only single letter variable names, and no explanation of how it works.
Because of this, even though we know that it works, no-one knows how it works.
Make sure to always comment your code and have sensible variable names so that people can understand how your code works!
1511 style easter.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest easter
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_easter easter.c
You must run give
before Friday 01 March 20:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give
must be entirely your own.
easter.c
// Compute Easter Sunday date
// Modified 3/3/2019 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511
// code for Butcher's Formula copied from
// http://smart.net/~mmontes/nature1876.html
// Note this program doesn't check whether
// the scanf call successfully reads a number.
// This is good practice but was not requested in the lab exercise
// because this isn't covered until later in COMP1511.
#include <stdio.h>
int main(void) {
int a, b, c, d, e, f, g, h, i, k, l, m, p;
int year, month, date;
printf("Enter year: ");
scanf("%d", &year);
a = year % 19;
b = year / 100;
c = year % 100;
d = b / 4;
e = b % 4;
f = (b + 8) / 25;
g = (b - f + 1) / 3;
h = (19 * a + b - d - g + 15) % 30;
i = c / 4;
k = c % 4;
l = (32 + 2 * e + 2 * i - h - k) % 7;
m = (a + 11 * h + 22 * l) / 451;
month = (h + l - 7 * m + 114) / 31; // [3=March, 4=April]
p = (h + l - 7 * m + 114) % 31;
date = p + 1; // (date in Easter Month)
printf("Easter is ");
if (month == 3) {
printf("March");
} else {
printf("April");
}
printf(" %d in %d.\n", date, year);
return 0;
}
Submission
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 Week 2 Sunday 20: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