UNIT-3 Arrays
UNIT-3 Arrays
Arrays:-
• Introduction to arrays
• one dimensional arrays
• Two dimensional arrays
• Multi dimensional arrays
• Sample Programs
• An array is defined as the collection of similar
type of data items stored at contiguous memory
locations.
• Arrays are the derived data type in C
programming language which can store the
primitive type of data such as int, char, double,
float, etc.
• It also has the capability to store the collection
of derived data types, such as pointers,
structure, etc.
• The array is the simplest data structure where
each data element can be randomly accessed by
using its index number.
For example,
• if we want to store the marks of a
student in 6 subjects, then we don't
need to define different variables for
the marks in the different subject.
• Instead of that, we can define an
array which can store the marks in
each subject at the contiguous
memory locations.
Properties of Array
array_name [index];
#include<stdio.h>
int main()
{
int i=0;
int marks[5]; //declaration of array
//initialization of array
marks[0]=80;
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
//end of for loop
return 0;
} Output
80
60
70
85
75
2. Array Initialization with Declaration without Size
int main()
{
int arr[5],i;
for(i=0;i<5;i++)
scanf(“%d”,arr[i]);
//Printng Array Elements
printf(“The Given Array elements are:”);
for(i=0;i<5;i++)
Printf(“%d\n”,arr[i]);
return 0;
}
Types of Array in C
Syntax
array_name [size];
Example
int arr[5];
char status[10];
2. Multidimensional Array in C
Syntax
array_name[size1] [size2];
Here,
int main()
{
return 0;
}
B. Three-Dimensional Array in C
Syntax
int main()
{
// 3D array declaration
int arr[2][2][2] = { 10, 20, 30, 40, 50, 60 };
// printing elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n \n");
}
return 0;
}