πŸ’©

programierds

c / operaciones / arreglos

Array Operations

We're not just repeating definitions here. You'll see, visually, how an array is traversed, how a value is searched for, and how a new element is inserted by moving positions. The idea is for you to understand WHAT the program does and WHY it does it.

Traversalvisit position by position
Searchfind a value within the array
Insertionmake room and add data
General view: an array is an ordered sequence of positions
[0]10
[1]20
[2]30
[3]40
[4]50
Same structure

It's always the same array. What changes is the operation we perform on it.

Same index

The index variable tells us which position we are working with.

Same order

Almost all operations start by traversing from the beginning toward the end.

1. Traversing an Array

Traversing means visiting all elements one by one. It is the base operation: without traversal you cannot display, sum, compare, or search.

Step-by-step traversal simulation
int numeros[5] = {10, 20, 30, 40, 50}; int i; for (i = 0; i < 5; i = i + 1) {   printf("Posicion %d -> %d\n", i, numeros[i]); }
Step 1: The array is declared with 5 values.
Step 1 / 7
Current state
[0]10
[1]20
[2]30
[3]40
[4]50
current i-
value read-
./recorridostdout
Important idea: in a traversal you don't "magically skip" to the data. The program goes through each position using index i.

2. Searching for a Value in the Array

Searching means comparing the searched value with each element of the array until a match is found or until the traversal ends.

Sequential search simulation
int numeros[6] = {8, 14, 21, 14, 35, 42}; int buscado = 14; int posicion = -1; for (i = 0; i < 6; i = i + 1) {   if (numeros[i] == buscado) {     posicion = i;     break;   } }
Step 1: The array and the value we want to find are prepared.
Step 1 / 6
Comparison visualization
[0]8
[1]14
[2]21
[3]14
[4]35
[5]42
searched14
current i-
found position-1
We use -1 because no valid array position can be negative. So -1 means "I haven't found anything yet".

3. Inserting a Value in the Middle

Inserting is not simply "putting a value". If the position is already occupied, elements must first be shifted to the right to make room.

Insertion simulation with shifting
int numeros[6] = {10, 20, 40, 50}; int cantidad = 4; int posicion = 2; int nuevo = 30; for (i = cantidad; i > posicion; i = i - 1) {   numeros[i] = numeros[i - 1]; } numeros[posicion] = nuevo; cantidad = cantidad + 1;
Step 1: There is room for 6 elements, but the array is using 4.
Step 1 / 7
Shift visualization
[0]10
[1]20
[2]40
[3]50
[4]-
[5]-
current count4
insertion position2
new value30
Common error: moving from left to right. That overwrites values. To insert in the middle, the shift is done from the end toward the insertion position.

4. Key Takeaways

Traverse

It's going through each position using an index. Without that, there's no real array processing.

Search

It's comparing one by one until you find the data or finish the traversal.

Insert

It's making room and only then saving the new value.