You are on page 1of 66

BITS Pilani

K K Birla Goa Campus

CSF111: Computer Programming

Aggregate data types I: Arrays

Swaroop Joshi

2023
Representing real-world data

✤ So far—

✤ A single number: sqrt(n)

✤ A single character: response = ‘y’

✤ Very few values: max_of_3(n1, n2, n3)


Representing real-world data

✤ Consider problems like—


✤ So far—

✤ A single number: sqrt(n)

✤ A single character: response = ‘y’

✤ Very few values: max_of_3(n1, n2, n3)


Representing real-world data

✤ Consider problems like—


✤ So far—
✤ Compute a cricket team’s total
✤ A single number: sqrt(n) runs given its scorecard (list of
each player’s runs).
✤ A single character: response = ‘y’

✤ Very few values: max_of_3(n1, n2, n3)


Representing real-world data

✤ Consider problems like—


✤ So far—
✤ Compute a cricket team’s total
✤ A single number: sqrt(n) runs given its scorecard (list of
each player’s runs).
✤ A single character: response = ‘y’
✤ Given a city’s highest
✤ Very few values: max_of_3(n1, n2, n3) temperature record for a month,
display the hottest day.
A possible representation:
Representing real-world data 11 variables for individual runs
12th variable for the total

✤ Consider problems like—


✤ So far—
✤ Compute a cricket team’s total
✤ A single number: sqrt(n) runs given its scorecard (list of
each player’s runs).
✤ A single character: response = ‘y’
✤ Given a city’s highest
✤ Very few values: max_of_3(n1, n2, n3) temperature record for a month,
display the hottest day.
A possible representation:
Representing real-world data 11 variables for individual runs
12th variable for the total
Cumbersome!
Error prone!
Dif cult to carry the sense of
✤ Consider problems like—
abstraction…
✤ So far—
✤ Compute a cricket team’s total
✤ A single number: sqrt(n) runs given its scorecard (list of
each player’s runs).
✤ A single character: response = ‘y’
✤ Given a city’s highest
✤ Very few values: max_of_3(n1, n2, n3) temperature record for a month,
display the hottest day.
fi
Representing real-world data

✤ Consider problems like—


✤ So far—
✤ Compute a cricket team’s total
Most languages
✤ A single number: sqrt(n)
have some runs given its scorecard (list of
way to represent ‘many values
of the same type’
each player’s runs).
✤ A single character: response = ‘y’
Simplest: arrays!
✤ Given a city’s highest
✤ Very few values: max_of_3(n1, n2, n3) temperature record for a month,
display the hottest day.
Array
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 | əˈreɪ |

Array noun
an impressive display or range of a particular type of thing:
there is a vast array of literature on the topic

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
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
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.

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
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

11
TEAM_SIZE

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
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
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.

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
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

11
TEAM_SIZE

23 12 34 0 57 39 5 3 2 10 8
scores

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
Array

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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
Array

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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
printf("%d\n", scores[0]);

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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
printf("%d\n", scores[0]);
Print the value at the 0-th index, or 0
printf("%d\n", scores[1]); steps from the start of the array

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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
printf("%d\n", scores[0]);
Print the value at the 1-st index, or 1
printf("%d\n", scores[1]);
step from the start of the array
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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
printf("%d\n", scores[0]);

printf("%d\n", scores[1]); Print the value at the 10-th index, or


printf("%d\n", scores[10]); 10 steps from the start of the array
Array The rst element in an N-length
array is at index 0

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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
printf("%d\n", scores[0]);

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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
printf("%d\n", scores[0]);

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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
printf("%d\n", scores[0]);

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

const int TEAM_SIZE = 11;


int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};
printf("%d\n", scores[0]);

printf("%d\n", scores[1]);

printf("%d\n", scores[10]);
Compute total score

✤ Accessing each value and


11
making a cumulative sum TEAM_SIZE

✤ How to access each value? 0 1 2 3 4 5 6 7 8 9 10


23 12 34 0 57 39 5 3 2 10 8
scores
Compute total score

✤ Accessing each value and


11
making a cumulative sum TEAM_SIZE

✤ How to access each value? 0 1 2 3 4 5 6 7 8 9 10


23 12 34 0 57 39 5 3 2 10 8
scores

int total_score = 0;
int index = 0;
while (index < TEAM_SIZE) {
total_score += scores[index];
++index;
}

printf("Total score: %d\n", total_score);


Compute total score

✤ Accessing each value and


11
making a cumulative sum TEAM_SIZE

✤ How to access each value? 0 1 2 3 4 5 6 7 8 9 10


23 12 34 0 57 39 5 3 2 10 8
scores

int total_score = 0;
int index = 0; Identify the initialisation, test,
while (index < TEAM_SIZE) { loop body, and update
total_score += scores[index];
++index;
}

printf("Total score: %d\n", total_score);


Compute total score

✤ Accessing each value and


11
making a cumulative sum TEAM_SIZE

✤ How to access each value? 0 1 2 3 4 5 6 7 8 9 10


23 12 34 0 57 39 5 3 2 10 8
scores

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

✤ Accessing each value and


11
making a cumulative sum TEAM_SIZE

✤ How to access each value? 0 1 2 3 4 5 6 7 8 9 10


23 12 34 0 57 39 5 3 2 10 8
scores

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

✤ Accessing each value and


11
making a cumulative sum TEAM_SIZE

✤ How to access each value? 0 1 2 3 4 5 6 7 8 9 10


23 12 34 0 57 39 5 3 2 10 8
scores

int total_score = 0;
int index = 0;
while (index < TEAM_SIZE) {
total_score += scores[index];
++index;
}

printf("Total score: %d\n", total_score);


A for loop
A for loop
✤ Another format of a loop

✤ Preferred when the loop is supposed to go over a


range in equals hops

✤ Accessing each element of an array

✤ Processing all even integers between m and n


A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n


A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body Update
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body Update
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body Update
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body Update
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array

✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body Update
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index;
}
✤ Accessing each element of an array
Any while loop can be written as a
for loop, and vice versa ✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body Update
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index; Any of these four can be empty
} But don’t forget the semicolon in the (..)
✤ Accessing each element of an array

Any while loop can be written as a


for loop, and vice versa ✤ Processing all even integers between m and n

for (int index = 0; index < TEAM_SIZE; ++index) {


total_score += scores[index];
}
Initialisation Test Body Update
A for loop
✤ Another format of a loop
int index = 0;
while (index < TEAM_SIZE) { ✤ Preferred when the loop is supposed to go over a
total_score += scores[index]; range in equals hops
++index; Any of these four can be empty
} But don’t forget the semicolon in the (..)
✤ Accessing each element of an array

Any while loop can be written as a


for loop, and vice versa ✤ Processing all even integers between m and n

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};

printf("Total score: %d\n", array_sum(scores, TEAM_SIZE));

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};

printf("Total score: %d\n", array_sum(scores, TEAM_SIZE));

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; It needs to know the length of the array;


} don’t forget to pass it to the function
int main() {
const int TEAM_SIZE = 11;
int scores[TEAM_SIZE] = {23, 12, 34, 0, 57, 39, 5, 3, 2, 10, 8};

printf("Total score: %d\n", array_sum(scores, TEAM_SIZE));

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};

printf("Total score: %d\n", array_sum(scores, TEAM_SIZE));

return 0;
}
Your turn

✤ Write a function to nd the


largest element in a
nonempty array of integers
fi
Your turn

Data An array and its length

✤ Write a function to nd the


largest element in a
nonempty array of integers
fi
Your turn

Data An array and its length


Contract, Finds the largest element in the
Purpose, and given array
Header int largest(int nums[], int n)
✤ Write a function to nd the
largest element in a
nonempty array of integers
fi
Your turn

Data An array and its length


Contract, Finds the largest element in the
Purpose, and given array
Header int largest(int nums[], int n)
✤ Write a function to nd the
1 largest([1], 1)
largest element in a
Examples 2 largest([2, 1], 2)
nonempty array of integers …
fi
Your turn

Data An array and its length


Contract, Finds the largest element in the
Purpose, and given array
Header int largest(int nums[], int n)
✤ Write a function to nd the
1 largest([1], 1)
largest element in a
Examples 2 largest([2, 1], 2)
nonempty array of integers …

Body // ???
fi
Your turn

✤ Write a function to nd the


mean of a nonempty array of
real numbers
fi
Your turn

Data An array and its length

✤ Write a function to nd the


mean of a nonempty array of
real numbers
fi
Your turn

Data An array and its length


Contract,
Finds the mean of the given array.
Purpose,
double mean(double nums[], int n)
and Header
✤ Write a function to nd the
mean of a nonempty array of
real numbers
fi
Your turn

Data An array and its length


Contract,
Finds the mean of the given array.
Purpose,
double mean(double nums[], int n)
and Header
✤ Write a function to nd the
1 mean([1], 1)
mean of a nonempty array of
Examples 1.5 mean([2, 1], 2)
real numbers …
fi
Your turn

Data An array and its length


Contract,
Finds the mean of the given array.
Purpose,
double mean(double nums[], int n)
and Header
✤ Write a function to nd the
1 mean([1], 1)
mean of a nonempty array of
Examples 1.5 mean([2, 1], 2)
real numbers …

Body // ???
fi

You might also like