COMP2521
Data Structures & Algorithms
Week 1.2
COMP1511 Code Gap
1
In this lecture
Why?
You need to understand slightly more about C to complete
COMP2521
What?
for statements (looping)
asserts
switch
2
Style
There will be some relaxing of style requirements compared
to the strict styleguide of COMP1511 (e.g. we will allow
multiple returns). However, your code must adhere to the
COMP2521 style guide at all times.
3
For Loops
COMP1511 COMP2521
#include
int main(int argc, char* argv[]) {
int sum = 0;
int i = 1;
while (i < 10) {
sum = sum + i;
i++;
}
printf("%d\n", sum);
}
1
2
3
4
5
6
7
8
9
10
11
#include
int main(int argc, char* argv[]) {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += i;
}
printf("%d\n", sum);
}
1
2
3
4
5
6
7
8
9
Ways of combining initialization, condition, and increment in one line
for (init; cond; incr) { }
4
Switch Statements
COMP1511 COMP2521
#include
int main(int argc, char* argv[]) {
char input;
scanf(“%c\n”, &input);
if (input == ‘W’) {
printf(“Up\n”);
} else if (input == ‘A’) {
printf(“Left\n”);
} else if (input == ‘S’) {
printf(“Down\n”);
} else if (input == ‘D’) {
printf(“Right\n”);
} else {
printf(“Not valid\n”);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Switch statements make it easier to handle branching when dealing with
equality comparisons where one side is always the same.
#include
int main(int argc, char* argv[]) {
char input;
scanf(“%c\n”, &input);
switch (input) {
case ‘W’: printf(“Up\n”); break;
case ‘A’: printf(“Left\n”); break;
case ‘S’: printf(“Down\n”); break;
case ‘D’: printf(“Right\n”); break;
default: printf(“Not valid\n”);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
5 . 1
Switch Statements
COMP2521
Now let’s write this as pseudocode – a method of describing the flow of
program logic without having to use a particular programming langauge
syntax.
switch (v) {
case CASE1:
STATEMENT1;
break;
case CASE2:
STATEMENT2;
break;
…
case CASEn:
STATEMENTx;
break;
default:
STATEMENTy;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
5 . 2
Exercise 1
Write a function monthName(int) that
accepts a month number 1=Jan … 12=Dec
returns a string containing the month name
assume that the string will be read-only
use a switch to decide on the month
Suggest an alternative approach using an array.
6 . 1
Exercise 2
Write a program that prints integer sequences (one per line):
Package the core part as a function:
main checks errors and sets up args for seq()
seqq 10 prints 1 2 3 4 5 6 7 8 9 10
seqq 5 10 prints 5 6 7 8 9 10
seqq 10 1 prints 10 9 8 7 6 5 4 3 2 1
seqq 1 3 10 prints 1 4 7 10
seqq 1 3 11 prints 1 4 7 10
seqq -3 prints 1 0 -1 -2 -3
seqq 1 -3 10 gives an error
1. void seq(int start, int step, int finish) {…}
6 . 2
Asserts
Asserts are statements that given a condition, will crash the program if
the condition is not true. We them for testing.
COMP1511 COMP2521
#include
int is_even(int num) {
if (num % 2 == 0) {
return 1;
}
return 0;
}
void test_is_even() {
if (is_even(2) != 1) {
printf(“Failed is_even(2)\n”);
}
if (is_even(3) != 0) {
printf(“Failed is_even(3)\n”);
}
}
int main(int argc, char* argv[]) {
test_is_even();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include
#include
int is_even(int num) {
if (num % 2 == 0) {
return 1;
}
return 0;
}
void test_is_even() {
assert(is_even(2) == 1);
assert(is_even(3) == 0);
}
int main(int argc, char* argv[]) {
test_is_even();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
7