C Programming

Chapter 6 – Arrays in C Language

Chapter 6 – Arrays in C Language

Introduction

While writing programs, we often need to store multiple values of the same type. For example, a program may need to store the marks of 50 students or the prices of 20 products.

Creating a separate variable for every value would make the program difficult to manage. Arrays provide a convenient way to store multiple values using a single variable name.

In this chapter, you will learn what arrays are, how to declare and initialize them, how to access their elements, and how to process arrays using loops.

1. What is an Array?

An array is a collection of elements of the same data type stored under a single variable name.

For example:

int marks[5];

This creates an integer array capable of storing 5 integer values.

An array can be visualized as:

marks +----+----+----+----+----+ | 10 | 20 | 30 | 40 | 50 | +----+----+----+----+----+  0    1    2    3    4

The numbers below the elements are called index positions.

2. Why Do We Use Arrays?

Suppose we want to store five numbers without an array:

int n1, n2, n3, n4, n5;

With an array, we can write:

int numbers[5];

Arrays make programs:

Shorter

Easier to understand

Easier to process using loops

More convenient for storing collections of similar data

3. Declaring an Array

The general syntax for declaring an array is:

data_type array_name[size];

Example

int marks[10];

Here:

int is the data type.

marks is the array name.

10 is the number of elements.

Other examples:

float price[20]; char grade[5]; double salary[10];

4. Array Index

C uses zero-based indexing.

This means the first element has index 0, not 1.

For an array containing five elements:

int numbers[5];

The valid indexes are:

0   1   2   3   4

The fifth element is therefore stored at index 4.

Example

int numbers[5]; numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50;

5. Initializing an Array

An array can be initialized when it is declared.

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

The values are assigned to the indexes in order.

numbers[0] = 10 numbers[1] = 20 numbers[2] = 30 numbers[3] = 40 numbers[4] = 50

6. Automatic Array Size

The size can be omitted when values are provided during initialization.

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

The compiler determines the required size from the number of elements.

7. Accessing Array Elements

An individual array element can be accessed using its index.

#include <stdio.h> int main() {    int marks[5] = {75, 82, 68, 90, 88};    printf("%d", marks[0]);    return 0; }

Output

75

To access the third element:

printf("%d", marks[2]);

Output:

68

8. Changing an Array Element

An existing value can be changed by assigning a new value to its index.

#include <stdio.h> int main() {    int numbers[5] = {10, 20, 30, 40, 50};    numbers[2] = 100;    printf("%d", numbers[2]);    return 0; }

Output

100

The original value 30 at index 2 has been replaced by 100.

9. Input in an Array

We can use scanf() to take array values from the user.

#include <stdio.h> int main() {    int numbers[5];    int i;    for(i = 0; i < 5; i++)    {        printf("Enter number %d: ", i + 1);        scanf("%d", &numbers[i]);    }    return 0; }

The loop allows us to enter all five values without writing five separate scanf() statements.

10. Displaying Array Elements

A loop can also be used to display all elements.

#include <stdio.h> int main() {    int numbers[5] = {10, 20, 30, 40, 50};    int i;    for(i = 0; i < 5; i++)    {        printf("%d ", numbers[i]);    }    return 0; }

Output

10 20 30 40 50

11. Finding the Sum of Array Elements

Arrays and loops are frequently used together.

#include <stdio.h> int main() {    int numbers[5] = {10, 20, 30, 40, 50};    int i;    int sum = 0;    for(i = 0; i < 5; i++)    {        sum = sum + numbers[i];    }    printf("Sum = %d", sum);    return 0; }

Output

Sum = 150

12. Finding the Average

We can calculate the average of array elements using:

Average = Sum / Number of Elements

Example

#include <stdio.h> int main() {    int marks[5] = {80, 75, 90, 85, 70};    int i;    int sum = 0;    float average;    for(i = 0; i < 5; i++)    {        sum = sum + marks[i];    }    average = (float)sum / 5;    printf("Average = %.2f", average);    return 0; }

Output

Average = 80.00

The cast (float) ensures that the division can produce a decimal result.

13. Finding the Largest Element

An array can be searched to find its largest value.

#include <stdio.h> int main() {    int numbers[5] = {25, 72, 18, 91, 46};    int i;    int largest = numbers[0];    for(i = 1; i < 5; i++)    {        if(numbers[i] > largest)        {            largest = numbers[i];        }    }    printf("Largest = %d", largest);    return 0; }

Output

Largest = 91

14. Finding the Smallest Element

The same approach can be used to find the smallest value.

#include <stdio.h> int main() {    int numbers[5] = {25, 72, 18, 91, 46};    int i;    int smallest = numbers[0];    for(i = 1; i < 5; i++)    {        if(numbers[i] < smallest)        {            smallest = numbers[i];        }    }    printf("Smallest = %d", smallest);    return 0; }

Output

Smallest = 18

15. Searching an Array

We can search for a particular value using a loop.

#include <stdio.h> int main() {    int numbers[5] = {10, 20, 30, 40, 50};    int search;    int i;    int found = 0;    printf("Enter value to search: ");    scanf("%d", &search);    for(i = 0; i < 5; i++)    {        if(numbers[i] == search)        {            found = 1;            break;        }    }    if(found)    {        printf("Value found");    }    else    {        printf("Value not found");    }    return 0; }

This is a basic example of linear search.

16. One-Dimensional Array

A one-dimensional array stores values in a single sequence.

Example:

int marks[5];

It can be represented as:

Index:    0    1    2    3    4          ↓    ↓    ↓    ↓    ↓ Value:   70   80   65   90   75

One-dimensional arrays are commonly used for:

Marks

Prices

Ages

Scores

Temperatures

Lists of numbers

17. Two-Dimensional Arrays

A two-dimensional array stores data in rows and columns.

It is useful for representing tables and matrices.

Declaration

int matrix[3][3];

This creates an array with:

3 rows

3 columns

9 total elements

It can be visualized as:

+----+----+----+ | 1  | 2  | 3  | +----+----+----+ | 4  | 5  | 6  | +----+----+----+ | 7  | 8  | 9  | +----+----+----+

18. Initializing a Two-Dimensional Array

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

Here:

Rows = 2 Columns = 3 Total elements = 6

19. Displaying a Two-Dimensional Array

Nested loops are commonly used with two-dimensional arrays.

#include <stdio.h> int main() {    int matrix[2][3] =    {        {1, 2, 3},        {4, 5, 6}    };    int i, j;    for(i = 0; i < 2; i++)    {        for(j = 0; j < 3; j++)        {            printf("%d ", matrix[i][j]);        }        printf("\n");    }    return 0; }

Output

1 2 3 4 5 6

20. Character Arrays

Arrays can also store characters.

char name[6] = {'G', 'W', 'A', 'L', 'N', 'E'};

Character arrays are closely related to strings in C.

A string is normally terminated by a special character called the null character:

'\0'

For example:

char name[] = "GWALNET";

The compiler automatically includes the terminating null character.

21. Array Size and Valid Indexes

If an array is declared as:

int numbers[10];

then it contains 10 elements.

The valid indexes are:

0 to 9

The following is incorrect:

numbers[10]

because index 10 is outside the valid range.

Always remember:

For an array of size N, valid indexes are 0 through N-1.

22. Advantages of Arrays

Arrays provide several advantages:

Multiple values can be stored under one variable name.

Elements can be accessed using indexes.

Loops can process large collections of data efficiently.

Arrays make programs more organized.

They are useful for mathematical and data-processing tasks.

23. Limitations of Arrays

Traditional C arrays also have some limitations:

Their size is normally fixed after declaration.

All elements of an array have the same data type.

Accessing an invalid index can cause unexpected behavior.

Managing very large collections may require other data structures.

24. Common Mistakes

Mistake 1: Using an invalid index

int numbers[5]; numbers[5] = 100;

The valid indexes are only 0 through 4.

Mistake 2: Forgetting zero-based indexing

The first element is:

numbers[0]

not:

numbers[1]

Mistake 3: Incorrect loop condition

For five elements:

for(i = 0; i < 5; i++)

is appropriate.

25. Array and Loop Relationship

Arrays become especially powerful when combined with loops.

For example:

int numbers[100];

Instead of manually accessing every element, a loop can process all 100 elements:

for(i = 0; i < 100; i++) {    printf("%d ", numbers[i]); }

This combination is one of the most frequently used techniques in C programming.

26. Quick Revision

ConceptMeaning
ArrayCollection of elements of the same data type
IndexPosition of an element
First index0
Last indexsize - 1
1D ArrayData stored in a single sequence
2D ArrayData stored in rows and columns
Nested LoopLoop inside another loop

27. Practice Questions

Multiple Choice Questions

1. Which index represents the first element of a C array?

A. 0
B. 1
C. -1
D. 10

Answer: A. 0

2. Which declaration creates an integer array containing 10 elements?

A. int numbers;
B. int numbers[10];
C. array int numbers[10];
D. integer numbers[10];

Answer: B. int numbers[10];

3. How many elements are present in int a[5]?

A. 4
B. 5
C. 6
D. 10

Answer: B. 5

4. What is the last valid index of int a[5]?

A. 5
B. 4
C. 3
D. 1

Answer: B. 4

5. Which type of array stores data in rows and columns?

A. One-dimensional array
B. Two-dimensional array
C. Character variable
D. Pointer

Answer: B. Two-dimensional array

28. Programming Exercises

Try writing C programs to:

Store and display 10 integers.

Find the sum of all elements in an array.

Find the average of five numbers.

Find the largest element in an array.

Find the smallest element in an array.

Count even and odd numbers in an array.

Search for a value in an array.

Reverse the elements of an array.

Add two two-dimensional matrices.

Display the elements of a 3 × 3 matrix.

Chapter Summary

An array is a collection of elements of the same data type stored using a single variable name. C uses zero-based indexing, meaning the first element is stored at index 0.

Arrays can be one-dimensional or multi-dimensional. Loops are commonly used to input, display, search, and process array elements.

Understanding arrays is essential before moving to more advanced topics such as strings, functions, pointers, and data structures.

Key Takeaways

Arrays store multiple values of the same data type.

Array indexing starts from 0.

A one-dimensional array stores values in a sequence.

A two-dimensional array stores values in rows and columns.

Loops make array processing easier.

Always use valid array indexes.