Introduction
► Arrays
are also known as subscript variable.
► Array
is a collection of similar elements
► Whatever
may the size of array, it always consumed memory in a contiguous manner
Need of array
Till now we have been designing solution to small problems
that require less number of variables to handle program data. Think about a
scenario where you need to handle hundreds of variables or even more than that.
In such scenario, you might be thinking about what variable
names should be used, how to reduce redundant code, etc.
Assume you have to store marks of 100 students and then
think about the following:
1) What
could be your variable naming convention?
2) How
you efficiently write input instruction to store 100 data.
3) How
could you easily manipulate data like adding all of them in a less complex
style?
The answer to all these questions is subscript notation also
known as arrays.
Array Declaration
When you want to create large number of variables you need
not to think about 100s of names. Assume that you want to create 100 variables
to store marks of 100 students.
Here is the way:
int marks[100];
1) Notice
square brackets after array name marks. This pair of square brackets is used to
depict array usage.
2) Number
mention in the square bracket is size of array. In our example we wrote 100, so
size of the array is 100. Thus we have declared 100 variables in one go.
3) These
100 variables are all of type int. Since array is a collection of similar
elements, data type for all 100 variables is int.
4) Name
of the first variable is marks[0], second variable is marks[1], and so on. Therefore
the last variable is marks[99]. Here it is important to note that in C language
array indexing starts with 0 and not from 1.
Example: Program to calculate average of 10 marks
#include<stdio.h>
#include<conio.h>
main()
{
int i,
marks[10], sum=0;
float avg;
clrscr();
printf(“Enter
10 numbers”);
for(i=0;i<=9;i++)
scanf(“%d”,&marks[i]);
for(i=0;i<=9;i++)
sum=sum+marks[i];
avg=sum/10.0;
printf(“Average is %f”,avg);
getch();
}
Explanation:
1) Total
numbers of variables in this program are 13. Their names are i, sum, avg,
marks[0], marks[1],…marks[9].
2) Notice
the input statement, scanf() is repeated 10 times, this is possible as we can
access array index with the help of variable. Observe &marks[i] in scanf(), here i is used for indexing. As the loop
proceed value of i changes from 0 to 9. In this way wee need not to write
scanf() 10 times.
3) Data
manipulation becomes also easy, we added all 10 values stored in an array
putting statement sum=sum+marks[i]
in the loop.
4) Since
we have an array of size 10 and each of these blocks are of type int, to tal
memory consumed for this array is 20 bytes (2 bytes for each).
One dimension array
To use an array variable in a
program, it must be declared. When defining an array in a program, three things
need to be specified.
► What
kind of data it can hold, i.e., int, char, double, string, etc.
► How
many values it can hold, i.e., the maximum number it can hold
► A
name
In the previous example the array declared known as one
dimensional array.
Note: According to our previous example marks is not a
variable name but marks[0] is a variable. Thus we can not assign anything to
marks like marks=5;
Two dimension array
Two dimensional arrays is actually array of arrays. So here
we are creating an array of several identical arrays.
Consider the following declaration style
int a[2][3];
this declaration means, we have an array of 2 arrays
containing 3 int blocks each.
1) Total
numbers of blocks are 6, they are all of type int. Memory allocation done is
always sequential but we can assume it as two arrays each of size 3.
2) Logically
we can see it as a row column structure. First row is your 0th array
and second row is 1st array.
3) Two
dimensional arrays are used to handle data which is logically two dimensional
like matrix.
Example: Program to
add two matrix of order 3 x 3.
#include<conio.h>
#include<stdio.h>
main()
{
int a[3][3],b[3][3],c[3][3];
int i,j;
clrscr();
printf("Enter 9 numbers for first
matrix\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Enter 9 numbers for
second matrix\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nSum of matrix is:
\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d
",c[i][j]);
printf("\n");
}
getch();
}
Explanation:
1) Notice
the declaration of arrays. We have declared three two dimensional arrays.
2) Observe
the style of input. scanf() is repeated with nested loop.
3) Lastly
sum of corresponding elements of two matrices are added and stored in third
array.
4) Finally
sum is displayed on the screen
Initialization of
array at the time of declaration
Initializing one dimension array
int a[5]={
23,45,11,67,55};
int a[ ]= {
23,45,11,67,55};
int a[5]= {22,45};
int a[5]=
{12,23,76,85,43,33};
In the first style we declared an array with size 5 and
assign 5 values to them, First value is stored in a[0] and last in a[4].
Second style is also valid. When you initialize array at the
time of declaration it is not necessary to mention size of array, otherwise it
is compulsory. Compiler assumes the size of array by counting number of values
assigned to it.
Third notation style is also valid as two variables a[0] and
a[1] initializes with 22 and 45, remaining variables are initialized to 0.
Fourth style leads to compilation error. You can not
initialize an array with data more than the size of array.
Initialization of two dimension array
int b[2] [3] =
{12,65,78,45,33,21};
int b[ ] [3] =
{12,65,78,45,33,21};
int b[2] [ ] = {12,65,78,45,33,21};
int b[ ] [ ] =
{12,65,78,45,33,21};
Last two styles are invalid and lead to compile time error.
thanks for providing c programming examples with output
ReplyDelete