Nearby lessons

40 of 72

C - One Dimensional Array

A one-dimensional array is used to store multiple values of the same data type under one name. In this tutorial, you will learn how to declare, initialize, access, and process array elements using simple C programs and loops.

Introduction to One-Dimensional Arrays

What Is a One-Dimensional Array?

Definition: A one-dimensional array is a collection of elements of the same data type stored under a single variable name.

For example, if you want to store the marks of five students, you can create five separate variables. But this makes the program longer. An array allows you to store all five marks under one name.

Example

Here, the marks array stores the marks of five students.

Remember: Array indexes start from 0. Therefore, an array with five elements has indexes from 0 to 4.
Example01
CCode Cell
1#include <stdio.h>
2 
3int main(){
4 int marks[5] = {85, 90, 78, 92, 88};
5 
6 printf("First mark: %d\n", marks[0]);
7 printf("Second mark: %d\n", marks[1]);
8 printf("Last mark: %d\n", marks[4]);
9 
10 return 0;
11}
Output
First mark: 85
Second mark: 90
Last mark: 88

Declaring and Initializing an Array

Array Declaration

Array declaration means creating an array by specifying its data type, name, and size.

Syntax:

dataType arrayName[size];

Example:

int numbers[5];

This creates an integer array that can store five values.

Array Initialization

Array initialization means assigning values to an array when it is created.

Example02
CCode Cell
1#include <stdio.h>
2 
3int main(){
4 
5 int numbers[5] = {10, 20, 30, 40, 50};
6 int i;
7 
8 for (i = 0; i < 5; i++){
9 
10 printf("%d ", numbers[i]);
11 }
12 
13 return 0;
14}
Output
10 20 30 40 50

Accessing and Updating Array Elements

You can access an array element using its index. You can also change its value by assigning a new value to that index.

For example, numbers[1] represents the second element because indexing starts from 0.

Example03
CCode Cell
1#include <stdio.h>
2 
3int main(){
4 
5 int numbers[4] = {10, 20, 30, 40};
6 
7 printf("Before update: %d\n", numbers[1]);
8 
9 numbers[1] = 100;
10 
11 printf("After update: %d\n", numbers[1]);
12 
13 return 0;
14}
Output
Before update: 20
After update: 100

Printing Array Elements Using a Loop

A loop is useful when you want to print all array elements. Instead of writing a separate printf() statement for every element, you can use a for loop.

The loop starts from index 0 and continues until all five elements are printed.

Example04
CCode Cell
1#include <stdio.h>
2 
3int main(){
4 
5 int numbers[5] = {10, 20, 30, 40, 50};
6 int i;
7 
8 for (i = 0; i < 5; i++){
9 
10 printf("%d ", numbers[i]);
11 }
12 
13 return 0;
14}
Output
10 20 30 40 50

Calculating Sum and Average

Sum of Array Elements

The sum is the total of all the values stored in an array. We can use a loop to add each element to a total variable.

Average of Array Elements

The average is calculated by dividing the sum by the number of elements.

Formula: Average = Sum / Number of elements.

Example05
CCode Cell
1#include <stdio.h>
2 
3int main(){
4 
5 int marks[5] = {80, 90, 70, 60, 100};
6 int i, total = 0;
7 float average;
8 
9 for (i = 0; i < 5; i++){
10 
11 total = total + marks[i];
12 }
13 
14 average = total / 5.0f;
15 
16 printf("Total: %d\n", total);
17 printf("Average: %.2f\n", average);
18 
19 return 0;
20}
Output
Total: 400
Average: 80.00

Finding the Largest and Smallest Elements

We can find the largest and smallest values in an array using a loop and if statements.

  • Assume the first element is the largest.
  • Assume the first element is also the smallest.
  • Compare the remaining elements with these values.
  • Update the largest or smallest value whenever needed.
Example06
CCode Cell
1#include <stdio.h>
2 
3int main(){
4 
5 int numbers[5] = {45, 12, 89, 33, 67};
6 int i;
7 int largest = numbers[0];
8 int smallest = numbers[0];
9 
10 for (i = 1; i < 5; i++){
11 
12 if (numbers[i] > largest){
13 
14 largest = numbers[i];
15 }
16 
17 if (numbers[i] < smallest){
18 
19 smallest = numbers[i];
20 }
21 }
22 
23 printf("Largest: %d\n", largest);
24 printf("Smallest: %d\n", smallest);
25 
26 return 0;
27}
Output
Largest: 89
Smallest: 12

Counting Even and Odd Numbers

We can count even and odd numbers in an array using the modulus operator (%).

  • If number % 2 == 0, the number is even.
  • Otherwise, the number is odd.
Example07
CCode Cell
1#include <stdio.h>
2 
3int main(){
4 
5 int numbers[6] = {12, 7, 45, 8, 33, 20};
6 int i, even = 0, odd = 0;
7 
8 for (i = 0; i < 6; i++){
9 
10 if (numbers[i] % 2 == 0){
11 even++;
12 }else{
13 odd++;
14 }
15 }
16 
17 printf("Even numbers: %d\n", even);
18 printf("Odd numbers: %d\n", odd);
19 
20 return 0;
21}
Output
Even numbers: 4
Odd numbers: 2

Common Mistakes in One-Dimensional Arrays

1. Using an Invalid Index

For an array of size 5, valid indexes are 0 to 4. Do not access index 5 or any index outside this range.

2. Using the Wrong Loop Condition

Use i < 5 when processing five elements. Using i <= 5 will try to access one extra element.

3. Forgetting to Initialize the Total

Always initialize the total variable to 0 before adding array elements.

4. Initializing the Largest Value to Zero

If all array elements are negative, starting the largest value at 0 gives the wrong result. Use the first array element as the starting value instead.

Remember: Start with index 0, use the correct loop condition, and always stay within the array's valid range.
📝 Key Takeaways
  • A one-dimensional array stores multiple values of the same data type.
  • Array indexes start from 0.
  • Use square brackets [] to access array elements.
  • Loops make it easy to process array elements.
  • Use a total variable to calculate the sum and average.
  • Use comparisons to find the largest and smallest values.
  • Linear search checks array elements one by one.
  • Always use valid array indexes.

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8