Skip to content
StudyFame

One-Dimensional Arrays in C

A one-dimensional (1D) array is a single row of values, like a row of lockers in a school corridor. It is the most common kind of array.

In this lesson you will take array values from the user and write the programs every exam asks: sum, largest, smallest, reverse and even/odd count. Once you know these patterns, most array questions become easy.

What You Will Learn

  • What a one-dimensional array is.
  • How to read array elements with scanf.
  • The "visit every element" loop pattern.
  • How to find the sum, largest and smallest element.
  • How to reverse an array in place.

What is a One-Dimensional Array?

Exam definition: A one-dimensional array is an array with only one subscript (one index). Its elements form a single row (a linear list).

Memory trick: One dimension = one pair of square brackets = one row. a[i] needs one number to find a box.

The pattern behind every 1D array program

for i = 0 to n-1
    do something with a[i]     (read it, print it, add it, compare it)

Only the "do something" part changes from program to program.

Syntax to Read and Print a 1D Array

int a[100], n, i;

scanf("%d", &n);                 /* how many elements (n <= 100) */
for (i = 0; i < n; i++)
    scanf("%d", &a[i]);          /* &a[i] = address of element i */

for (i = 0; i < n; i++)
    printf("%d ", a[i]);

We declare a bigger array (100) because the size must be known at compile time, then use only the first n elements.

Example 1: Read n Numbers and Find Sum, Largest and Smallest

#include <stdio.h>

int main(void)
{
    int a[100], n, i;
    int sum = 0, largest, smallest;

    printf("How many numbers? ");
    scanf("%d", &n);

    printf("Enter %d numbers: ", n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    largest = a[0];
    smallest = a[0];
    for (i = 0; i < n; i++)
    {
        sum = sum + a[i];
        if (a[i] > largest)
            largest = a[i];
        if (a[i] < smallest)
            smallest = a[i];
    }

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

    return 0;
}

Output

How many numbers? 5
Enter 5 numbers: 12 45 7 30 18
Sum      = 112
Largest  = 45
Smallest = 7

Code Explanation

  1. int a[100] reserves room for up to 100 numbers. The user decides n, how many to use.
  2. The first loop reads each number into a[i]. Note the &: scanf needs the address of the element.
  3. largest = a[0]; smallest = a[0]; starts both with the first element. Never start largest with 0: it fails when all numbers are negative.
  4. The second loop adds each element to sum and updates largest and smallest when it finds a bigger or smaller value.

See also: find maximum and minimum number in an array.

How Finding the Largest Element Works

Dry run with {12, 45, 7, 30, 18}:

ia[i]a[i] > largest?largest
012No (equal)12
145Yes45
27No45
330No45
418No45

Think of a "current champion": each new element challenges it, and the winner stays.

Example 2: Reverse an Array and Count Even and Odd Numbers

#include <stdio.h>

int main(void)
{
    int a[] = {10, 21, 32, 43, 54, 65};
    int n = sizeof(a) / sizeof(a[0]);
    int i, temp, even = 0, odd = 0;

    for (i = 0; i < n; i++)
    {
        if (a[i] % 2 == 0)
            even++;
        else
            odd++;
    }

    /* reverse in place: swap first with last, second with second-last ... */
    for (i = 0; i < n / 2; i++)
    {
        temp = a[i];
        a[i] = a[n - 1 - i];
        a[n - 1 - i] = temp;
    }

    printf("Reversed array:");
    for (i = 0; i < n; i++)
        printf(" %d", a[i]);
    printf("\n");

    printf("Even: %d, Odd: %d\n", even, odd);

    return 0;
}

Output

Reversed array: 65 54 43 32 21 10
Even: 3, Odd: 3
  • The partner of index i is index n - 1 - i.
  • The loop runs only n / 2 times. If it ran n times, every pair would be swapped twice and the array would come back unchanged.

Want a second array instead? See copy all elements of an array into another array.

Important Points to Remember

  • A 1D array has one index and looks like a single row.
  • Read elements with scanf("%d", &a[i]): don't forget the &.
  • Start largest/smallest with a[0], not with 0.
  • To reverse in place, swap a[i] with a[n-1-i] for i < n/2.
  • Always make sure n is not bigger than the declared size.

Common Mistakes Beginners Make

MistakeWhat happensFix
scanf("%d", a[i]) without &Crash or wrong valuesscanf("%d", &a[i])
largest = 0 at the startWrong answer for all-negative inputlargest = a[0]
Reverse loop runs to nArray ends up unchangedRun to n / 2
int a[n]; before reading nSize is garbageRead n first, or use a fixed big size
Not resetting sum = 0Garbage totalInitialize before the loop

1D Array Practice Questions

  1. What is a one-dimensional array? Give one real-life example.
  2. Why should largest start with a[0]?
  3. What is the partner index of i when reversing an array of size n?
  4. How many swaps are needed to reverse an array of 7 elements?
  5. Why do we often declare int a[100] when the user enters n?
Show answers
  1. An array with one index whose elements form a single row, for example the roll-number-wise marks of a class.
  2. So the answer is correct even when every number is negative (or every number is less than the starting value).
  3. n - 1 - i.
  4. 7 / 2 = 3 swaps (the middle element stays).
  5. The array size must be fixed at compile time, so we reserve a safe maximum and use only the first n elements.

Coding Exercise

Write a program that stores {4, 9, 2, 9, 5, 9, 1} in an array, then prints the position (index) of the first largest element and how many times the largest value appears.

Show solution
#include <stdio.h>

int main(void)
{
    int a[] = {4, 9, 2, 9, 5, 9, 1};
    int n = sizeof(a) / sizeof(a[0]);
    int i, pos = 0, count = 0;

    for (i = 1; i < n; i++)
    {
        if (a[i] > a[pos])
            pos = i;
    }
    for (i = 0; i < n; i++)
    {
        if (a[i] == a[pos])
            count++;
    }

    printf("Largest %d first found at index %d\n", a[pos], pos);
    printf("It appears %d times\n", count);
    return 0;
}
Largest 9 first found at index 1
It appears 3 times

Quick Quiz: One-Dimensional Arrays (MCQ)

  1. A one-dimensional array has how many subscripts?

    A) 0   B) 1   C) 2   D) Any

  2. Correct way to read element i:

    A) scanf("%d", a[i]);   B) scanf("%d", &a[i]);   C) scanf("%d", *a[i]);   D) scanf(a[i]);

  3. When reversing an array of size n, a[i] is swapped with:

    A) a[n-i]   B) a[n+i]   C) a[n-1-i]   D) a[i+1]

  4. Output of int a[] = {3, 8, 1}; int s = 0; for (int i = 0; i < 3; i++) s += a[i]; printf("%d", s);

    A) 3   B) 8   C) 12   D) 11

  5. Best starting value for smallest:

    A) 0   B) 100   C) a[0]   D) a[n]

Show answers
  1. B: 1.
  2. B: &a[i].
  3. C: a[n-1-i].
  4. C: 12.
  5. C: a[0].

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

1D arrayOne index, a single row
Read elementscanf("%d", &a[i])
Visit allfor (i = 0; i < n; i++)
Largest/smallest starta[0]
Reverse partnera[n − 1 − i], loop to n/2
Time to find maxO(n): one pass

Frequently asked questions

How do you find the largest element in an array?
Set max = a[0], loop through the rest and update max when a bigger element appears.
How do you reverse an array without a second array?
Swap a[i] with a[n−1−i] for i from 0 to n/2 − 1.
Why not start max with 0?
If all elements are negative, 0 would wrongly stay the answer.

Remember

  • Every 1D program = one loop + one action on a[i].
  • Largest = current champion idea.

Next Topic

Next lesson: Two-Dimensional Arrays in C. You will store data in rows and columns, like a matrix.