COMP1521 22T1 - Week 01 Tutorial Answers
COMP1521 22T1 - Week 01 Tutorial Answers
Week 01
Tutorial
Answers
1. Class introduction (for everyone, starting with the tutor):
Please turn on your camera for the introduction, and for all your COMP1521 tut-labs,
unless you have a cute pet in which case it's required, but you only need show the pet.
What is your preferred name (what should we call you?)
What other courses are you doing this term
What parts of C from COMP1511/COMP1911 were the hardest to understand?
Do you know any good resources to help students who have forgotten their C?
For example:
https://learnxinyminutes.com/docs/c/
ANSWER:
Answered in class
2. Consider the following C program skeleton:
int a;
char b[100];
int fun1() {
int c, d;
...
double e;
int fun2() {
int f;
...
fun1();
...
int g;
int main(void) {
char h[10];
int i;
...
fun2()
...
int main(void) {
// ...
int main(void)
printf("%d\n", nums[i]);
return 0;
This program uses a for loop to print each element in the array
Rewrite this program using a recursive function
ANSWER:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/01/answers 2/12
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Tutorial Answers
#include <stdio.h>
if (i == 10) return;
printf("%d\n", nums[i]);
print_array(nums, i + 1);
int main(void)
print_array(nums, 0);
return 0;
int main(void) {
char str[10];
str[0] = 'H';
str[1] = 'i';
printf("%s", str);
return 0;
What will happen when the above program is compiled and executed?
ANSWER:
The above program will compile without errors .
printf, like many C library functions expects strings to be null-
terminated.
In other words printf, expects the array str to contain an element with value '\0'
which marks the end of the
sequence of characters to be printed.
printf will print str[0] ('H'), str[1] then examine
str[2].
Another common behaviour will be that the program prints some extra "random" characters.
It is also possible the program will index outside the array which would result in it stopping
with an error if it was
compiled with dcc.
If the program was compiled with gcc and uses indexes well outside the array it may
be terminated by the operating
system because of an illegal memory access.
int main(void) {
char str[10];
str[0] = 'H';
str[1] = 'i';
str[2] = '\0';
printf("%s", str);
return 0;
b. gcc -S x.c
c. gcc -c x.c
d. gcc x.c
Use the following simple C code as an example:
#include <stdio.h>
#define N 10
int main(void) {
printf("%s\n", str);
return 0;
ANSWER:
a. gcc -E x.c
mipsy is very new so if you find bugs - please report in the course forum.
i. $31
ANSWER:
a. $0 = $zero
The "zero" register.
This register is read-only,
and always contains the value zero.
Values written to this register
are discarded.
It is frequently used as a source register
when we want to copy constant values
into another
register,
or as a destination register
when we don't care about
the result of an instruction.
b. $1 = $at
The "assembler temporary" register.
This register is used for
various purposes by the assembler.
One use is to
hold the results of tests
when implementing conditional branch pseudo-instructions.
Programmers are
encouraged to not use it directly.
c. $2 = $v0
This value is used to hold
the return value from a function
that is returning a single 32-bit result.
There is a
second register ($v1)
that can be used to return
a second 32-bit result, if needed.
d. $4 = $a0
This register is used to hold
the first argument to a function,
if that argument fits in a 32-bit register.
There are
three other registers,
$a1 to $a3,
that can be used to hold arguments to functions.
Arguments that don't fit into
32-bits
are placed on the stack.
e. $8 = $t0
Used for holding temporary values
while calculating expressions.
There are 10 temporary registers
that can be
used for this purpose:
$t0 through $t9.
Programmers should not rely on
the values of these registers
persisting
over a function call.
f. $16 = $s0
The set of registers
$s0 through $s7
are used to hold values
that are required to persist
across function calls.
In
other words,
if a programmer stores a value
in one of these registers
and then calls a function,
the same value
should be in the register
after the function returns.
If necessary,
the function being called
needs to save and
restore these values.
g. $26 = $k0
This register (and register $k1)
are reserved for use by the operating system.
Programmers should not use these
registers.
h. $29 = $sp
The "stack pointer" register.
This register holds the address of
the top of the program's run-time stack.
Its initial
value is 0x7FFFFEFC,
and it changes down towards lower addresses.
Each time a function is called,
it needs to
reduce $sp
by an amount large enough to hold
all of its non-register local variables,
and space for
saving/restoring register values.
i. $31 = $ra
This register holds a return address.
When a linking instruction is invoked,
such as jal,
the address of the next
instruction
is stored in register $ra.
Link are often used
to implement function calls:
when the function wants to
return,
it can use jr $ra
to return to the correct location.
#include <stdio.h>
int main(void) {
int x, y;
scanf("%d", &x);
y = x * x;
printf("%d\n", y);
return 0;
li $v0, 4
syscall
syscall
li $v0, 1
syscall
li $v0, 11
syscall
jr $ra # return
.data
prompt:
.asciiz "Enter a number: "
Revision questions
The following questions are primarily intended for revision, either this week or later in session.
Your tutor may still choose to cover some of these questions, time permitting.
1. C's sizeof operator is a prefix unary operator (precedes its 1 operand) - what are examples of other C unary operators?
ANSWER:
2. Why is C's sizeof operator different to other C unary & binary operators?
ANSWER:
sizeof can be given a variable, value or a type as an argument.
The syntax to distinguish this is weird, a type must be surrounded by brackets to be passed to sizeof.
Note the value of sizeof can mostly be pre-calculated by the compiler when it compiles the program.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/01/answers 6/12
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Tutorial Answers
c = a;
d.data = 42;
c->data = 42;
ANSWER:
sizeof b should be sizeof *b
sizeof struct node should be sizeof (struct node)
malloc(8) might be correct (depending on what struct ndoe is) but is definitely non-portable,
struct node might be 8
bytes on a 32-bit OS and 12 or 16 bytes on a 64-bit OS
d.data is incorrect as d is not a struct its a pointer to a struct
int main(void) {
int *p;
p = &n[0];
n[0]++;
printf("%d\n", *p);
p++;
printf("%p\n", p);
printf("%d\n", *p);
return 0;
43
0x7fff00000004
23
b. x = 5; # x == 5, p == 1000
c. *p = 3; # x == 3, p == 1000
7. What is a struct? What are the differences between structs and arrays?
ANSWER:
Arrays and struct are both compound data types, formed from other data types.
Array are homogeneous - formed from a single data type.
Structs can be heterogeneous - formed from a multiple data types.
Array element are accessed with integer array indexes.
Structs fields are accessed by name.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/01/answers 8/12
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Tutorial Answers
#include <stdio.h>
#include <string.h>
#define MAX_BREED_LENGTH 64
struct pet {
char name[MAX_NAME_LENGTH];
char breed[MAX_BREED_LENGTH];
int age;
int weight;
};
int main(void) {
strcpy(my_pet.name, "Fluffy");
strcpy(my_pet.breed, "axolotl");
my_pet.age = 7;
my_pet.weight = 300;
return 0;
9. Write a function that increases the age of fluffy by one and then increases
its weight by the fraction of its age that has
increased. The function is defined like this:
void age_fluffy(struct pet *my_pet);
e.g.: If fluffy goes from age 7 to age 8, it should end up weighing 8/7 times
the amount it weighed before. You can store the
weight as an int and ignore
any fractions.
Show how this function can be called by passing the address of a struct variable
to the function.
ANSWER:
#include <stdio.h>
#include <string.h>
#define MAX_BREED_LENGTH 64
struct pet {
char name[MAX_NAME_LENGTH];
char breed[MAX_BREED_LENGTH];
int age;
int weight;
};
int main(void) {
strcpy(my_pet.name, "Fluffy");
strcpy(my_pet.breed, "axolotl");
my_pet.age = 7;
my_pet.weight = 300;
age_fluffy(&my_pet);
return 0;
my_pet->age = my_pet->age + 1;
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/01/answers 9/12
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Tutorial Answers
10. Write a main function that takes command line input that fills out the fields
of the pet struct. Remember that command line
arguments are given to our main
function as an array of strings, which means we'll need something to convert
strings to
numbers.
ANSWER:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_BREED_LENGTH 64
struct pet {
char name[MAX_NAME_LENGTH];
char breed[MAX_BREED_LENGTH];
int age;
int weight;
};
if(argc < 5) {
return 1;
} else {
strcpy(my_pet.name, argv[1]);
strcpy(my_pet.breed, argv[2]);
return 0;
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/01/answers 10/12
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Tutorial Answers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
if (argc != 2) exit(1);
trim(string);
freeTokens(tokens);
return 0;
first = 0;
last = strlen(str)-1;
int i, j = 0;
str[j] = '\0';
char *tmp;
// count tokens
tmp = strdup(str);
int n = 0;
free(tmp);
assert(strings != NULL);
tmp = strdup(str);
strings[i++] = strdup(next);
strings[i] = NULL;
free(tmp);
return strings;
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/01/answers 11/12
23/05/2022, 16:17 COMP1521 22T1 — Week 01 Tutorial Answers
free(toks);
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/01/answers 12/12