Arrays in C
Subject: Data Structures and Algorithms · Language: C · Level: beginner · 6 min read
Learn one-dimensional arrays, traversal, insertion-style updates, and common coding patterns.
An array is a collection of values of the same data type stored in continuous memory locations. Instead of creating many variables like `mark1`, `mark2`, `mark3`, we can store all marks in one array.
For example, `int marks[5];` creates an integer array that can store 5 integer values.
Why Arrays Are Important
Arrays are used when we need to store and process multiple values together. They are very important in searching, sorting, matrices, strings, and almost every DSA topic.
When to Use an Array
Use an array when all values are of the same type and you know the maximum number of values you need to store.
Examples include marks of students, prices of products, scores in a game, or numbers used in searching and sorting.
Array Declaration
Array declaration means creating an array with a fixed size.
```c
int numbers[5];
```
This creates an array named `numbers` that can store 5 integers.
Array Initialization
Array initialization means giving values to the array while declaring it.
```c
int numbers[5] = {10, 20, 30, 40, 50};
```
C also allows this shorter form when values are already given.
```c
int numbers[] = {10, 20, 30, 40, 50};
```
Array Indexing
Array indexing starts from 0 in C. This is very important.
- First element is at index `0`
- Second element is at index `1`
- Last element of an array of size `n` is at index `n - 1`
```c
printf("%d", numbers[0]); // prints first element
printf("%d", numbers[4]); // prints fifth element
```
Updating Array Elements
Array elements can be changed using their index.
```c
numbers[2] = 99;
```
This changes the third element because index counting starts from 0.
Reading Array Elements
We usually use a loop to take array input from the user.
```c
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
```
Here, `&arr[i]` gives the address of the current array element, so `scanf` can store the input there.
Traversing an Array
Traversing means visiting each element one by one.
```c
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
```
```c
#include <stdio.h>
int main(void) {
int n;
int arr[100];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = 0;
int max = arr[0];
int min = arr[0];
for (int i = 0; i < n; i++) {
sum += arr[i];
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
printf("Sum = %d\n", sum);
printf("Maximum = %d\n", max);
printf("Minimum = %d\n", min);
return 0;
}
```
Arrays and Loops Work Together
Most array problems are solved using loops because the same operation must be repeated for every element.
- To print all elements, visit every index from `0` to `n - 1`
- To find a sum, keep adding each element to a total
- To find maximum or minimum, compare each element with the current best value
Passing Arrays to Functions
When an array is passed to a function, the function receives access to the original array elements.
```c
void printArray(int arr[], int n)
```
Because the function can access the same elements, changes made inside the function can affect the original array.
Array Memory Idea
Array elements are stored side by side in memory. If `arr[0]` is the first element, then `arr[1]` comes next, then `arr[2]`, and so on. This is why indexing is fast in arrays.
Two-Dimensional Arrays
A two-dimensional array stores data in rows and columns. It is commonly used for matrices, tables, and grid problems.
```c
int matrix[2][3];
```
This creates 2 rows and 3 columns.
Important Rule
Do not access an index outside the array size. If an array has 5 elements, valid indexes are 0 to 4. Accessing `arr[5]` is wrong.
Complexity
Traversing an array takes O(n) time because each element is visited once. The extra space used by this example is O(1), apart from the input array itself.
Array Problem Patterns
- Find sum or average
- Find maximum or minimum
- Count even and odd numbers
- Search for a value
- Reverse the array
- Sort the array
- Use prefix sums for fast range queries
Practice Question
Write a C program that takes `n` numbers as input and counts how many numbers are even and how many numbers are odd.