Introduction
In this chapter, we will discuss a very important data type arrays. Let us see, “Why arrays?’’
We know that in one variable we can store the information of only one data item. Suppose that a
Student has scored 90 marks. These marks can be stored in a variable as shown below:
int marks=90;
After executing this statement, the value 90 will be stored in the variable marks. Suppose there is
A need to store marks of 10 students. In such case, we are forced to use 10 variables like marksl,
Marks2… marks10. But, if it is required to store the marks of 100 students, definitely it is not
Feasible to use 100 variables marksl ,marks2…marks100, Now, the question is “How to store 100
different marks?” In mathematics, we use sets to group the items of similar kind, For example,
consider the set shown below :
marks = {80,90,45,99,100,36,88,96,67,92}
This is a set of marks of `10 students. Note that every item can be accessed by prefixing marks along
With the position of marks in the set. The first item 80 corresponds to the marks of first student,90
Corresponds to the marks of second student and so on i.e., marks1=80, marks2=90…marks 10=92.
In C language, this is where, the concept of arrays is used. Since all the marks are of the same type,
We can group and refer all the marks with a common name using arrays.
Note: In general, if more number of data items of same type (kind) are to be accessed or read
or stored, then we use arrays.
The meaning of an array
Definition : An array is defined as an ordered set of similar data items. All the data items of
an array are stored in consecutive memory locations in main memory . The elements of an array
are of same data type and each item can be accessed using the same name. e.g. consider an
array of marks of 5 students as shown below:
80 | 90 | 45 | 99 | 100 |
Marks(0) | Marks(1) | Marks(2) | Marks(3) | Marks(4) |
To refer an item in the array, we specify the name of the array along with position of the item. The
Position of the item must be written within square brackets ‘ []’. The position of the item, The item enclosed within square brackets is called ‘subscript’ or ‘index’.
For example, the above figure represents an integer array called marks where marks of 5 students
Are stored. The marks of each student can be accessed as shown below:
marks[0] i.e. 80 – represent marks of first student
marks[ 1] i.e. 90- represent marks of second student
marks[2] i.e. 45- represent marks of third student
marks [3] i.e. 99- represent marks of fourth student
marks[4] i.e. 100 – represent marks of fifth student
Thus using marks [0] through marks[4] we can access the marks of 5 students.
Note: Using marks [0] through marks [n-1] We can access the marks of n students in general.
In an array it not possible to have a group of items with different data types. Types. For example,
83 | 94.8 | “Delhi” | ‘3’ | 910 |
a[0] | a[1] | a[2] | a[3] | a[4] |
Is invalid way of storing the elements in an array. This is because, it is a collection of int, float, char
and string datatypes. Once we know the definition of an array. The next question is “How arrays
are classified?” The arrays can be classified based on how the data items are arranged for human
understanding. This is pictorially represented as shown below:
Tidak ada komentar:
Posting Komentar