HKBK College of Engineering Department of
Computer Science and Engineering
Programming In C – 1BEIT105
Module 3
Arrays in C:
What is a one-dimensional array?
A one-dimensional array in C is a collection of values (like numbers), stored
together in a single list or row. All the values have the same data type (e.g., all
are integers)like a row of boxes, each holding a value and each with its own
position number (called the index).
Example:
Program to input and print 5 numbers
#include <stdio.h>
int main()
{ int numbers[5];
printf("Enter 5 numbers:\n");
for(int i = 0; i < 5; i++)
{ scanf("%d", &numbers[i]);
}
printf("The numbers are:\n");
for(int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
o/p
Enter 5 numbers : 5 3 7 2 9
The numbers are: 5 3 7 2 9
Program to calculate the sum of array elements
#include <stdio.h>
int main()
{ int arr[5], sum = 0;
printf("Enter 5 numbers:\n");
for(int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum = %d\n", sum);
return 0;
}
o/p
Enter 5 numbers: 1 2 3 4 5
Sum = 15
Program to find the largest number in an array
#include <stdio.h>
int main()
{
int arr[5], max;
printf("Enter 5 numbers:\n");
for(int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
max = arr[0];
for(int i = 1; i < 5; i++)
{
if(arr[i] > max) max = arr[i];
}
printf("Largest = %d\n", max);
return 0;
}
o/p
Enter 5 numbers : 5 3 7 2 9
Largest = 9
Two-Dimensional Arrays
A tw 2dimensional array is an "array of arrays"as a table with rows and columns.
A two-dimensional array in C is like a table or grid made of rows and columns.
It’s used to store data in a matrix form (for example, marks of students in
different subjects, or elements of a chessboard)
ample:
ex: int arr[2][3];
Print a 2x3 Matrix:
#include <stdio.h>
int main()
{ int arr[2][3] = { {1,2,3}, {4,5,6} };
printf("Matrix (2x3):\n");
for(int i=0;i<2;i++)
{ for(int j=0;j<3;j++)
{ printf("%d ", arr[i][j]);
} printf("\n");
}
return 0;
}
o/p 1 2 3 4 5 6
Print Transpose of a 2x3 Matrix:
#include <stdio.h>
int main() {
int arr[2][3] = {{2,4,6},{8,10,12}};
printf("Transpose (3x2):\n");
for(int i=0;i<3;i++) {
for(int j=0;j<2;j++) {
printf("%d ", arr[j][i]);
}
printf("\n");
}
return 0;
}
Output:
//Transpose (3x2):
28
4 10
6 12
Multidimensional Arrays in C
A multidimensional array in C is an array with more than one index (dimension). The most
common is a two-dimensional array (matrix/grid/table). Beyond two
dimensions, you can have 3D arrays and more.
2D array: int arr[2][3]; (2 rows, 3 columns)
3D array: int arr[2][3][4]; (2 blocks, each block has 3 rows, each row has 4
columns)
Program to Print the Elements of a 3D Array
#include <stdio.h>
int main() {
int arr[2][2][3] = {
{ {1,2,3}, {4,5,6} },
{ {7,8,9}, {10,11,12} }
};
printf("Elements of the 3D array are:\n");
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
for(int k=0;k<3;k++) {
printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
}
}
}
return 0;
}
Sample Output:
text
Elements of the 3D array are:
arr[0][0][0] = 1
arr[0][0][1] = 2
arr[0][0][2] = 3
arr[0][1][0] = 4
arr[0][1][1] = 5
arr[0][1][2] = 6
arr[1][0][0] = 7
arr[1][0][1] = 8
arr[1][0][2] = 9
arr[1][1][0] = 10
arr[1][1][1] = 11
arr[1][1][2] = 12