Lesson 40 of 60 – One-Dimensional Array in C
67%

One-Dimensional Array in C

A one-dimensional array stores multiple values of the same data type in a single sequence. The elements are arranged in one row and can be accessed using a single index.

Note: C arrays use zero-based indexing. Therefore, the first element is at index 0, and the last element of an array of size n is at index n - 1.

1. What is a One-Dimensional Array?

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

int numbers[5];

This array can store five integer values.

Index:   0    1    2    3    4
Value:  [ ]  [ ]  [ ]  [ ]  [ ]

2. Declaration of a 1D Array

The syntax for declaring a one-dimensional array is:

data_type array_name[size];

Example:

int marks[5];
  • int is the data type.
  • marks is the array name.
  • 5 is the number of elements.

3. Indexing in a 1D Array

A one-dimensional array uses one index to identify each element.

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

The indexes are:

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

4. Initializing a 1D Array

A one-dimensional array can be initialized when it is declared.

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

The values are stored in the array from left to right.

5. Array Size Can Be Omitted

When an array is initialized with values, its size can be omitted.

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

The compiler determines that the array contains five elements.

6. Accessing the First Element

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

printf("%d", numbers[0]);

Output:

10

The index 0 accesses the first element.

7. Accessing the Last Element

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

printf("%d", numbers[3]);

Output:

40

Because the array has four elements, the last valid index is 3.

8. Changing an Array Element

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

numbers[1] = 200;

printf("%d", numbers[1]);

Output:

200

An array element can be modified using its index.

9. Displaying All Elements

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

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

Output:

10 20 30 40 50

A for loop is commonly used to traverse a one-dimensional array.

10. Taking Input into a 1D Array

int numbers[5];

for (int i = 0; i < 5; i++)
{
    scanf("%d", &numbers[i]);
}

Each input value is stored at a different array index.

11. Complete Input and Display Program

#include <stdio.h>

int main()
{
    int numbers[5];

    printf("Enter 5 numbers:\n");

    for (int i = 0; i < 5; i++)
    {
        scanf("%d", &numbers[i]);
    }

    printf("Array elements:\n");

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

    return 0;
}

12. Sum of 1D Array Elements

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

for (int i = 0; i < 5; i++)
{
    sum += numbers[i];
}

printf("Sum = %d", sum);

Output:

Sum = 150

13. Finding the Average

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

for (int i = 0; i < 5; i++)
{
    sum += numbers[i];
}

float average = sum / 5.0f;

printf("Average = %.2f", average);

Output:

Average = 30.00

14. Finding the Largest Element

int numbers[] = {25, 60, 15, 90, 40};

int largest = numbers[0];

for (int i = 1; i < 5; i++)
{
    if (numbers[i] > largest)
    {
        largest = numbers[i];
    }
}

printf("Largest = %d", largest);

Output:

Largest = 90

15. Finding the Smallest Element

int numbers[] = {25, 60, 15, 90, 40};

int smallest = numbers[0];

for (int i = 1; i < 5; i++)
{
    if (numbers[i] < smallest)
    {
        smallest = numbers[i];
    }
}

printf("Smallest = %d", smallest);

Output:

Smallest = 15

16. Searching an Element

A loop can be used to search for a value in a one-dimensional array.

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

for (int i = 0; i < 5; i++)
{
    if (numbers[i] == search)
    {
        found = 1;
        break;
    }
}

if (found)
{
    printf("Element found");
}
else
{
    printf("Element not found");
}

17. Counting Even Numbers

int numbers[] = {10, 15, 20, 25, 30};
int count = 0;

for (int i = 0; i < 5; i++)
{
    if (numbers[i] % 2 == 0)
    {
        count++;
    }
}

printf("Even numbers = %d", count);

The modulo operator checks whether each element is divisible by 2.

18. Counting Odd Numbers

int numbers[] = {10, 15, 20, 25, 30};
int count = 0;

for (int i = 0; i < 5; i++)
{
    if (numbers[i] % 2 != 0)
    {
        count++;
    }
}

printf("Odd numbers = %d", count);

19. Reversing a 1D Array

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

for (int i = 4; i >= 0; i--)
{
    printf("%d ", numbers[i]);
}

Output:

50 40 30 20 10

The array is traversed from the last index to the first index.

20. Copying One Array to Another

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

for (int i = 0; i < 5; i++)
{
    destination[i] = source[i];
}

for (int i = 0; i < 5; i++)
{
    printf("%d ", destination[i]);
}

Each element is copied individually using a loop.

21. Comparing Two 1D Arrays

int a[] = {10, 20, 30};
int b[] = {10, 20, 30};
int same = 1;

for (int i = 0; i < 3; i++)
{
    if (a[i] != b[i])
    {
        same = 0;
        break;
    }
}

if (same)
{
    printf("Arrays are equal");
}
else
{
    printf("Arrays are different");
}

Elements are compared one by one.

22. Finding the Number of Elements

When the array is available in its original scope, the number of elements can be calculated using sizeof.

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

int size = sizeof(numbers) / sizeof(numbers[0]);

printf("Size = %d", size);

Output:

Size = 5

23. Passing a 1D Array to a Function

#include <stdio.h>

void display(int numbers[], int size)
{
    for (int i = 0; i < size; i++)
    {
        printf("%d ", numbers[i]);
    }
}

int main()
{
    int numbers[] = {10, 20, 30, 40};

    display(numbers, 4);

    return 0;
}

The array and its element count are passed to the function.

24. Sum of Array Using a Function

#include <stdio.h>

int arraySum(int numbers[], int size)
{
    int sum = 0;

    for (int i = 0; i < size; i++)
    {
        sum += numbers[i];
    }

    return sum;
}

int main()
{
    int numbers[] = {10, 20, 30, 40};

    int result = arraySum(numbers, 4);

    printf("Sum = %d", result);

    return 0;
}

25. Modifying a 1D Array Through a Function

#include <stdio.h>

void update(int numbers[], int size)
{
    for (int i = 0; i < size; i++)
    {
        numbers[i] *= 2;
    }
}

int main()
{
    int numbers[] = {10, 20, 30};

    update(numbers, 3);

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

    return 0;
}

Output:

20 40 60

26. Out-of-Bounds Access

Always use indexes within the valid range of the array.

int numbers[5];

numbers[0] = 10;
numbers[4] = 50;

/* Invalid */
numbers[5] = 60;

For an array of five elements, the valid indexes are 0 to 4. Accessing outside this range causes undefined behavior.

27. Complete Search Program

#include <stdio.h>

int main()
{
    int numbers[5];
    int search;
    int found = 0;

    printf("Enter 5 numbers:\n");

    for (int i = 0; i < 5; i++)
    {
        scanf("%d", &numbers[i]);
    }

    printf("Enter number to search: ");
    scanf("%d", &search);

    for (int i = 0; i < 5; i++)
    {
        if (numbers[i] == search)
        {
            found = 1;
            break;
        }
    }

    if (found)
    {
        printf("Element found");
    }
    else
    {
        printf("Element not found");
    }

    return 0;
}

28. Complete Largest and Smallest Program

#include <stdio.h>

int main()
{
    int numbers[5];

    printf("Enter 5 numbers:\n");

    for (int i = 0; i < 5; i++)
    {
        scanf("%d", &numbers[i]);
    }

    int largest = numbers[0];
    int smallest = numbers[0];

    for (int i = 1; i < 5; i++)
    {
        if (numbers[i] > largest)
        {
            largest = numbers[i];
        }

        if (numbers[i] < smallest)
        {
            smallest = numbers[i];
        }
    }

    printf("Largest = %d\n", largest);
    printf("Smallest = %d", smallest);

    return 0;
}

29. Common 1D Array Mistakes

  • Forgetting that indexing starts from zero.
  • Using an index greater than or equal to the array size.
  • Using the wrong loop limit.
  • Reading uninitialized local array elements.
  • Forgetting to pass the array size to a function when it is needed.
  • Confusing an array's total byte size with its number of elements.
int numbers[5];

for (int i = 0; i < 5; i++)
{
    scanf("%d", &numbers[i]);
}

Using the correct loop limit prevents accidental out-of-bounds access.

30. Practice Programs on One-Dimensional Arrays

Practice the following programs:

  1. Declare and display a one-dimensional integer array.
  2. Take five numbers from the user and display them.
  3. Find the sum of all elements.
  4. Find the average of all elements.
  5. Find the largest element.
  6. Find the smallest element.
  7. Count even numbers.
  8. Count odd numbers.
  9. Search for an element.
  10. Reverse the array while displaying its elements.
  11. Copy one array into another.
  12. Compare two arrays element by element.

Practice each problem using loops and array indexes.

📌 Key Points

  • A one-dimensional array stores values in a single sequence.
  • All elements of an array have the same data type.
  • Array indexing starts from 0.
  • The last valid index is one less than the array size.
  • A for loop is commonly used to traverse a 1D array.
  • Array elements can be accessed and modified using indexes.
  • Arrays can be passed to functions.
  • The array size is commonly passed separately to a function.
  • sizeof can be used to calculate the number of elements when the array is in its original scope.
  • Accessing an array outside its valid range causes undefined behavior.

🧠 Quick Quiz

Question: If an integer array has 5 elements, what is the index of its last element?