Professional Documents
Culture Documents
Swaroop Joshi
2023
Representing real-world data
✤ So far—
Array noun
an impressive display or range of a particular type of thing:
there is a vast array of literature on the topic
array | əˈreɪ |
Array noun
an impressive display or range of a particular type of thing:
there is a vast array of literature on the topic
Array noun
an impressive display or range of a particular type of thing:
there is a vast array of literature on the topic
I need a box big enough to keep
an integer in it and I want to call
it ‘TEAM_SIZE’. Its value is 11
and it cannot be changed.
Array noun
an impressive display or range of a particular type of thing:
there is a vast array of literature on the topic
11
TEAM_SIZE
Array noun
an impressive display or range of a particular type of thing:
there is a vast array of literature on the topic
I need an array of TEAM_SIZE
boxes, each big enough to keep
an integer, and I want to call it 11
scores. Each of it should hold the TEAM_SIZE
values given here in the given
order.
Array noun
an impressive display or range of a particular type of thing:
there is a vast array of literature on the topic
11
TEAM_SIZE
23 12 34 0 57 39 5 3 2 10 8
scores
11
TEAM_SIZE
0 1 2 3 4 5 6 7 8 9 10
23 12 34 0 57 39 5 3 2 10 8
scores
11
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
23 12 34 0 57 39 5 3 2 10 8
scores
11
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
11
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
printf("%d\n", scores[1]);
printf("%d\n", scores[10]);
Array
11
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
printf("%d\n", scores[10]);
Array
11
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
11
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
11
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
printf("%d\n", scores[1]);
printf("%d\n", scores[10]);
fi
Array
The last element in an N-length array
11 is at index N-1
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
printf("%d\n", scores[1]);
printf("%d\n", scores[10]);
Array
11
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
printf("%d\n", scores[1]);
printf("%d\n", scores[10]);
Array
HW: what happens if you try
to print scores[k], where
11
k<0 or k≥TEAM_SIZE?
TEAM_SIZE
Array indexes 0 1 2 3 4 5 6 7 8 9 10
Array values 23 12 34 0 57 39 5 3 2 10 8
scores
printf("%d\n", scores[1]);
printf("%d\n", scores[10]);
Compute total score
int total_score = 0;
int index = 0;
while (index < TEAM_SIZE) {
total_score += scores[index];
++index;
}
int total_score = 0;
int index = 0; Identify the initialisation, test,
while (index < TEAM_SIZE) { loop body, and update
total_score += scores[index];
++index;
}
int total_score = 0;
int index = 0;
while (index < TEAM_SIZE) {
total_score += scores[index];
++index; Does this code work as
} expected? Trace it!
printf("Total score: %d\n", total_score);
Compute total score
int total_score = 0;
int index = 0;
while (index < TEAM_SIZE) { What is the value of index after
total_score += scores[index];
++index; the loop? How many times did
} the loop run? How many times
was the test evaluated?
printf("Total score: %d\n", total_score);
Compute total score
int total_score = 0;
int index = 0;
while (index < TEAM_SIZE) {
total_score += scores[index];
++index;
}
for (int index = 0; index < TEAM_SIZE; ++index) { HW: what is the scope of index
total_score += scores[index];
}
in the for loop example?
Passing an array to a function
/**
* @brief Computes the sum of elements of the given array of the given length.
* ...
*/
int array_sum(int nums[], int length) {
int answer = 0;
for (int index = 0; index < length; ++index)
answer += nums[index];
return answer;
}
int main() {
const int TEAM_SIZE = 11;
int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
return 0;
}
Passing an array to a function
/**
* @brief Computes the sum of elements of the given array of the given length.
* ...
*/ A general function that can be used
int array_sum(int nums[], int length) {
int answer = 0; in several examples
for (int index = 0; index < length; ++index)
answer += nums[index];
return answer;
}
int main() {
const int TEAM_SIZE = 11;
int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
return 0;
}
Passing an array to a function
/**
* @brief Computes the sum of elements of the given array of the given length.
* ...
*/
int array_sum(int nums[], int length) {
int answer = 0;
for (int index = 0; index < length; ++index)
answer += nums[index];
return 0;
}
Passing an array to a function
/**
* @brief Computes the sum of elements of the given array of the given length.
* ...
*/
int array_sum(int nums[], int length) {
int answer = 0;
for (int index = 0; index < length; ++index)
answer += nums[index];
return answer;
}
int main() {
const int TEAM_SIZE = 11;
int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
return 0;
}
Your turn
Body // ???
fi
Your turn
Body // ???
fi