C Programming • Arrays & Functions
C Programming / Arrays (1-D)

Arrays (1-D)

Notes 3 Arrays & Functions

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.

Notes

Arrays (1-D)

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.

💡 Example: int marks[5]; creates an array named marks that can store 5 integer values.

Why Do We Need an Array?

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.

💡 Example: int marks[5]; can store all five marks using the single name marks.

What is a One-Dimensional Array?

Definition: A one-dimensional array is a linear collection of elements arranged in a single sequence. Each element is accessed using one index.

💡 Example: int marks[5] = {78, 65, 91, 54, 82}; stores five integer values in one-dimensional form.

Characteristics of an Array

  • All elements have the same data type.
  • Elements are stored in a continuous sequence of memory locations.
  • Each element is accessed using an index.
  • Array indexing in C starts from 0.
  • The size of a normally declared fixed-size array is determined at declaration.

Array Declaration

Definition: Array declaration tells the compiler the data type, name and number of elements of the array.

data_type array_name[size];
💡 Example: int marks[5]; declares an integer array named marks with 5 elements.

Array Initialization

Definition: Array initialization means assigning initial values to the array elements.

int marks[5] = {78, 65, 91, 54, 82};
💡 Example: Here the five elements are initialized with 78, 65, 91, 54 and 82.

Declaration vs Initialization

Declaration Initialization
Creates/defines the array. Assigns initial values.
int marks[5]; int marks[5] = {78,65,91,54,82};

Array Size

Definition: The size of an array is the total number of elements it can store.

💡 Example: In 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.

Indexing in an Array

Definition: Indexing is the method of identifying and accessing individual elements of an array using their position. In C, array indexing starts from 0.

💡 Example: For int marks[5], the valid indexes are 0, 1, 2, 3 and 4.

Array with Indexes

Array:    marks

Index:      0    1    2    3    4
            ↓    ↓    ↓    ↓    ↓

Value:     78   65   91   54   82
📌 Important: If an array contains n elements, its valid indexes are 0 to n-1.

First and Last Index

Definition: In a one-dimensional array, the first element is always at index 0 and the last element is at index size - 1.

💡 Example: For int marks[5]:

First index = 0
Last index = 4

Accessing Array Elements

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]
💡 Example: If marks[0] = 78, then marks[0] returns the first element, which is 78.

Modifying an Array Element

Definition: An array element can be changed by assigning a new value to its index.

marks[2] = 95;
💡 Example: If the third element was 91, the statement marks[2] = 95; changes it to 95.

Traversing an Array

Definition: Traversing an array means visiting each element of the array one by one.

💡 Example: A 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]);
}

Taking Array Input

Definition: Array elements can be read from the user one by one using a loop and scanf().

💡 Example: scanf("%d", &marks[i]); stores the entered value at the current index.

Displaying Array Elements

Definition: Array elements can be displayed by accessing each index, usually with the help of a loop.

💡 Example: printf("%d ", marks[i]); displays the current array element.

Array and Memory

Definition: Array elements are stored in consecutive memory locations, which makes sequential access efficient.

💡 Example: If the first element is stored at one memory location, the next element is stored in the next appropriate memory location for that data type.

Array Initialization with Fewer Values

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};
💡 Example: The first two elements receive 78 and 65, while the remaining elements are initialized to 0 in this declaration.

Initializing All Elements with Zero

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};
💡 Example: The resulting array values are: 0, 0, 0, 0, 0.

Array and Loop

Arrays and loops are commonly used together because a loop can process every element using its index.

💡 Example: To print all five elements, the loop can run from i = 0 to i < 5.

Practical Example — Read and Display Array Elements

Problem Statement

Write a C program to accept 5 integers from the user and display all the elements of the one-dimensional array.

Learning Outcomes

  • Declare and initialize a one-dimensional array.
  • Use a loop to accept array elements.
  • Use indexes to access and display array elements.

Hint

Use an integer array of size 5. Use one for loop to read the elements and another loop to display them.

Theory

Array elements are accessed using indexes starting from 0. A loop provides a convenient way to process each element in sequence.

Program

#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;
}

Expected Output

Enter 5 integers: 78 65 91 54 82
Array elements: 78 65 91 54 82

Note

Remember that for an array of size 5, the indexes are only 0 through 4. Accessing marks[5] is outside the valid range.

Array Index Out of Bounds

Definition: An out-of-bounds access occurs when a program tries to access an index outside the valid range of the array.

💡 Example: For int marks[5], marks[5] is invalid because the last valid element is marks[4].

Common Mistakes in Arrays

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.

Finding Sum of Array Elements

Definition: The sum of an array is obtained by adding all its elements together.

💡 Example: For {10, 20, 30}, sum = 10 + 20 + 30 = 60.
int sum = 0;

for (int i = 0; i < 3; i++)
    sum += arr[i];

Finding Average of Array Elements

Definition: The average of array elements is the sum of all elements divided by the number of elements.

💡 Example: For {10, 20, 30}, average = 60 / 3 = 20.

Finding Largest Element

Definition: The largest element is the element having the greatest value among all array elements.

💡 Example: In {12, 45, 23, 67}, the largest element is 67.

Finding Smallest Element

Definition: The smallest element is the element having the lowest value among all array elements.

💡 Example: In {12, 45, 23, 67}, the smallest element is 12.

Searching an Element

Definition: Searching an array means checking its elements to determine whether a particular value is present.

💡 Example: In {10, 25, 30, 45}, searching for 30 finds the value at index 2.

Practical Example — Find Sum and Average

Problem Statement

Write a C program to accept 5 numbers in a one-dimensional array and calculate their sum and average.

Learning Outcomes

  • Store multiple values in a one-dimensional array.
  • Traverse an array using a loop.
  • Calculate sum and average from array elements.

Hint

Initialize sum to 0. Add every array element to sum, then divide the sum by 5.0.

Theory

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.

Program

#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;
}

Expected Output

Enter 5 numbers: 10 20 30 40 50
Sum = 150
Average = 30.00

Note

When a fractional average is required, use a floating-point divisor such as 5.0 instead of 5 to avoid unintended integer division.

Quick Revision

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.

Important Exam Questions

Short Answer Questions

  1. What is an array?
  2. What is a one-dimensional array?
  3. Why are arrays used in C?
  4. Write the syntax for declaring an array.
  5. What is array indexing?
  6. Why does array indexing start from 0 in C?
  7. What is the last valid index of an array of size 10?
  8. What is array traversal?
  9. What is an out-of-bounds array access?
  10. How are array elements accessed?

Long Answer Questions

  1. Define an array and explain one-dimensional arrays with examples.
  2. Explain array declaration, initialization and indexing in C.
  3. Explain how to access, modify and traverse a one-dimensional array.
  4. Write a C program to read and display elements of a one-dimensional array.
  5. Write a C program to find the sum and average of array elements.
  6. Explain common mistakes while working with one-dimensional arrays.
🎥 Recommended Learning

Watch a beginner-friendly explanation of one-dimensional arrays, indexing and traversal in C.

▶ Watch: 1-D Arrays in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for one-dimensional arrays and indexing will be provided here.

🧠 Mind Map

Use the mind map for quick revision of declaration, initialization, indexing, traversal and basic array operations.