Arrays are structures that hold multiple variables of the same data type. The first element in the array is numbered 0, so the last element is 1 less than the size of the array. An array is also known as a subscripted variable. Before using an array its type and dimension must be declared.
1. Array Declaration
Like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want.
int marks[30] ;
Here, int
specifies the type of the variable, just as it does with ordinary variables and the word marks
specifies the name of the variable. The [30]
however is new. The number 30 tells how many elements of the type int
will be in our array. This number is often called the "dimension" of the array. The bracket ( [ ] )
tells the compiler that we are dealing with an array.
Let us now see how to initialize an array while declaring it. Following are a few examples that demonstrate this.
int num[6] = { 2, 4, 12, 5, 45, 5 } ;
int n[] = { 2, 4, 12, 5, 45, 5 } ;
float press[] = { 12.3, 34.2 -23.4, -11.3 } ;
2. Accessing Elements of an Array
Once an array is declared, let us see how individual elements in the array can be referred. This is done with subscript, the number in the brackets following the array name. This number specifies the element’s position in the array. All the array elements are numbered, starting with 0. Thus, marks [2] is not the second element of the array, but the third.
int valueOfThirdElement = marks[2];
3. Entering Data into an Array
Here is the section of code that places data into an array:
for(i = 0;i <= 29;i++)
{
printf("\nEnter marks ");
scanf("%d", &marks[i]);
}
The for
loop causes the process of asking for and receiving a student’s marks from the user to be repeated 30 times. The first time through the loop, i
has a value 0, so the scanf()
function will cause the value typed to be stored in the array element marks[0]
, the first element of the array. This process will be repeated until i
becomes 29. This is last time through the loop, which is a good thing, because there is no array element like marks[30]
.
In scanf()
function, we have used the "address of" operator (&) on the element marks[i]
of the array. In so doing, we are passing the address of this particular array element to the scanf()
function, rather than its value; which is what scanf()
requires.
4. Reading Data from an Array
The balance of the program reads the data back out of the array and uses it to calculate the average. The for loop is much the same, but now the body of the loop causes each student’s marks to be added to a running total stored in a variable called sum. When all the marks have been added up, the result is divided by 30, the number of students, to get the average.
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ;
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
5. Advantages of using an Array
- Similar data types can be grouped together under one name.
for ex.-int a=1;int b=2;int c=3;int d=4;int e=5;
could be represented as-int X[5]={1,2,3,4,5};
- It allows random accessing of elements using indices which saves a huge time.
- It can be used to implement other data structures like- stack,queues etc.
- We can represent 2-D matrices using array.
6. Disadvantages of using an Array
- It is mandatory to determine the size of array to store the elements in it.
- As array elements are stored at consecutive memory locations,so insertion and deletion of an element is difficult/time consuming.
- There is a certain chance of memory wastage/shortage.
- Array size is static in nature so size of array cannot be altered.
7. Multidimensional arrays
The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x][y], you would write something as follows −
type arrayName [x][y];
Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
8. Example
8.1 Simple Array
Let us try to write a program to find average marks obtained by a
class of 30 students in a test.
#include<stdio.h>
main()
{
int avg, i, sum=0;
int marks[30] ; /*array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}
8.2 Two-Dimensional Array
#include <stdio.h>
main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}
9. Quiz
Test what you learn from this tutorial. Click here to give a quiz based on this tutorial.
10. Examples
Next - Strings