Definition: An array is a collection of elements of the same data type stored in contiguous memory locations. Each element is identified using an index.
Definition: An array is a collection of elements of the same data type stored in contiguous memory locations. Each element is identified using an index.
int marks[5];
creates an array named marks that can store
5 integer values.
Suppose we need to store the marks of five students. Without an array, we would need five separate variables.
int mark1 = 78;
int mark2 = 65;
int mark3 = 91;
int mark4 = 54;
int mark5 = 82;
An array allows us to store all these values using one variable name.
int marks[5];
can store all five marks using the single name marks.
Definition: A one-dimensional array is a linear collection of elements arranged in a single sequence. Each element is accessed using one index.
int marks[5] = {78, 65, 91, 54, 82};
stores five integer values in one-dimensional form.
Definition: Array declaration tells the compiler the data type, name and number of elements of the array.
data_type array_name[size];
int marks[5];
declares an integer array named marks with 5 elements.
Definition: Array initialization means assigning initial values to the array elements.
int marks[5] = {78, 65, 91, 54, 82};
| Declaration | Initialization |
|---|---|
| Creates/defines the array. | Assigns initial values. |
int marks[5]; |
int marks[5] = {78,65,91,54,82}; |
Definition: The size of an array is the total number of elements it can store.
int marks[5];, the array size is
5.
Remember that array size and last index are different. For an array of size 5, the last valid index is 4.
Definition: Indexing is the method of identifying and accessing individual elements of an array using their position. In C, array indexing starts from 0.
int marks[5], the valid indexes are
0, 1, 2, 3 and 4.
Array: marks
Index: 0 1 2 3 4
↓ ↓ ↓ ↓ ↓
Value: 78 65 91 54 82
n elements, its valid indexes are
0 to n-1.
Definition: In a one-dimensional array, the first
element is always at index 0 and the last element is
at index size - 1.
int marks[5]:Definition: An individual array element is accessed by writing the array name followed by its index inside square brackets.
marks[0]
marks[1]
marks[2]
marks[0] = 78, then marks[0]
returns the first element, which is 78.
Definition: An array element can be changed by assigning a new value to its index.
marks[2] = 95;
marks[2] = 95; changes it to 95.
Definition: Traversing an array means visiting each element of the array one by one.
for loop can be used to visit every element from
index 0 to the last index.
for (int i = 0; i < 5; i++)
{
printf("%d ", marks[i]);
}
Definition: Array elements can be read from the user
one by one using a loop and scanf().
scanf("%d", &marks[i]);
stores the entered value at the current index.
Definition: Array elements can be displayed by accessing each index, usually with the help of a loop.
printf("%d ", marks[i]);
displays the current array element.
Definition: Array elements are stored in consecutive memory locations, which makes sequential access efficient.
Definition: An array can be initialized with fewer values than its declared size. The remaining elements are initialized to zero when the array has static initialization in a declaration such as this.
int marks[5] = {78, 65};
A simple way to initialize all elements of an array to zero is to provide the first element as zero and let the remaining elements be initialized to zero.
int marks[5] = {0};
Arrays and loops are commonly used together because a loop can process every element using its index.
i = 0 to i < 5.
Write a C program to accept 5 integers from the user and display all the elements of the one-dimensional array.
Use an integer array of size 5. Use one for loop to
read the elements and another loop to display them.
Array elements are accessed using indexes starting from 0. A loop provides a convenient way to process each element in sequence.
#include <stdio.h>
int main()
{
int marks[5];
int i;
printf("Enter 5 integers: ");
// read each array element
for (i = 0; i < 5; i++)
scanf("%d", &marks[i]);
printf("Array elements: ");
// display each array element
for (i = 0; i < 5; i++)
printf("%d ", marks[i]);
return 0;
}
Enter 5 integers: 78 65 91 54 82 Array elements: 78 65 91 54 82
Remember that for an array of size 5, the indexes are only
0 through 4. Accessing
marks[5] is outside the valid range.
Definition: An out-of-bounds access occurs when a program tries to access an index outside the valid range of the array.
int marks[5], marks[5] is invalid
because the last valid element is marks[4].
| Mistake | Correct Understanding |
|---|---|
| Starting index from 1 | C arrays start from index 0. |
| Using size as last index | Last valid index is size - 1. |
| Using different data types in one array | All elements normally have the same data type. |
| Using an invalid index | Use only valid indexes from 0 to size - 1. |
| Wrong loop limit | For size 5, use indexes 0 to 4. |
Definition: The sum of an array is obtained by adding all its elements together.
{10, 20, 30},
sum = 10 + 20 + 30 = 60.
int sum = 0;
for (int i = 0; i < 3; i++)
sum += arr[i];
Definition: The average of array elements is the sum of all elements divided by the number of elements.
{10, 20, 30},
average = 60 / 3 = 20.
Definition: The largest element is the element having the greatest value among all array elements.
{12, 45, 23, 67}, the largest element is
67.
Definition: The smallest element is the element having the lowest value among all array elements.
{12, 45, 23, 67}, the smallest element is
12.
Definition: Searching an array means checking its elements to determine whether a particular value is present.
{10, 25, 30, 45}, searching for
30 finds the value at index 2.
Write a C program to accept 5 numbers in a one-dimensional array and calculate their sum and average.
Initialize sum to 0. Add every array element to
sum, then divide the sum by 5.0.
The sum of array elements can be calculated by traversing the array and adding each element to a running total. The average is obtained by dividing the total by the number of elements.
#include <stdio.h>
int main()
{
int arr[5];
int i, sum = 0;
float average;
printf("Enter 5 numbers: ");
// read array elements
for (i = 0; i < 5; i++)
scanf("%d", &arr[i]);
// calculate the sum
for (i = 0; i < 5; i++)
sum += arr[i];
average = sum / 5.0; // use 5.0 for floating-point division
printf("Sum = %d
", sum);
printf("Average = %.2f
", average);
return 0;
}
Enter 5 numbers: 10 20 30 40 50 Sum = 150 Average = 30.00
When a fractional average is required, use a floating-point divisor
such as 5.0 instead of 5 to avoid
unintended integer division.
| Concept | Remember |
|---|---|
| Array | Collection of same-type elements. |
| 1-D Array | Linear collection using one index. |
| Indexing | Starts from 0 in C. |
| Last Index | size - 1 |
| Access | array[index] |
| Traversal | Visit every element one by one. |
| Common Tool | Loops are commonly used with arrays. |
| Invalid Access | Index outside 0 to size - 1 is invalid. |
Watch a beginner-friendly explanation of one-dimensional arrays, indexing and traversal in C.
A short handwritten-style revision sheet for one-dimensional arrays and indexing will be provided here.
Use the mind map for quick revision of declaration, initialization, indexing, traversal and basic array operations.