1. Introduction
A single-dimensional array is the simplest form of an array that enables you to organize a series of items sequentially. This can be thought of as defining a single line with a number of slots that you can use to store values. In C, the most common type of array is the array of integers. In this blog post, we'll take a look at how to declare, initialize, and access data in a single-dimensional array in C.
2. Program Overview
In this program, we will:
1. Declare a single-dimensional array.
2. Initialize the array with values.
3. Print out each value of the array.
3. Code Program
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Declare and initialize an array of size 5
int i; // Loop counter variable
// Print each value of the array
for(i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
return 0;
}
Output:
arr[0] = 10 arr[1] = 20 arr[2] = 30 arr[3] = 40 arr[4] = 50
4. Step By Step Explanation
1. We start by declaring and initializing a single-dimensional array arr with 5 integers.
2. We then use a for loop to iterate through each element of the array.
3. Inside the loop, we print the index and the value of the current array element using the printf function.
Comments
Post a Comment
Leave Comment