COMP1521 22T1 - Week 01 Laboratory Sample Solutions
COMP1521 22T1 - Week 01 Laboratory Sample Solutions
Week 01
Laboratory
Sample Solutions
Objectives
to refamiliarise yourself with C
to practice interacting with files and standard I/O
to practice interacting with command-line arguments
to explore implementing recursive functions
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Getting Started
Set up for the lab by
creating a new directory called lab01 and changing to this directory.
$ mkdir lab01
$ cd lab01
There are some provided files for this lab which you can fetch with this command:
$ 1521 fetch lab01
exercise — individual:
Hello, World!
Hll, Wrld!
Ctrl-D
$ ./no_vowels
ndrw s th LC f CMP1521
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 1/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
HINT:
$ man 3 scanf
$ man 3 printf
NOTE:
You can assume a vowel is one of the ASCII values 'a', 'e', 'i', 'o', 'u' and their uppercase equivalents 'A', 'E', 'I', 'O', 'U'.
You should not make any assumptions on the number of characters supplied to your program.
#include <string.h>
int main(void) {
char c;
if (strchr("aAeEiIoOuU", c) == NULL) {
printf("%c", c);
return 0;
exercise — individual:
ABC
abc
ABCabc123
abcabc123
123!@#
123!@#
Hello, World!
hello, world!
Ctrl-D
HINT:
$ man 3 getchar
$ man 3 putchar
$ man 3 tolower
$ man 7 ascii
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 3/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
#include <stdio.h>
#include <ctype.h>
int main(void) {
int c;
// getchar will return the special value EOF if it can not read a byte
// Otherwise, it returns c.
putchar(tolower(c));
return 0;
exercise — individual:
Hello, World
Hello, World!
Hello, World!
Ctrl-D
$ echo "A peck of pickled peppers Peter Piper picked." >> input
$ echo "If Peter Piper picked a peck of pickled peppers," >> input
$ echo "Where's the peck of pickled peppers Peter Piper picked?" >> input
HINT:
$ man 3 fgets
$ man 3 fputs
$ man 3 strlen
NOTE:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 4/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
#include <string.h>
int main(void) {
if (!(strlen(line) % 2)) {
fputs(line, stdout);
return 0;
exercise — individual:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 5/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
$ ./my_args
Argument 1 is "hello"
Argument 2 is "world"
Argument 2 is "1"
Argument 3 is "2"
Argument 4 is "3"
Argument 5 is "4"
Argument 6 is "5"
$
if (argc == 1) {
} else {
return 0;
exercise — individual:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 6/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
$ ./arg_stats
$ ./arg_stats 1
MIN: 1
MAX: 1
SUM: 1
PROD: 1
MEAN: 1
$ ./arg_stats 1 2 3 4 5 6 7 8 9
MIN: 1
MAX: 9
SUM: 45
PROD: 362880
MEAN: 5
$ ./arg_stats 9 8 7 6 1 2 3 5 4
MIN: 1
MAX: 9
SUM: 45
PROD: 362880
MEAN: 5
$ ./arg_stats 1 9 1 9 1 9
MIN: 1
MAX: 9
SUM: 30
PROD: 729
MEAN: 5
$
HINT:
$ man 3 atoi
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 7/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
#include <stdio.h>
#include <stdlib.h>
if (argc == 1) {
exit(EXIT_FAILURE);
int sum = 0;
int prod = 1;
sum += atoi(argv[i]);
prod *= atoi(argv[i]);
return 0;
exercise — individual:
If the current number is ODD, then multiply by 3 and add 1 to get the next number
If the current number is EVEN, then divide by 2 to get the next number
NOTE:
You must implement collatz.c using a recursive function.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 8/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
$ ./collatz
$ ./collatz 1
$ ./collatz 12
12
10
16
$ ./collatz 10
10
16
#include <stdlib.h>
void collatz(int);
if (argc != 2) {
return EXIT_FAILURE;
collatz(atoi(argv[1]));
return EXIT_SUCCESS;
void collatz(int n) {
printf("%d\n", n);
if (n == 1) {
return;
} else if (n % 2 == 1) {
collatz((3 * n) + 1);
} else {
collatz(n/2);
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 9/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
exercise — individual:
Add the preceding two fibonacci number together to get the current fibonacci number:
NOTE:
You must implement fibonacci.c using a recursive function.
fibonacci.c should continue reading input and outputing ansers until it recived EOF.
fibonacci.c doesn't need to calculate any values larger than the 30th fibonacci number.
34
Ctrl-D
NOTE:
You can assume the largest integer entered will be 30.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 10/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
#include <stdio.h>
#include <stdlib.h>
#define SERIES_MAX 30
int main(void)
int n;
return EXIT_SUCCESS;
int fib(int n)
if (n == 1 || n == 2) return 1;
SOLUTION:
Alternative solution for fibonacci.c
#include <stdio.h>
#include <stdlib.h>
#define SERIES_MAX 30
int main(void) {
int already_computed[SERIES_MAX + 1] = { 0, 1 };
int n;
return EXIT_SUCCESS;
return 0;
if (already_computed[n] != 0) {
return already_computed[n];
already_computed[n] = value;
return value;
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 11/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
#include <stdio.h>
int main(void)
return 0;
For example:
$ 1521 mipsy bad_pun.s
HINT:
The i_love_mips.s
lecture example would make a good starting point.
main:
syscall
jr $ra # return
.data
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 12/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
// A simple C program that calculates the Gaussian sum between two numbers
// Written 12/2/2022
#include <stdio.h>
int main(void)
scanf("%d", &number1);
scanf("%d", &number2);
printf("The sum of all numbers between %d and %d (inclusive) is: %d\n", number1, number2, gaussian_sum);
return 0;
For example:
$ 1521 mipsy gaussian_sum.s
The sum of all numbers between 1 and 100 (inclusive) is: 5050
The sum of all numbers between 1 and 1000 (inclusive) is: 500500
HINT:
You can assume that the first number is always smaller than or equal to the second number
You can assume that all input and calculated values can fit in 32-bits
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 13/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
# A simple MIPS program that calculates the Gaussian sum between two numbers
# Written 12/2/2022
# int main(void)
# {
# scanf("%d", &number2);
# printf("The sum of all numbers between %d and %d (inclusive) is: %d\n", number1, number2,
gaussian_sum);
#
# return 0;
# }
main:
syscall
syscall
syscall
syscall
syscall
syscall
syscall
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 14/16
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Laboratory Sample Solutions
syscall
syscall
syscall
li $a0, '\n'
syscall
li $v0, 0
jr $ra # return
.data
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
Week 3 Wednesday 21:00: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.
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:
$ 1521 classrun -sturec
https://cgi.cse.unsw.edu.au/~cs1521/22T1/lab/01/answers 16/16