Array
Array
Array is the collection of similar data types or collection of similar entity stored in
contiguous memory location. Array of character is a string. Each data item of an array
is called an element. And each element is unique and located in separated memory
location. Each of elements of an array share a variable but each element having
different index no. known as subscript. An array can be a single dimensional or multi-
dimensional and number of subscripts determines its dimension. And number of
subscript is always starts with zero. Array variable can store more than one value at
a time where other variable can store one value at a time.
The declaration of an array tells the compiler that, the data type, name of the array,
size of the array and for each element it occupies memory space. Like for int data type,
it occupies 2 bytes for each element and for float it occupies 4 byte for each element
etc. The size of the array operates the number of elements that can be stored in an
array.
#include<stdio.h>
int main(void)
{
int marks[5];
marks[0] = 80;
marks[1] = 89;
marks[2] = 78;
marks[3] = 76;
marks[4] = 67;
return 0;
}
#include<stdio.h>
int main(void)
{
int marks[5];
scanf("%d", &marks[0]);
scanf("%d", &marks[1]);
scanf("%d", &marks[2]);
scanf("%d", &marks[3]);
scanf("%d", &marks[4]);
return 0;
}
#include<stdio.h>
int main(void)
{
int marks[5], i, sum = 0;
return 0;
}
int main(void)
{
int n ;
printf("Enter n : ");
scanf("%d", &n);
return 0;
}
#include<stdio.h>
int main(void)
{
int n ;
printf("Enter n : ");
scanf("%d", &n);
return 0;
}
Two dimensional array is known as matrix. The array declaration in both the array
i.e.in single dimensional array single subscript is used and in two dimensional array
two subscripts are used.
We can say 2-d array is a collection of 1-D array placed one below the other. Total no.
of elements in 2-D array is calculated as row*column.
int arr[7];
0 1 2 3 4 5 6
#include<stdio.h>
int main(void)
{
int row, column ;
int arr[row][column] , i , j ;
return 0;
1 2 5 7
0,0 0,1 0,2 0,3
3 6 9 2
1,0 1,1 1,2 1,3
6 2 8 7
2,0 2,1 2,2 2,3
int main(void)
{
int r, c;
printf("Enter the number of rows : ");
scanf("%d", &r);
printf("Enter the number of columns : ");
scanf("%d", &c);
return 0;
}