INTRODUCTION OF ARRAY
Introduction to Arrays
Arrays are a data structure that is used to store a group of objects of the same type sequentially in memory. All the elements of an array must be the same data type.An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. Usually, the array of characters is called a "string", where as an array of integers or floats is called simply an array. All elements of any given array must be of the same type i.e we can't have an array of 10 numbers, of which 5 are ints and 5 are floats.
Some important points about Array:
- First element of an array has zero index
- All the data items in array are always stored in consecutive memory locations.
- Arrays are always store under a common heading or a variable name
- An array either be an integer, character, or floating point data item but initialized only on declaration time not afterwards.
- An array can always be read or write through loops
datatype arrayName[size];
Examples:int ID[30];
/* Could be used to store the ID numbers of students in a class */
float temperatures[31];
/* Could be used to store the daily temperatures in a month */
char name[20];
/* Could be used to store a character string. Character strings in C are terminated by the null character, '\0'. This will be discussed later in the this lesson. */
Advantage and Disadvantage of Array
Advantages:
1. You can use one name for similar objects and save then with the same name but different indexes.2. Arrays are very useful when you are working with sequences of the same kind of data (similar to the first point but has a different meaning).
3. Arrays use reference type and so.
Disadvantages:
1. Sometimes it's not easy to operate with many index arrays.2. C environment doesn't have checking mechanism for array sizes.
3. An array uses reference mechanism to work with memory which can cause unstable behavior of operating system (unless special methods were created to prevent it) and often even "blue screen" and so on.
Using Arrays
when we declareint x[50];
| | | | | | | | ....... | | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ....... ..... 49 |
x[7]=57;
then our block look as follows | | | | | | | 57 | | ....... | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ....... 49 |
0 comments:
Post a Comment