.png)
In C programming, arrays are a fundamental concept that allows you to store multiple values of the same data type in a single variable. Instead of using multiple variables to store similar data, arrays make it easier to manage and manipulate these values.
Types of Arrays
There are two main types of arrays in C:
- One-Dimensional Arrays
- Multi-Dimensional Arrays
One-Dimensional Arrays
A one-dimensional array is like a list where all the elements are stored in a single row or column. It uses only one subscript to access each element.
Declaration:
DataType variable_name[size];
Here, DataType
is the type of elements (e.g., int
, float
), variable_name
is the name of the array, and size
is the number of elements the array can hold.
Example: Declaring an integer array with 5 elements:
int number[5];
This array can store 5 integers, with indices ranging from 0 to 4.
Initialization of One-Dimensional Arrays
Arrays can be initialized at the time of declaration:
int number[3] = {1, 2, 3};
If you provide fewer values than the size, the remaining elements are automatically set to zero.
Example: Initializing an array with specific values:
float total[5] = {0.0, 15.75, -10};
Example Program:
#include<stdio.h>
int main() {
int a[5] = {1, 4, 3, 7, 8};
int i;
printf("\nArray elements are:");
for(i = 0; i < 5; i++) {
printf("\n a[%d] = %d", i, a[i]);
}
return 0;
}
Output:
Array elements are:
a[0] = 1
a[1] = 4
a[2] = 3
a[3] = 7
a[4] = 8
Accepting Array Elements from the User
Here’s an example where the array size and elements are input by the user:
#include<stdio.h>
void main() {
int a[20], i, n;
printf("\nEnter array size: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("\nEnter array element a[%d]: ", i);
scanf("%d", &a[i]);
}
printf("\nArray elements are:");
for(i = 0; i < n; i++) {
printf("\n a[%d] = %d", i, a[i]);
}
}
Output:
Enter array size: 5
Enter array element a[0]: 1
Enter array element a[1]: 4
Enter array element a[2]: 3
Enter array element a[3]: 7
Enter array element a[4]: 8
Array elements are:
a[0] = 1
a[1] = 4
a[2] = 3
a[3] = 7
a[4] = 8
Two-Dimensional Arrays
Two-dimensional arrays are used to store data in a matrix form, consisting of rows and columns. They require two subscripts: one for the row and another for the column.
Declaration:
DataType array_name[row-size][column-size];
Here, DataType
is the type of elements, array_name
is the name of the array, and row-size
and column-size
are the number of rows and columns respectively.
Initialization of Two-Dimensional Arrays
Two-dimensional arrays can be initialized at the time of declaration:
int table[2][3] = {0, 0, 0, 1, 1, 1};
This can also be written as:
int table[2][3] = {{0, 0, 0}, {1, 1, 1}};
Example Program:
#include<stdio.h>
void main() {
int a[2][2] = {{3, 4}, {4, 5}};
int i, j;
printf("\nArray elements are:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%4d", a[i][j]);
}
printf("\n");
}
}
Output:
Array elements are:
3 4
4 5
Accepting a 3x3 Matrix from the User
Here’s an example where a 3x3 matrix is input by the user:
#include<stdio.h>
void main() {
int a[3][3], i, j;
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("\nEnter array element a[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("\nGiven Matrix is:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("%4d", a[i][j]);
}
printf("\n");
}
}
Output:
Enter array element a[0][0]: 1
Enter array element a[0][1]: 2
Enter array element a[0][2]: 3
Enter array element a[1][0]: 4
Enter array element a[1][1]: 5
Enter array element a[1][2]: 6
Enter array element a[2][0]: 7
Enter array element a[2][1]: 8
Enter array element a[2][2]: 9
Given Matrix is:
1 2 3
4 5 6
7 8 9
Thank you for reading! Your thoughts and suggestions are always welcome—let’s connect in the comments below!
0 Comments
Got questions? Feel free to ask!