ARRAYS IN C

C - Arrays

Arrays in C : The C language provides a capability that enables the user to design a set of similar data types, called array. For understanding the arrays properly, let us consider the following program.

# include<stdio.h>
int main()
{
int x;
x=5;
x= 10;
printf(“x= %d\n”,x);
return 0;
}

No doubt, this program will print the value of x as 10. Because when a value 10 is assigned to x the earlier value of x, i.e. 5, is lost. Thus, ordinary variables are capable of holding only one value at a time.

Thus , an array is a collection of similar elements. These similar elements could be all ints or all floats, or all chars, etc. Usually, the array of character is called a ‘string’, whereas an array of ints or floats is called simply an array. Remember that all elements of any given array must be of the same type i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 floats.


A simple program of Arrays in C

#include<stdio.h>
int main()
{
int avg,sum=0;
int i;
int marks[30];

for(i=0;i<=29;i++)
{
printf(“Enter marks ”);
scanf(“%d”,&marks[i]);
}

for(i=0;i<=29;i++)
sum= sum+marks[i];

avg=sum/30
printf(“Average marks = %d\n”,avg);
return 0;
}

Array Declaration

To begin with, 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. In our program, we have done this with the statement:

int marks[30];

Accessing Elements of an array:

Oncean array is declared, let us see how individual elements in the array can be reffered. 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.In our program, we are using the variable i as a subscript to refer a various elements of the array. This variable can take different values and hence can refer to the different elements in the array in turn.this ability to use variables to represents subscripts is what makes arrays so useful.


Entering Data into Array:

Here is the section of the code that places data places data into an array:

for(i=0;i<=87;i++)
{
printf(“Enter the marks”);
Scanf(“%d”,&marks[i]);
}

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.

for (i=0;i<=29;i++)
sum=sum +marks[i];

avg =sum/30;
printf(“Average marks=%d\n”,avg);

More on Array

Array is a very popular data types with C programmers. This is because of the convenience with which arrays lend themselves to programming. The features which make arrays so convenient to program would be discussed below,along with the possible pitfalls in using them.

Array Initialisation

So far we have used arrays that did not have any values in them to begin with. We managed to store values in them during program execution. Let us now see how to initialize an array while declaring it. Following are a few examples:

int num[6]={2,4,12,5,6,45};
int n[]={1,2,3,4,5,6};
float press[]={12.3,34.5,37.7,54.2};

Two Dimensional Array

So far, we have explored arrays with only one dimension. It is also possible for arrays to have two or more dimensions. The two-dimensional array is also called a matrix.

Here is a simple program that stores roll number and marks obtained by a student side by side matrix.

#include <stdio.h>
int main()
{
int stud[4][2]
int I,j;
for(i=0;i<=3;i++)
{
printf(“Enter roll no. and marks”);
scanf(“%d %d”,&stud[i][0], &stud[i][1]);
}

for(i=0; i<=3; i++)
{
 printf(“%d %d\n”, Stud[i][0], stud[i][1]);
return 0;
}

Initialising a 2-Dimensional Array

How do initialize a two-dimensional array:

int stud[4][2]=
{
{1234,56},
{1212,33},
{1434, 80},
{1312, 78}
};

Or even this would work….

int stud[4][2] ={1234,56,1212,33,1434,80,1312,78};

Three-Dimensional Array

We aren’t going to show a programming example that uses a three-dimensional array. This is because, in practice, one rarely uses this array. However an example of initializing a three-dimensional array will consolidate your understanding of subscripts:

int arr[3][4][2]={
{
{2,4},
{7,8},
{3,4},
{5,6}

},

{
{7,6},
{3,4},
{5,3},
{2,3}

},
{
{8,9},
{7,2},
{3,4},
{5,1}
}
};

A 3-D array can be thought of as an array of arrays of arrays. The outer array has three elements, each of which is a 2-D array of four 1-D arrays, each of which contains two integers. In other words, a1-D array of two elements is constructed first. Then four such 1-D arrays are placed one below the other to give a 2-D array containing four rows. Then, three such 2-D arrays are placed one behind the other to yield a 3–D array containing three 2-D arrays. In the array declaration, note how the commas have been given.


Recommended Posts

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *