LightReader

Chapter 9 - Chapter 9 — The Data Vault

The robot on the screen stood quietly again.

Its little square eyes blinked.

[^_^]

/| |\

/ \

Below it, the computer printed another system message.

SYSTEM PROGRESS: 80%

NEXT MODULE: DATA STORAGE

REQUIREMENT: ARRAY SYSTEM

Pran leaned forward.

"Data storage?"

The cursor blinked.

_

Pran thought about everything he had learned so far.

Variables could store one value.

For example:

int age = 27;

That worked perfectly.

But what if the computer needed to store many numbers?

For example:

5 test scores

10 robot positions

100 sensor readings

Creating hundreds of variables would be ridiculous.

Imagine writing this:

int score1;

int score2;

int score3;

int score4;

int score5;

And then doing it again for 100 numbers.

Programmers would go insane.

Fortunately, C has something designed exactly for this.

Something called an array.

An array allows you to store many values inside one variable.

The computer screen flickered.

A new diagram appeared.

DATA VAULT

---------------------------------

Slot Value

---------------------------------

0 ?

1 ?

2 ?

3 ?

4 ?

---------------------------------

Pran nodded.

"So the vault has slots."

Exactly.

Each slot stores a value.

And every slot has a position number, called an index.

Here is the interesting part.

In C programming, arrays start at 0, not 1.

So the positions look like this:

Index: 0 1 2 3 4

Value: ? ? ? ? ?

Pran opened the code editor again.

He typed a simple array.

#include

int main() {

int numbers[5] = {10, 20, 30, 40, 50};

printf("%d\n", numbers[0]);

return 0;

}

He ran the program.

The output appeared.

10

Pran pointed at the screen.

"That printed the first number!"

Exactly.

The array looks like this in memory.

Index: 0 1 2 3 4

Value: 10 20 30 40 50

So:

numbers[0]

means:

Give me the value in position 0.

Which is:

10

Pran changed the code slightly.

printf("%d\n", numbers[3]);

He ran the program again.

The output appeared.

40

Pran grinned.

"That's easy."

The computer screen flickered again.

The Data Vault diagram updated.

DATA VAULT

---------------------------------

Slot Value

---------------------------------

0 10

1 20

2 30

3 40

4 50

---------------------------------

Another message appeared.

ARRAY STORAGE VERIFIED

Then another.

DATA ORGANIZATION REQUIRED

Pran tilted his head.

"Organization?"

The computer printed another message.

LOAD VALUES INTO DATA VAULT

Pran realized something.

Instead of writing the values when creating the array, he could also fill them later.

He wrote another program.

#include

int main() {

int numbers[5];

numbers[0] = 5;

numbers[1] = 15;

numbers[2] = 25;

numbers[3] = 35;

numbers[4] = 45;

printf("%d\n", numbers[2]);

return 0;

}

He ran it.

The output appeared.

25

Pran nodded.

"That's actually very useful."

Arrays are used everywhere in programming.

Examples:

• storing game scores• storing player positions• storing lists of items• storing sensor data• storing images and sounds

Without arrays, modern software would be impossible.

The computer screen flickered again.

A final Data Vault diagram appeared.

DATA VAULT

---------------------------------

Index Value

---------------------------------

0 5

1 15

2 25

3 35

4 45

---------------------------------

Then a new system message appeared.

DATA VAULT SECURED

The robot on the screen suddenly stood taller.

[^o^]

/| |\

/ \

Another message appeared.

SYSTEM PROGRESS: 95%

Pran's eyes widened.

"Ninety five percent?"

The system was almost fully unlocked.

Then the computer displayed one final message.

FINAL MODULE DETECTED

Pran leaned closer.

The screen flashed brightly.

Then new text appeared.

FINAL CHALLENGE

BUILD A COMPLETE PROGRAM

Pran smiled.

"So this whole system was training me."

He had learned:

• printing• input• variables• decisions• loops• functions• pointers• arrays

Now the computer wanted him to use everything together.

The cursor blinked again.

_

Waiting.

Silent.

The final test had begun.

Chapter 10 — The Final Program for C

You will build a complete C program, combining everything learned:

input

loops

functions

arrays

conditions

More Chapters