WHAT IS ARRAY? | ARRAY IN C | C ARRAY | TYPES OF C ARRAYS | ARRAYS IN C LANGUAGE
Arrays:
An array is defined as finite ordered collection of homogenous data, stored in contiguous memory locations.
Here the words,Finite means data range must be defined.
Ordered means data must be stored in continuous memory addresses.
Homogenous means data must be of similar data type.
Example where arrays are used,
to store list of Employee or Student names, to store marks of a students,or to store list of numbers or characters etc.
Arrays in c language |
Declaring an Array
Like any other variable, arrays must be declared before they are used. General form of
array declaration is,
data-type variable-name[size];
For example:
int arr[10];
In the above example the array name arr is declared of type integer and of size 10
.Each arr holds 2 bytes as the size of integer is 2 bytes .
Here int is the data type, arr is the name of the array and 10 is the size of array. It means
array arr can only contain 10 elements of int type. Index of an array starts from 0 to size-
1 i.e first element of arr array will be stored at arr[0] address and last element will occupy
arr[9].
TYPES OF C ARRAYS:
One – Dimensional Arrays
Two – Dimensional Arrays
Multi- Dimensional Arrays
One – Dimensional Arrays
Sequential collections of data of same type with one subscript is called
One-Dimensional Arrays.
type arrayName [ arraySize ];
For example,
float mark[5];// it contains one subscript which is used to store the size of array.
Here, we declared an array, mark, of floating-point type and size 5. Meaning, it can hold
5 floating-point values.
Initialization of an Array ( Imp)
After an array is declared it must be initialized. Otherwise, it will contain garbage value
(any random value).
An array can be initialized at either compile time or at runtime
Compile time Array initialization of One- Dimensional Array
Compile time initialization of array elements is same as ordinary variable
initialization.
The values for the array is initialized in the code directly.
The general form of initialization of array is,
type array-name[size] = { list of values };
int marks[4]={ 67, 87, 56, 77 }; //integer array initialization
float area[5]={ 23.4, 6.8, 5.5 }; //float array initialization
Program Example :
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int arr[]={2,3,4}; //Compile time array initialization
for(i=0 ; i<3 ; i++) {
printf("%d\t",arr[i]);
}
getch();
}
Output
2 3 4
Runtime Array initialization of One- Dimensional Array
An array can also be initialized at runtime using scanf() function. This
approach is usually used for initializing large array, or to initialize array with user
specified values.
Example,
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[4];
int i, j;
printf("Enter array element");
for(i=0;i<4;i++)
{
scanf("%d",&arr[i]); //Run time array initialization
}
for(j=0;j<4;j++)
{
printf("%d\n",arr[j]);
}
getch();
}
Two – Dimensional Arrays
The arrays with contains rows and columns (2 Subscript) is called as Two-
Dimensional Arrays.
Two Dimensional Array stores the values in the form of matrix.
Syntax :
type array-name[row-size][column-size] ;
Example :
int a[3][4];
In the above example the arrays consists of 3 rows and 4 columns.
Compile time Array initialization of Two- Dimensional Array
Compile time initialization of array elements is same as ordinary variable
initialization.
The values for the array is initialized in the code directly.
Syntax:
type array-name[row_size][col-size] = { list of values };
int arr[2][2] = {10,20,30,40};
EXAMPLE PROGRAM FOR TWO DIMENSIONAL ARRAY IN C:
#include<stdio.h>
int main()
{
int i,j;
// declaring and Initializing array
int arr[2][2] = {10,20,30,40};
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
Printf(“%d”,a[i][j]);
}
Printf(“\n”);
}
return 0;
}
Output:
10 20
30 40
Runtime Array initialization of Two- Dimensional Array
An array can also be initialized at runtime using scanf() function. This
approach is usually used for initializing large array, or to initialize array with user
specified values.
EXAMPLE PROGRAM FOR TWO DIMENSIONAL ARRAY IN C:
#include<stdio.h>
int main()
{
int i,j;
// declaring 2d array
int arr[2][2] ;
for (i=0;i<2;i++) // run time code
{
for (j=0;j<2;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
Printf(“%d”,a[i][j]);
}
Printf(“\n”);
}
return 0;
}
Output:
10 20 30 40
10 20
30 40
How to pass arrays to a function in C Programming?
In C programming, a single array element or an entire array can be passed to a function.
This can be done for both one-dimensional array and 2-dimensional array.
Passing One-dimensional Array In Function
Single element of an array can be passed in similar manner as passing variable to a function.
Passing an entire one-dimensional array to a function
While passing arrays as arguments to the function, only the name of the array is
passed (,i.e, starting address of memory area is passed as argument).
#include<stdio.h>
Void main()
{
Int arr[5]={10,20,30,40,50};
Sum=add(arr);
Printf(“%d ”,sum); }
Int add(int b[])
{
int i,s=0;
for (i=0; i<5;i++)
{
s=s+b[i];
}
return s;
}
Output: 150
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment