INTRODUCTION
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
There are 2 types of array : 1D array and 2D array.
1.1D Array :
- It is a derived (we can create own data type) data type.
- All the elements in array are of same data type.(at a time we can use int/char/..cant use both at a time)
- No boundary.
- Elements store in a continuous memory allocation.
data_type array_name[size of array];
eg. int array[5]={1,2,3,4,5};
- arrays index value starts with 0: array[0]=1,array[1]=2,array[2]=3,array[3]=4,array[4]=5
- to change the element value ,refer index value.
- we cant change the size of array after creation.
#include<stdio.h>
int main()
{
int array[10];
printf("enter 5 values of array :");
for(i=0; i=<4; i++)
{
scanf("%d", &array[i]);
}
for(i=0; i=<4; i++)
{
printf("%d", array[i]);
}
}
output :enter 5 values of array : 12345
1.2D Array :
Two dimensional array can be defined as an array of array. It is also known as a matrix(a table of rows and columns).
Syntax :
data_type array_name[row][column] ;
eg. int arr[4][4]={{2,1,2,2}, {1,8,1,0}, {3,5,9,1}, {5,4,6,8}};
- Rules to write 2D array :
- int arr[2][3]; -----------valid
- int arr[ ][3]; ------------valid
- int arr[ ][ ]; --------------invalid
- int arr[2][ ];-------------invalid
- This 2D array program can be written in only nested loop.
#include<stdio.h>
int arr[2][2];
int main()
{
int i,j;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
print("enter value of array : \n");
scanf("%d", &arr[i][j]);
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d \t",arr[i][j]);
}
printf("\n");
}
}
output :
enter value of array :
2
3
4
5
2 3
4 5
No comments:
Post a Comment