Or suppose we'd like to

manage population statistics,

how would these statistics be represented ?

How would be represented a set, an array, of data (ages) ?

Or in the case of statistics,

how would a person involved in the statistics be represented ?

In both cases, we'll use what is known as data structure.

As another example, how would we represent the names of the persons involved in the statistics

using the types we have seen until now ?

We wouldn't know how to do it.

This episode will look at arrays.

We'll see later in other episodes,

how to represent sequence of characters,

and then how to represent structures,

namely, how to represent a line in such an array.

But let us begin with arrays.

Let's suppose that we want to create a game program

and that we have to manage the players' scores,

we would like for example to display the score

of every player,

as well as the deviation of these scores from the mean.

How would we proceed ?

Let's modestly begin by handling

the case of a two-player program.

What we would do, of course, is to introduce

a variable for the score of each of the two players.

So, let's say that their scores are here integers,

which will be initialized using a particular function here.

This function would calculate and return the score.

The score is computed like this for the first player.

Similarly, computing the second player's score

is done using the parameters associated with this second player.

And then, as we want the deviation from the mean,

we compute the mean of the two players by using the function <i>moyenne</i> (TN : <i>moyenne</i> means <i>mean</i>)

that we have already illustrated in previous episodes.

This function would take the scores of our two players as parameters.

The mean is stored in a variable moyenne_joueurs of type integer.

And finally, we could print the scoreboard,

by printing the first score and the deviation from the mean,

and then by printing the second score and its deviation from the mean.

Two players is not bad, but the question is,

how can we improve our program to accept more players ?

We could simply use several variables.

So for example, if we wanted to pass to five players,

we would then typically use five scores : one for each player.

We would then compute the score of each player

using our function (with the players' specific parameters).

And we would compute the mean of all these players

by changing this time the mean function

and passing the five scores to the new mean function,

then we would write the scoreboard like this.

Here, any good programmer would immediately

thinks of using a loop,

and will therefore modify this part of the program

to introduce a loop here.

The loop would go from the first player to the fifth one,

it would iterate on each player,

and print the score of every player,

as well as its deviation from the mean.

The question is how to do it concretely ?

Because firstly, writing scorei like this, from the previous

program, isn't valid.

scorei would be here a variable name.

It can't be used with different values of i. scorei cannot stand for score1 or score2.

So this is something that doesn't work.

Also, how would we do if we moved to 100, 1000 players ?

It would quickly become tedious to write 100 variables,

modify the mean function to take 100 parameters,

so it's just impossible in practice.

Finally, how would we do if the number of players is unknown at the beginning ?

You wouldn't know if you had to deal with five, two, or ten players.

The answer to fulfill all these needs is actually the same,

it's introducing a new type, the array type.

Let me give you by way of preamble the version

with an array of the players' score program,

but of course we'll detail all this

later on in this episode.

So, we would declare an array of players,

then we would loop,

we again encounter this notion of loop to compute the score

of each player,

then we would compute the mean here,

this mean could take an array of scores,

and finally we would print the final score

with here a for loop which iterates on all the values of the array.

Let's have a look at these different elements one by one.