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
int a[100]reserves room for up to 100 numbers. The user decidesn, how many to use.- The first loop reads each number into
a[i]. Note the&: scanf needs the address of the element. largest = a[0]; smallest = a[0];starts both with the first element. Never startlargestwith 0: it fails when all numbers are negative.- The second loop adds each element to
sumand updateslargestandsmallestwhen 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}:
| i | a[i] | a[i] > largest? | largest |
|---|---|---|---|
| 0 | 12 | No (equal) | 12 |
| 1 | 45 | Yes | 45 |
| 2 | 7 | No | 45 |
| 3 | 30 | No | 45 |
| 4 | 18 | No | 45 |
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
iis indexn - 1 - i. - The loop runs only
n / 2times. If it ranntimes, 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]witha[n-1-i]for i < n/2. - Always make sure
nis not bigger than the declared size.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
scanf("%d", a[i]) without & | Crash or wrong values | scanf("%d", &a[i]) |
largest = 0 at the start | Wrong answer for all-negative input | largest = a[0] |
Reverse loop runs to n | Array ends up unchanged | Run to n / 2 |
int a[n]; before reading n | Size is garbage | Read n first, or use a fixed big size |
Not resetting sum = 0 | Garbage total | Initialize before the loop |
1D Array Practice Questions
- What is a one-dimensional array? Give one real-life example.
- Why should
largeststart witha[0]? - What is the partner index of
iwhen reversing an array of size n? - How many swaps are needed to reverse an array of 7 elements?
- Why do we often declare
int a[100]when the user enters n?
Show answers
- An array with one index whose elements form a single row, for example the roll-number-wise marks of a class.
- So the answer is correct even when every number is negative (or every number is less than the starting value).
n - 1 - i.- 7 / 2 = 3 swaps (the middle element stays).
- 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)
-
A one-dimensional array has how many subscripts?
A) 0 B) 1 C) 2 D) Any
-
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]); -
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] -
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
-
Best starting value for
smallest:A) 0 B) 100 C)
a[0]D)a[n]
Show answers
- B: 1.
- B:
&a[i]. - C:
a[n-1-i]. - C: 12.
- C:
a[0].
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| 1D array | One index, a single row |
|---|---|
| Read element | scanf("%d", &a[i]) |
| Visit all | for (i = 0; i < n; i++) |
| Largest/smallest start | a[0] |
| Reverse partner | a[n − 1 − i], loop to n/2 |
| Time to find max | O(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.