Introduction
In this chapter, we will learn about one-dimensional arrays in C programming. One-dimensional arrays are a fundamental data structure that allows you to store and manage a sequence of elements of the same type efficiently.
What is a One-Dimensional Array?
A one-dimensional array is a collection of elements of the same type, stored in contiguous memory locations. Each element in the array can be accessed using its index. The index of the first element is 0, the second element is 1, and so on.
Syntax
The basic syntax for declaring a one-dimensional array in C is as follows:
data_type array_name[array_size];
data_type
: The type of elements stored in the array (e.g.,int
,float
,char
).array_name
: The name of the array.array_size
: The number of elements the array can hold.
Example: Declaring and Initializing a One-Dimensional Array
Let’s look at a simple example to understand how to declare and initialize a one-dimensional array.
Example: Declaring and Initializing an Integer Array
#include <stdio.h>
int main() {
// Declaring an array of integers
int numbers[5] = {1, 2, 3, 4, 5};
// Accessing and printing array elements using a loop
for (int i = 0; i < 5; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
}
return 0; // Returning 0 to indicate successful execution
}
Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
In this example, we declared an array of integers named numbers
with five elements. We then used a loop to access and print each element in the array.
Accessing Array Elements
Array elements are accessed using their index, which starts from 0. The first element of an array is at index 0, the second element is at index 1, and so on.
Example: Accessing Array Elements
#include <stdio.h>
int main() {
int numbers[3] = {10, 20, 30};
// Accessing and printing array elements
printf("First element: %d\n", numbers[0]);
printf("Second element: %d\n", numbers[1]);
printf("Third element: %d\n", numbers[2]);
return 0; // Returning 0 to indicate successful execution
}
Output:
First element: 10
Second element: 20
Third element: 30
Modifying Array Elements
Array elements can be modified by assigning new values to them using their index.
Example: Modifying Array Elements
#include <stdio.h>
int main() {
int numbers[3] = {10, 20, 30};
// Modifying array elements
numbers[0] = 100;
numbers[1] = 200;
numbers[2] = 300;
// Accessing and printing modified array elements
printf("First element: %d\n", numbers[0]);
printf("Second element: %d\n", numbers[1]);
printf("Third element: %d\n", numbers[2]);
return 0; // Returning 0 to indicate successful execution
}
Output:
First element: 100
Second element: 200
Third element: 300
Array Initialization
Arrays can be initialized at the time of declaration. If the array size is not specified, the compiler determines the size based on the number of elements in the initialization list.
Example: Array Initialization
#include <stdio.h>
int main() {
// Array initialization without specifying size
int numbers[] = {10, 20, 30, 40, 50};
// Determining the size of the array
int array_size = sizeof(numbers) / sizeof(numbers[0]);
// Accessing and printing array elements
for (int i = 0; i < array_size; i++) {
printf("Element at index %d: %d\n", i, numbers[i]);
}
return 0; // Returning 0 to indicate successful execution
}
Output:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Simple C Programs to Demonstrate One-Dimensional Arrays
Program 1: Finding the Largest Element in an Array
Example:
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int array_size = sizeof(numbers) / sizeof(numbers[0]);
int max = numbers[0];
for (int i = 1; i < array_size; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
printf("The largest element is: %d\n", max);
return 0; // Returning 0 to indicate successful execution
}
Output:
The largest element is: 50
Program 2: Summing Elements of an Array
Example:
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int array_size = sizeof(numbers) / sizeof(numbers[0]);
int sum = 0;
for (int i = 0; i < array_size; i++) {
sum += numbers[i];
}
printf("The sum of the elements is: %d\n", sum);
return 0; // Returning 0 to indicate successful execution
}
Output:
The sum of the elements is: 150
Program 3: Reversing an Array
Example:
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int array_size = sizeof(numbers) / sizeof(numbers[0]);
printf("Original array: ");
for (int i = 0; i < array_size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
printf("Reversed array: ");
for (int i = array_size - 1; i >= 0; i--) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0; // Returning 0 to indicate successful execution
}
Output:
Original array: 10 20 30 40 50
Reversed array: 50 40 30 20 10
Conclusion
One-dimensional arrays are a fundamental data structure in C that allow you to store and manage a sequence of elements of the same type efficiently. By understanding how to declare, initialize, and manipulate one-dimensional arrays, you can write more efficient and organized code. Arrays are versatile and can be used in a wide range of applications, from simple data storage to complex data processing tasks. Understanding one-dimensional arrays is essential for effective programming in C.