Lecture 11
Arrays
In this lecture
we will get familiar with one of the C’s most essential data structures called
arrays. Arrays are data structures consisting of related data items of the same
type. You can also say that it is a group of memory locations related by the
fact that they all have the same name and same type.
Why Arrays
For understanding the arrays properly, let
us consider the following program:
No doubt, this
program will print the value of x as 10. Why so? Because when a value 10
is assigned to x, the earlier value of x, i.e. 5, is lost. Thus,
ordinary variables (the ones which we have used so far) are capable of holding
only one value at a time (as in the above example). However, there are
situations in which we would want to store more than one value at a time in a
single variable.
For example,
suppose we wish to arrange the percentage marks obtained by 100 students in
ascending order. In such a case we have two options to store these marks in
memory:
1.
Construct 100 variables to
store percentage marks obtained by 100 different students, i.e. each variable
containing one student’s marks.
2.
Construct one variable (called
array or subscripted variable) capable of storing or holding all the hundred
values.
Obviously, the
second alternative is better. A simple reason for this is, it would be much
easier to handle one variable than handling 100 different variables. Moreover,
there are certain logics that cannot be dealt with, without the use of an array
One Dimensional Array
In C, a one
dimensional array is a list of variables that are all of the same type and
accessed through a common name. An individual element in an array is called
array element. Array is helpful to handle a group of similar types of data.
To declare a one
dimensional array, we use-
data_type array_name [size];
Here, data_type
is a valid C data type, array_name is the name of tha array and follows the
convention of naming a variable, and size specifies the number of elements in
the array.
int my_array[20];
-Declares an
array name my_array of type integer that contains 20 elements.
An array element
is accessed by indexing the array using the number of element. In C, all arrays
begin at zero. This means that if you want to access the first element in an
array, use zero for the index. To index an array, specify the index of the
element you want inside square brackets. The second element of my_array will
be-
my_array [1]
To assign an
array element a value, put the array on the left side of the assignment
operator. The example gives the first element of my_array the value 200.
my_array [0]=200;
C stores one
dimensional array in one contiguous memory location with first element at the
lower address. For example, an array named a of 10 elements can occupy
the memory as follows-
Index
|
Value
|
a [0]
|
-45
|
a [1]
|
10
|
a [2]
|
32
|
a [3]
|
100
|
a [4]
|
9
|
a [5]
|
9
|
a [6]
|
50
|
a [7]
|
100
|
a [8]
|
-9
|
a [9]
|
12
|
Now, let us
consider a program that declares an array of 10 elements and initializes every
element of that array with 0.
main(){
int a[10], i;
for (i=0;i<10;i++)
a [i] = 0;
printf (“Element \t Value”);
for (i=0;i<10;i++)
printf (“%d \t%d”, i, a [i] );
}
The elements of
an array can also be initialized by following the declaration with an equal
sign and a comma separated list of values within a pair of curly brace.
main(){
int a[10]={1,2,3,4,5,6,7,8,9,10},
int i;
printf (“Element \t Value”);
for (i=0;i<10;i++)
printf (“%d \t%d”, i, a [i] );
}
If there are
fewer values than elements in array, the remaining elements are initialized
automatically with zero.
main(){
int a[10]={1,2,3,4,5,6,7,8,9},
int i;
printf (“Element \t Value”);
for (i=0;i<10;i++)
printf (“%d \t%d”, i, a [i] );
}
If you put more
values than the array can hold, there will be a syntax error-
main(){
int a[5]={1,2,3,4,5,6,7,8,9,10},
int i;
printf (“Element \t Value”);
for (i=0;i<10;i++)
printf (“%d \t%d”, i, a [i] );
}
If array size is
omitted during declaration, the number of values during array initialization
will be the number of elements the array can hold. For example, the following
array will automatically have space for 5 elements.
main(){
int a[]={1,2,3,4,5},
int i;
printf (“Element \t Value”);
for (i=0;i<10;i++)
printf (“%d \t%d”, i, a [i] );
}
A Simple Program Using Array
The following
program declares an array to hold the marks of 30 students. Firstly, the
program with the help of a loop takes marks of 30 students inside the array.
Then, again with the help of another loop, the program calculates their total
marks. Finally, it delivers the average mark by dividing the total mark by
number students (30)-
Character Arrays
Character arrays
have several unique features. A character array can be initialized using a string
literal.
char
string1[ ] = “first”;
-this
declaration initialized the elements of array string1 to the individual
characters in the string literal “first”. The size of array string1 is
determined by the compilers based on the string.
Note- “first” string literal contains five characters plus a special
string termination character called null character (\0). Thus, string1 array
actually has 6 elements-f, i, r, s, t and \0. A character array representing a
string should always have declared large enough to hold the number of characters
in the string plus the terminating null character.
Character arrays
can be initialized as follows as well-
char
string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘\0’};
This is because
string is really an array of characters. We can access individual characters in
a string directly using array subscript notation. For example, string1 [3] is
the character ‘s’.
We can also
input a string directly from the keyboard using scanf () function and %s
specifier.
char
string1 [20];
scanf
(“%s”, string1);
Note- the name of the array is passed to scanf () without the preceding
& used with other variables. The & is normally used to provide scanf ()
with a variable’s location in a memory so a value can be stored there. Array
name is the address of the start of the array, therefore, & is not
necessary.
Note- scanf () reads characters from the keyboard until the first
whitespace character is encountered- it does not care how large the array is. scanf
() takes upto the first whitespace.
main(){
char string1 [20], string2 [ ] = “Hello
There”;
int i;
printf (“Enter a string with white spaces:
”);
scanf (“%s”, string1);
printf (“\nString 2 is %s”, string2);
printf (\n String 1 is );
for (i=0;string1 [i]!=’\0’;i++)
printf (“%c”, string1 [i]);
}
The output of
the program is-
Enter
a string with white spaces: Hello There
String
2 is Hello There
String
1 is Hello
1 comments:
Very informative aircon chemical wash price singapore
Post a Comment