What is Array
What is Array is a common and important question among the programming people. Many times teacher in viva voice questions ask this question What is Array. Sometimes interviewers also asks this easy question to interviewees to just start questioning.
Before we came to know what is array,lets us discuss what is the need of using arrays in any programming language. Arrays are famous among programmer because they make programming easy, when in a program there is need of large number of variables of same data type. Other reason of array's popularity among programmer is their memory storage. When we use arrays to process a list of elements, all the elements are stored in contiguous manner. Their many allocation is static in nature.
What is Array |
Arrays in C are also known as sub scripted variables. Array is basically a collection of similar items. Same thing apply to array in C.
When in some C language program there is need of using a large number of variables in a single program, it become tedious for programmer to deal with such a large list of variables.
For example, if we have to read roll nos of 100 students of a class in a program, we have to use 100 different variables in a single program.
Declaring 100 different variables and then reading them with 100 format strings is difficult. To overcome this problem, we can use Array in C language.
What is array
Arrays are basically collection of variables of same data type.Types of Array In C
There are two types of arrays in C
- One Dimensional Array
- Two Dimensional Array
One Dimensional Array
One Dimensional Array in C is like a list of items arranged in a row or in a column. For example , consider the list- 10, 20,34, 56, 78, 2 . You can arrange elements of a one dimensional array as a row or column of elements. One dimensional arrays are usually processed using a for loop.
Array in C starts with index 0. Suppose you have array A of size 10, the first element will be A[0]
How to declare One Dimensional Array
The general syntax for declaring One Dimensional Array in C
datatype name of array[size];
Here datatype is the datatype of elements to be stored in array. Name of the array is the name you wants to assign to array in C .
Square brackets([ ]) indicates that this is an array.
Size is the number of elements array will store. Size must be integer.
Example
int A[10];
Here name of the array is A, size is 10, all the ten elements of the array will be integers.
Similarly you can declare array of float data types and char data types.
float b[10];
char c[5];
Programming Examples of One Dimensional Arrays
Program to read 10 elements and print these elements.
main()
{
int a[10],i;
printf(“Enter Elements ofthe list\n”);
for(i=0;i<=9;i++)
{
scanf(“%d”,&a[i]);
}
printf(“You have entered following list”);
for(i=0;i<=9;i++)
{
printf(“\n%d”,a[i]);
}
}
Program to sort a list of elements.
Program to search an element in a list of elements.
Two Dimensional Array in C
Two Dimensional Array in C are the arrays , where elements are arranged in the form of matrix. Each element of a Two Dimensional Array in C will have a row number and column number.
Array in C is usually processed in loops, for loop is most convenient loop for array processing.
Two Dimensional Arrays are to be processed in nested loops.
Programming examples of two Dimensional Arrays
Program to print transpose of a matrix
Program to Add Two Matrices
Program to Multiply Two Matrices
Relational Operators of C language
C language operators
Popular Programs of C language
Program to Reverse a Number in C language
Program to Calculate Factorial of a number in C language
Program to print Fibbonacci sequence in C language
Summary
We hope this article What is Array is informative for you. If you like , you can share it on your social media ids. This will encourage us to arrange more such articles for you.