ARRAYS:
An array is a group of related data
items that share a common name and stored in contiguous memory locations.
Ordinary variables are capable of
holding only one value at a time.
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 of marks obtained by 100 students in ascending
order. In such a case we have two
options to store these marks in memory.
i) Construct
100 variables to store percentage marks obtained by 100 different students.
ii) Construct
one variable capable of storing or holding all the hundred variables.
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.
Array declaration:-
Like any other variable, arrays must be declared before they are
used. The general form of array
declaration is
type variable-name [size];
The type specifies the type of element that will be contained in the
array, such as int, float or char.
Size indicates the maximum number of elements that can be stored inside
the array.
For example
1)
int number[5];
declares an
array number which can store a maximum of 5 integer numbers.
2)
float height[50];
Declares an
array height which can store a maximum of 50 floating-point numbers.
3)
char name[10];
Declares an
array name which can store a maximum of 10 characters.
If we want to represent a set of five numbers say (35, 40, 20, 57, 19) by
an array variable number, then we may declare the variable number as follows.
int number [5];
ACCESSING ELEMENTS OF
AN ARRAY:
Once an array is declared, the
individual elements of the array can be referred by the use of subscript.
Subscript specifies the element position in the array. Subscript starts at 0.
i.e. first number is started at position
0, second number is started at position 1 etc.
Entering data into an array
int num [10];
for (i=0; i<10; i++)
{
Printf (“enter a
number”);
Scanf (“%d”, & num
[i]);
}
Reading data from an array
To add the elements of an array to
variable sum
sum=0;
for (i=0; i<10; i++)
sum
= sum + a [i];
1. An array is a collection of similar
elements.
2. First element is at position ‘0’, so the
last element is at position 1 less then the size of the array.
3. An array is also known as subscripted
variable.
4. Before using an array its type and dimension
must be declared.
5. The
elements are always stored in contiguous memory locations
Initialization of arrays:
We can initialize the elements of an array
in the same way as the ordinary variables when they are declared.
Static type array-name [size] =
{list of values};
The values in the list are separated by
commas. If the number of values in the list is less then the size, then only
that many elements will be initialized. The remaining elements will be set to
zero automatically.
Static int num [5] = {10, 20, 30,
40, 50};
Static char name[ ] = { ‘j’,’o’,’h’,’n’ };
There are two types of arrays:-
1.One-dimensional arrays
2.Two-dimensional arrays.
1.One-dimensional
arrays:-
A
list of related data type items can be given one variable name using single
subscript. Such variable is called single subscripted variable or
one-dimensional array.
Syntax:-
Datatype
variable[size];
Eg.,
int
salary[10];
float marks[100];
char ch[10];
Ø A
Program to initialize an array and display the values.
#include<stdio.h>
#include<conio.h>
void
main( )
{
int i,n,a[5]={11,22,33,44,55};
clrscr( );
for(i=0;i<5;i++)
printf(“%d\n”,a[i]);
getch( );
}
2.Two-dimensional arrays:-
So
far we have looked at 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.
Consider the following data table
Sub1 sub2
sub3 sub4
Student2 5 7 8 15
Student3 3 2 4 50
The table contains a total of 12 values,
i.e in each line. We can think of this table as a matrix consisting of 3 rows
and 4 columns.
Each row represents the marks obtained in
4 subjects by a particular student. Each column represents the marks obtained
in a particular subject.
In mathematics, we represent a particular
value in a matrix by using two subscripts, such as vij. Here v
denotes the entire matrix and vij refers to the value in the ith
row and jth column.
For example in the above table v23
refers to the value 50.
C allows us to
define such tables of items by using two-dimensional arrays, the above table
can be defined in C as
Int v[3][4];
General form:
Type array_name [row-size]
[col-size];
MULTI
DIMENSIONAL ARRAYS:
C language allows not
only one-dimensional and two dimensional arrays but also three or more
dimensional arrays. A multi dimensional array can be declared as follows:-
Syntax:-
Datatype
variable[size1][size2]……[size n];
Eg.,
Int
a[3][3][3]={{{1,2,3}
ARRAYS AND FUNCTIONS:
As values of
variables can be passed to a function it is also possible to use array values
in a function.
To pass a
one-dimensional array to a function, it is sufficient to list the name of the
array without any subscripts and size of the array as arguments.
Eg:-
sum (a,n);
Where a is
the array name and n is the size of the array.
But in
function header, we need to mention the array as a subscripted variable.
Eg:-
sum (int a[ ], int n)
It is not
necessary to specify the size here.
0 comments:
Post a Comment