Nearby lessons
40 of 72C - 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.
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.
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.
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.
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.
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.
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.
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.
- 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.