Lecture 4 - Arrays&Loops
Lecture 4 - Arrays&Loops
Code: XX-XX-XX
22 February, 2024
This session
• Arrays
o Scalar vs array data types
o How to declare them
o How to access their items
• Loops
o while, do-while, for
o Flow control within loops
Arrays Loops
• Declare a single block of • Runs a block of code a
memory number of times
• Holding multiple variables of • Can run repeatedly until a
the same type condition is met
• Can be treated as a group or • Can work with array items
individual items iteratively
Scalar declaration
data
type
name = value ;
• We can choose any valid combination of letters, numbers and underscores
• Names like this represent scalar variables – they are completely independent
Array declaration
data
type
name [N] = value
;
• Adding square brackets lets us declare how many memory items to associate with this
variable name
• Functionally, this is the same as declaring N variables one after the other
o Except, we can refer to them as a group by their name
Scalar vs Array
Scalar Declaration Array Declaration
int sc1; int arr[3];
int sc2;
int sc3; arr[0] = 1;
arr[1] = 2;
sc1 = 1; arr[2] = 3;
sc2 = 2;
sc3 = 3;
printf("My array items:
printf("My scalars: %d, %d, %d, %d",
%d, %d", sc1, sc2, arr[0], arr[1],
sc3); arr[2]);
Scalar vs Array
Scalar Declaration Array Declaration
int sc1 = 1; int arr[3] = {1, 2, 3};
int sc2 = 2;
int sc3 = 3;
1 2 3
also refer to it as a whole by 'referencing'
the variable arr[0]; finding its starting
memory location
input # # #
Example: user input
Array Declaration
• Familiar from the 2nd week of exercises -> char input[3];
&input[0]
scanf("%2s", &input[0]);Whole array
input[0] input[1] input[2] referenced
&
input[0] input[1] input[2] 'address of'
# # #
operator
input
Loops
• Structures that repeat an inner block of code
• Come in three flavours:
• While the expression is true, execute code scanf("%d %d", &start, &end);
block repeatedly
• Expression ideally will change at some point int t = start;
to allow the while loop to exit
while (t < end) {
• Does this loop ever end?
printf("%d", t);
t++;
}
While
int start, end;
• Yes; regardless of how large end is, t will scanf("%d %d", &start, &end);
eventually reach it
• If start >= end, it won't run int t = start;
printf("%d", t);
t++;
}
While
int start, end;
} else {
printf("%d", t);
t++;
}
}
While
int start, end;
• No! We have made a mistake, and are not scanf("%d %d", &start, &end);
checking the right variables
int t = start;
} else {
printf("%d", t);
t++;
}
}
While
int start, end;
} else {
printf("%d", t);
t++;
}
}
While
int start, end;
while (t != end) {
printf("%d", t);
t = t + 2;
}
While
int start, end;
printf("%d", t);
t = t + 2;
}
Do-While
#include <robot.h> // not a real .h
int status;
• A while loop flipped on its head
• Performs the loop body, then checks the do {
condition
status = startUp();
• Great for if something needs to run at least
once before checking if it needs to be run if (status != 0) {
again
• Couple of easy things to miss printf("Startup exited with
o Semicolon after the while expression status
%d: retrying...",
o No condition after the do keyword
status);
}
? . . . ?
for (int i = 0; i < samples; i++) {
data
currentItem = data[i];
nextItem = data[i+1];
nextItem = data[i];
}
Flow control - break
int emailAddress[40];
&data[0] int size = sizeof(emailAddress);
scanf("%s", emailAddress);
data[0] data[...] data[9] int username[20];
if(emailAddress[i] == '@') {
if(emailAddress[i] == '.') {
}
Casting arrays
&input[0] &output[0]
input[0] input[...] input[9] output[0] output[...] output[9]
? . . . ?
input[0][0] input[1][0] input[2][0]
• 2D (and higher) arrays are perfectly possible
3 6 9
Working with 2D Arrays
int input[3][3] = {{1, 2, 3},
&input[0][0] {4, 5, 6},
{7, 8, 9}};
input[0] input[1] input[2]
? . . . ?
input[0][0] input[1][0] input[2][0] n_cols = sizeof(input[0]);
n_rows = sizeof(input) / n_cols;
input
2 5 8
printf("Item (%d, %d): %d",
i, j, output[i][j]);
}
3 6 9 • Scales poorly, but is essential for working
with high-dimensional datasets
Flow control - break
int input[3][3] = {{1, 2, 3},
{4, 5, 6},
• break statements can also be used within {7, 8, 9}};
nested loops
• They only exit the innermost block they are n_cols = sizeof(input[0]);
called in, not all parent blocks n_rows = sizeof(input) / n_cols;
// do something
break;
}
Flow control - continue
int input[3][3] = {{1, 2, 3},
{4, 5, 6},
• continue can also be used within nested {7, 8, 9}};
loops
• Like break, continue stays within its n_cols = sizeof(input[0]);
block n_rows = sizeof(input) / n_cols;
// do something
continue;
}
Flow control - goto
int input[3][3] = {{1, 2, 3},
{4, 5, 6},
• If we want to exit several layers of {7, 8, 9}};
nested blocks at once, we can use goto
• Can simplify highly nested code n_cols = sizeof(input[0]);
n_rows = sizeof(input) / n_cols;
• It is, however, very difficult to debug
when overused for (int i = 0; i < n_cols; i++){
• If you find yourself wanting to use goto,
consider if there isn't a way to refactor your for (int j = 0; j < n_rows; j++){
code or rethink the problem first
// do something
goto exitPoint;
XX-XX-XX
• Loops
o while
o do-while
o for
Do I need to submit my source code? Yes; zipped, as per recent video demo
Do I need to do
flowcharts/pseudocode for every Yes; this is worth as much as the
exercise? source code itself
When is the first deadline? This Sunday; 25th Feb, 2pm. #1 and #2
None for the progress submissions, yes
What about RAs/extensions? for the final submission