💩

programierds

c / arrays / matrices

One-dimensional and multidimensional arrays in C

This presentation is designed for someone just starting out. We go from the problem of having many separate variables to understanding why an array organizes data better and how a matrix adds rows and columns.

1 namemany data of the same type
index 0always starts from zero
2 indicesrow and column in matrices
Memory concept of an array: int notas[5] = {8, 9, 6, 10, 7};
notas[0]
8
notas[1]
9
notas[2]
6
notas[3]
10
notas[4]
7

1. Why do arrays exist?

Because having many separate variables is chaos. An array lets you store several values of the same type under a single name and access each one using its position.

Bad approach with lots of data

int nota1;
int nota2;
int nota3;
int nota4;
int nota5;

This becomes repetitive, hard to maintain, and later it is very difficult to traverse or process the information.

Better approach with array

int notas[5];
  • Single name: notas.
  • All elements are the same type.
  • Each value lives at a position: notas[0], notas[1], etc.

2. One-dimensional array: a row of lockers

A one-dimensional array can be thought of as a single row. It has several positions but only one dimension. The important thing is to understand that the index starts at <strong>0</strong>.

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

printf("%d\n", numeros[0]);
printf("%d\n", numeros[2]);
  • numeros[0] is 10.
  • numeros[2] is 30.
  • If the array has 5 elements, the last valid index is 4.

Correct mental model

0
10
1
20
2
30
3
40
4
50

Don't think "5 values → last index 5". That's a classic mistake. Think like this: <strong>count = 5</strong>, <strong>last index = 4</strong>.

3. Step by step: load and read an array

Here you can see how an array is filled with a loop. Notice that we don't write one line per position: we let the index do the repetitive work.

Guided code Active line: 1
int numeros[3];
int i;
for (i = 0; i < 3; i = i + 1) {
  numeros[i] = i + 1;
}
printf("%d %d %d\n", numeros[0], numeros[1], numeros[2]);
Step 1: Space is reserved for 3 integers.
Step 1 / 6

Array state

numeros[0]
-
numeros[1]
-
numeros[2]
-
./arreglo-demostdout

4. Two-dimensional array: a table with rows and columns

When one row is not enough, the matrix appears. A matrix is an array with two dimensions. To access its data you need two indices: one for the row and one for the column.

int matriz[2][3] = {
  {1, 2, 3},
  {4, 5, 6}
};

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

That access returns <strong>6</strong>, because row 1 is the second row and column 2 is the third column.

Matrix visualization

[0][0]1
[0][1]2
[0][2]3
[1][0]4
[1][1]5
[1][2]6

Think of it as a table: first you choose the row, then the column. If you mix up that order, you get lost.

5. When to use an array and when a matrix?

Simple array

  • List of ages.
  • Student grades.
  • Week temperatures.

Matrix

  • Grades of several students in several subjects.
  • Game board.
  • Seats in rows and columns.

Simple rule

If the data can be imagined as a single list, use a one-dimensional array. If the data is organized like a table, use a matrix.

6. Typical beginner mistakes

Error 1: thinking that if an array has 5 elements, the last index is 5. NO. The last index is 4.
Error 2: using a position that does not exist, for example numeros[5] in an array of 5 elements.
Error 3: believing that a matrix is accessed with a single index. You need two: row and column.
Error 4: not understanding that all array elements must be of the same type.

7. Final idea

An array in C is not just "many data together". It is an orderly way of representing related information. When you understand that each data has a position and that position is accessed by index, you start thinking about problems better.

And when a second dimension appears, the idea does not change: you simply go from a list to a table. No magic. Just data organization.

What you should take away

  • An array stores several data of the same type.
  • The index starts at 0.
  • A matrix uses row and column.
  • Without understanding indexes, everything later becomes complicated.