Programming Fundamentals
Objectives
- using C input/output (I/O) facilities
- creating simple arithmetic expressions
- programming with
if
statements - creating relational expressions
Activities To Be Completed
The following is a list of all the activities available to complete this week...
Worth 1 mark(s) in total:
- addition
- negative
- debug_double
Worth 1 mark(s) in total:
- get_letter
- is_leap_year
- division
Worth 0.5 mark(s) in total:
- shift_inversion
- print_faces
Problem sets are capped at 15 marks (there are 4 possible bonus marks from the three-dot exercises that can bring you up to a total of 15 if you missed out on any other marks in the one- or two-dot exercises).
Completing just the one and two-dot exercises every week can give you the full 15 marks needed in this component.
For more details, see the course outline.
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
When attempting the following exercises, make sure to read the whole exercise, including any hints and assumptions that may make the exercise easier.
Exercise
(●◌◌)
:
Addition
Create a program called addition.c
, which will add together the number of students and tutors in a class.
This program should ask for two integers using the message
Please enter the number of students and tutors:
and then display the sum of the integers as
students + tutors = sum
Examples
dcc addition.c -o addition ./addition Please enter the number of students and tutors: 2 5 2 + 5 = 7 ./addition Please enter the number of students and tutors: 3 5 3 + 5 = 8 ./addition Please enter the number of students and tutors: -1 5 -1 + 5 = 4
Assumptions/Restrictions/Clarifications
- You can assume that the values entered will always be 2 whole numbers (i.e. integers)
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 must
submit your work by running give
:
give cs1511 lab02_addition addition.c
You must run give
before Monday 23 September 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.
addition.c
// addition solution
// addition.c
//
// This program was written by Andrew Taylor (z9300162)
// on 19-02-2019
//
// A program to calculate the sum of two integers.
#include <stdio.h>
int main(void) {
// Make two integers to store the scanned-in values.
int students;
int tutors;
// Get the two numbers from the user.
printf("Please enter the number of students and tutors: ");
scanf("%d %d", &students, &tutors);
// Calculate the sum of the two numbers, and store it in a variable.
int sum = students + tutors;
// Print out the sum of the two numbers.
printf("%d + %d = %d\n", students, tutors, sum);
return 0;
}
Exercise
(●◌◌)
:
Don't be so negative
Complete the program negative.c
so that it 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."
Examples
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.
Assumptions/Restrictions/Clarifications
- You can assume that the number will always be a whole number (i.e. an integer)
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 Monday 23 September 20:00
to obtain the marks for this lab exercise.
negative.c
// Don't be so negative Solution
// negative.c
//
// This program was written by Andrew Taylor (z9300162)
// on 16-02-2019
//
// 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!"
#include <stdio.h>
// Procedures for outputting user messages:
// Outputs: "You have entered a positive number.\n"
void print_positive_message(void) {
printf("You have entered a positive number.\n");
}
// Outputs: "Don't be so negative!\n"
void print_negative_message(void) {
printf("Don't be so negative!\n");
}
// Outputs: "You have entered zero.\n"
void print_zero_message(void) {
printf("You have entered zero.\n");
}
int main(void) {
int num;
// Read in a number.
scanf("%d", &num);
// Print the relevant message.
if (num > 0) {
print_positive_message();
} else if (num == 0) {
print_zero_message();
} else {
print_negative_message();
}
return 0;
}
Exercise
(●◌◌)
:
Debugging - Double
Download debug_double.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity debug_double
Debugging Tips!
Some debugging tips for you:
- dcc output - as you run into issues, dcc will point you to where the errors are. Remember that dcc gives you the line number the issue is on, and will give some sort of explanation. Make sure you read everything dcc gives you. Sometimes we get “errors carried forward”, so find your first error, fix that, then recompile.
- print statements - sometimes it can be handy to see if the flow of your code puts you in the spot you expect it to be (ie. inside the right if statement, or going through a loop the correct amount of times). A quick way you can check this is by putting print statements in your code for testing purposes, like
"the value of x is %d and y is %d"
. This lets you check that you got against what you expected. - COMP1511 debugging guide
The Task
This exercise reads a number from the user, doubles it, and prints it out. Currently it has some issues - it is your job to figure them out and fix the code.
Examples
dcc debug_double.c -o debug_double ./debug_double Enter a number: 2 Doubled: 2 x 2 = 4 ./debug_double Enter a number: -5 Doubled: -5 x 2 = -10 ./debug_double Enter a number: 0 Doubled: 0 x 2 = 0
1511 style debug_double.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest debug_double
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_debug_double debug_double.c
You must run give
before Monday 23 September 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.
debug_double.c
// Debugging - Double
// debug_double.c
//
// This program was written by Gab Steiner (z5258305)
// on 01/01/2023
//
// This is a C program that reads in a number.
// The number is doubled and printed out.
#include <stdio.h>
int main(void) {
// Read in the number:
int x;
printf("Enter a number: ");
scanf("%d", &x);
// Doubled the value of x and assign it to y.
int y;
y = x + x;
printf("Doubled: %d x 2 = %d\n", x, y);
return 0;
}
Exercise
(●●◌)
:
Print Letters, Given Their Numbers
Download get_letter.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity get_letter
Write a C program get_letter.c
that prints a letter from the
alphabet in either uppercase or lowercase, given its position.
First, scan in a character.
-
This character will determine whether the letter should be printed in uppercase or lowercase.
-
The character entered will be either
'y'
or'n'
, i.e. 'yes' or 'no'. -
If the character entered is
'y'
, then the letter should be printed in uppercase. -
If the character entered is
'n'
, then the letter should be printed in lowercase.
Second, scan in an integer.
- This integer is the 'index' that we will use to determine which letter of the alphabet to print.
- The integer entered will be a number between 0 and 25 (inclusive).
- This means that
- index 0 corresponds to
'A'
, - index 1 corresponds to
'B'
and - so on till we reach index 25 which corresponds to
'Z'
.
- index 0 corresponds to
Examples
dcc get_letter.c -o get_letter ./get_letter Uppercase: y Index: 4 The letter is E ./get_letter Uppercase: n Index: 17 The letter is r
Assumptions/Restrictions/Clarifications
- You may assume you will always be given a character first
- You may assume you will always be given a whole number second
- No error checking is required, i.e. The character entered will always be
'y'
or'n'
- The integer entered will always be between 0 and 25 (inclusive).
1511 style get_letter.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest get_letter
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_get_letter get_letter.c
You must run give
before Monday 23 September 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.
get_letter.c
// Print Letters, Given Their Numbers Solution
// get_letter.c
//
// This program was written by Clarissa Tatang (zXXXXXXX)
// on 14-09-2021
//
// Program which prints a letter depending on the user's request.
#include <stdio.h>
int main(void) {
char is_uppercase;
printf("Uppercase: ");
scanf("%c", &is_uppercase);
int index;
printf("Index: ");
scanf("%d", &index);
char letter;
if (is_uppercase == 'y') {
letter = 'A' + index;
} else {
letter = 'a' + index;
}
printf("The letter is %c\n", letter);
return 0;
}
Exercise
(●●◌)
:
Leap Year
Write a C program is_leap_year.c
that reads a year
and then
prints whether that year is a leap year.
A leap year is a year that is divisible by 4, except for years that are divisible by 100, unless they are also divisible by 400.
For example, 2000 is a leap year, but 1900 is not.
Examples
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.
Assumptions/Restrictions/Hints
- You may assume the tested year is not negative.
- You may assume the tested year is a whole number.
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 Monday 23 September 20:00
to obtain the marks for this lab exercise.
is_leap_year.c
// Leap Year Solution 1
// is_leap_year.c
//
// This program was written by Andrew Taylor (z9300162)
// on 03-03-2017
//
// 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
// Leap Year Solution 2
// is_leap_year.1.c
//
// This program was written by Finbar Berkon (zXXXXXXX)
// on 16-06-2019
//
// from https://en.wikipedia.org/wiki/Leap_year#Algorithm
// 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);
}
}
Exercise
(●●◌)
:
Division
Write a program division.c
, which divides two numbers scanned in and rounds the result to the nearest integer, 1 decimal place, 5 decimal places, and 20 decimal places.
In mathematics, a dividend is a number that is divided by another number (the divisor). For example, in the following equation, $$ 10 \div 5 = 2 $$
has 10 as the dividend and 5 as the divisor.
Examples
dcc division.c -o division ./division Enter the dividend: 6 Enter the divisor: 2 6 divided by 2 To the nearest integer: 3 To 1 decimal place: 3.0 To 5 decimal places: 3.00000 To 20 decimal places: 3.00000000000000000000 ./division Enter the dividend: 1 Enter the divisor: 3 1 divided by 3 To the nearest integer: 0 To 1 decimal place: 0.3 To 5 decimal places: 0.33333 To 20 decimal places: 0.33333333333333331483 ./division Enter the dividend: 10 Enter the divisor: 0 Don't be silly!
Assumptions/Restrictions/Clarifications
- You may assume you will be given 2 integers.
- If the divisor is 0, print "Don't be silly!" and return from main.
- When rounding to the nearest integer, the value should be rounded down.
1511 style division.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest division
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_division division.c
You must run give
before Monday 23 September 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.
division.c
// Division Solution
// division.c
//
// This program was written by Ibrahim Ghoneim (z5470570)
// on 30/01/2024
//
// A program to divide two numbers and prints the formatted result in terminal.
#include <stdio.h>
int main(void) {
printf("Enter the dividend: ");
double dividend;
scanf("%lf", ÷nd);
printf("Enter the divisor: ");
double divisor;
scanf("%lf", &divisor);
if (divisor == 0) {
printf("Don't be silly!\n");
return 0;
}
printf("%.0lf divided by %.0lf\n", dividend, divisor);
double result = dividend / divisor;
printf("To the nearest integer: %0.lf\n", result);
printf("To 1 decimal place: %0.1lf\n", result);
printf("To 5 decimal places: %0.5lf\n", result);
printf("To 20 decimal places: %0.20lf\n", result);
return 0;
}
Exercise
(●●●)
:
Shift Inversion
Write a program shift_inversion.c
which takes a character as input from the user,
along with preferences for case inversion and character shifting,
and prints the modified character based on these preferences.
Examples
dcc shift_inversion.c -o shift_inversion ./shift_inversion Please enter a character: z Would you like to invert the case? y or n: n By how much would you like to shift the character? 3 The character is c! ./shift_inversion Please enter a character: a Would you like to invert the case? y or n: y By how much would you like to shift the character? 5 The character is F! ./shift_inversion Please enter a character: K Would you like to invert the case? y or n: n By how much would you like to shift the character? 12 The character is W! ./shift_inversion Please enter a character: x Would you like to invert the case? y or n: y By how much would you like to shift the character? 10 The character is H!
Assumptions/Restrictions/Clarifications
- You may assume that either 'y' or 'n' will always be entered as input for deciding to invert the case.
- You may assume a valid alphabetical character (A-Z a-z) will be entered as input for the character.
- You may assume that the shift will always be positive
1511 style shift_inversion.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest shift_inversion
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_shift_inversion shift_inversion.c
You must run give
before Monday 23 September 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.
shift_inversion.c
// Shift Inversion Solution
// shift_inversion.c
//
// This program was written by Sofia De Bellis (z5418801)
// on 01/02/2024
//
// This program takes a character as input from the user,
// along with preferences for case inversion and character shifting,
// and prints the modified character based on these preferences.
#include <stdio.h>
#define ALPHABET_SIZE 26
int main(void) {
char character;
printf("Please enter a character: ");
scanf("%c", &character);
char convert;
printf("Would you like to invert the case? y or n: ");
scanf(" %c", &convert);
int shift;
printf("By how much would you like to shift the character? ");
scanf("%d", &shift);
if (convert == 'y') {
// shift and convert case
if (character >= 'A' && character <= 'Z') {
// wrap for uppercase input
character = 'a' + (character - 'A' + shift) % ALPHABET_SIZE;
} else if (character >= 'a' && character <= 'z') {
// wrap for lowercase input
character = 'A' + (character - 'a' + shift) % ALPHABET_SIZE;
}
} else {
// Shift without inverting case
if (character >= 'A' && character <= 'Z') {
// Wrap around for uppercase
character = 'A' + (character - 'A' + shift) % ALPHABET_SIZE;
} else if (character >= 'a' && character <= 'z') {
// wrap for lowercase
character = 'a' + (character - 'a' + shift) % ALPHABET_SIZE;
}
}
printf("The character is %c!\n", character);
return 0;
}
Exercise
(●●●)
:
Custom Printing Faces
Write a program print_faces.c
that creates a face based on user input.
You will prompt the user to input some characters. These characters correspond to different facial features. These features are:
Character | Group | Feature |
---|---|---|
|
eyes | _ O |
|
eyes | ~ O |
|
eyes | \ O |
|
eyes | / O |
|
nose | ^ |
|
nose | ~ |
|
mouth | \_/ |
|
mouth | /-\ |
|
mouth | o |
The user will put in the eye preference, then nose, then mouth.
If the user wants the eyes to be different, they can enter two letters for the eyes, with the first letter corresponding to the first eye, and the second letter being the second eye. This means the user can enter either 3 or 4 letters.
Examples
dcc print_faces.c -o print_faces ./print_faces How do you want to build a face?: afg _ _ O O ~ \_/ ./print_faces How do you want to build a face?: ceh \ \ O O ^ /-\ ./print_faces How do you want to build a face?: cdei \ / O O ^ o ./print_faces How do you want to build a face?: abeg _ ~ O O ^ \_/ ./print_faces How do you want to build a face?: dbfi / ~ O O ~ o ./print_faces How do you want to build a face?: bbei ~ ~ O O ^ o
Assumptions/Restrictions/Clarifications
- You should only use content taught in the course up to (and including) week 2.
- You can assume you will only be given letters between
'a'
and'i'
inclusive. - You can assume that the characters are given in the correct order (e.g. eyes then nose then mouth)
- You can assume you will only be given 3 or 4 letters, no more, no less.
- You can assume you will only get one letter corresponding to the nose, and one letter corresponding to the mouth.
1511 style print_faces.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest print_faces
When you are finished working on this exercise,
you must
submit your work by running give
:
give cs1511 lab02_print_faces print_faces.c
You must run give
before Monday 23 September 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.
print_faces.c
// print_faces.c
// A program to custom make and print faces based on user input.
// Written by Gab Steiner z525805 May 2023
#include <stdio.h>
#define FLAT_EYEBROW 'a'
#define SQUIGGLE_EYEBROW 'b'
#define LEFT_ANGRY_EYE 'c'
#define RIGHT_ANGRY_EYE 'd'
#define UP_NOSE 'e'
#define SQUIGGLE_NOSE 'f'
#define SMILE_MOUTH 'g'
#define GRUMPY_MOUTH 'h'
#define SURPRISE_MOUTH 'i'
int main(void) {
printf("How do you want to build a face?: ");
// Get the first eye
char first_eye;
scanf("%c", &first_eye);
char left_eyebrow = '?';
if (first_eye == FLAT_EYEBROW) {
left_eyebrow = '_';
} else if (first_eye == SQUIGGLE_EYEBROW) {
left_eyebrow = '~';
} else if (first_eye == LEFT_ANGRY_EYE) {
left_eyebrow = '\\';
} else if (first_eye == RIGHT_ANGRY_EYE) {
left_eyebrow = '/';
}
// Determine if the second letter is an eye or a nose
char eye_nose = '?';
scanf("%c", &eye_nose);
char right_eyebrow = '?';
// If we were given a different eye type
if (eye_nose >= FLAT_EYEBROW && eye_nose <= RIGHT_ANGRY_EYE) {
if (eye_nose == FLAT_EYEBROW) {
right_eyebrow = '_';
} else if (eye_nose == SQUIGGLE_EYEBROW) {
right_eyebrow = '~';
} else if (eye_nose == LEFT_ANGRY_EYE) {
right_eyebrow = '\\';
} else if (eye_nose == RIGHT_ANGRY_EYE) {
right_eyebrow = '/';
}
// If we were given a nose, make the eyes the same
} else {
right_eyebrow = left_eyebrow;
}
// Print the eyes out
printf("%c %c\n", left_eyebrow, right_eyebrow);
printf("O O\n");
// Nose
char nose;
// If we got a nose, don't scan for another nose.
if (eye_nose <= RIGHT_ANGRY_EYE) {
scanf("%c", &nose);
} else {
// copy across the nose we have already scanned.
nose = eye_nose;
}
// Print the correct nose
if (nose == UP_NOSE) {
printf(" ^ \n");
} else if (nose == SQUIGGLE_NOSE) {
printf(" ~ \n");
}
// Mouth
char mouth;
scanf("%c", &mouth);
// Find the correct mouth and print it out!
if (mouth == SMILE_MOUTH) {
printf("\\_/\n");
} else if (mouth == GRUMPY_MOUTH) {
printf("/-\\\n");
} else if (mouth == SURPRISE_MOUTH) {
printf(" o \n");
}
return 0;
}
Exercise — individual:
(Not For Marks) Debugging - Smallest Prime Factor
Download debug_smallest_prime.c here
Or, copy these file(s) to your CSE account using the following command:
1511 fetch-activity debug_smallest_prime
Note that this exercise is not marked or worth marks!
Debugging Tips!
Some debugging tips for you:
- dcc output - as you run into issues, dcc will point you to where the errors are. Remember that dcc gives you the line number the issue is on, and will give some sort of explanation. Make sure you read everything dcc gives you. Sometimes we get “errors carried forward”, so find your first error, fix that, then recompile.
- print statements - sometimes it can be handy to see if the flow of your code puts you in the spot you expect it to be (ie. inside the right if statement, or going through a loop the correct amount of times). A quick way you can check this is by putting print statements in your code for testing purposes, like
"the value of x is %d and y is %d"
. This lets you check that you got against what you expected. - COMP1511 debugging guide
The Task
This exercise takes a number greater than 1 and determines if 2, 3, or 5, is its smallest prime factor. If it has a different smallest prime factor, like 7, then the program lets you know that that factor is higher than 5.
You can assume the program is never given any number smaller than 2.
Examples
dcc debug_smallest_prime.c -o debug_smallest_prime ./debug_smallest_prime Enter a number: 6 The lowest prime factor is 2. ./debug_smallest_prime Enter a number: 15 The lowest prime factor is 3. ./debug_smallest_prime Enter a number: 55 The lowest prime factor is 5.
Assumptions/Restrictions/Clarifications
- You can assume that the values entered will always be a whole number greater than 1.
- You can assume you will not be given negative numbers
Walkthrough
Below is a video walkthrough of this exercise! Make sure to attempt it before watching this video
1511 style debug_smallest_prime.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest debug_smallest_prime
debug_smallest_prime.c
// debug_smallest_prime.c SOLUTION
//
// This is a C program that determines if the smallest prime factor of a
// positive number is 2, 3, or 5.
//
// Otherwise, the program tells you that it's lowest prime factor is higher
// then 5
//
// Gab Steiner, 2023
#include <stdio.h>
int main(void) {
// Scan the number.
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Find it's lowest factor.
if (number % 2 == 0) {
printf("The lowest prime factor is 2.\n");
} else if (number % 3 == 0) {
printf("The lowest prime factor is 3.\n");
} else if (number % 5 == 0) {
printf("The lowest prime factor is 5.\n");
} else {
printf("The lowest prime factor of %d is higher than 5\n", number);
}
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 3 Monday 20:00 to submit your work.
You cannot obtain marks by e-mailing your code 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,
using test cases different to those autotest
runs for you.
(Hint: do your own testing as well as running autotest
.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.