Arrays A Kind of Data Structure That Can Store A Fixed-Size Sequential Collection of Elements of The Same Type
Arrays A Kind of Data Structure That Can Store A Fixed-Size Sequential Collection of Elements of The Same Type
1 Introduction:
Arrays a kind of data structure that can store a fixed-size
sequential collection of elements of the same type.
An array is used to store a collection of data, but it
is often more useful to think of an array as a collection of
variables of the same type.
Instead of declaring individual variables, such as number0,
number1, ..., and number99, you declare one array variable such
as numbers[] and use
numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables.
A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest address
to the last element.
Array :
Definition:An array is a collection of homogenous data stored
under unique name. values in an array are called as ‘elements
of an array’. Array is The The first index in the array is
numbered 0, so that the last index is 1 less than the size of
array.
1. An array is also known as a subscripted variable.
2. Before using an array its type & dimensions must be
declared.
3. However big an array its elements are always stored in
contagious memory locations.
Index (
0 1 2 3 4
x)
Value
12 12 45 15 1
a[x]
Locati
2002 2004 2006 2008 2010
on
Types of an Array
1. One / Single Dimensional Array
2. Two Dimensional Array
Initializing Arrays
You can initialize an array in C either one by one or using a
single statement as
follows:
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between braces { } cannot be larger than
the number ofelements that we declare for the array between
square brackets [ ].
If you omit the size of the array, an array just big enough to hold
the initialization is created. Therefore, if you write:
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
You will create exactly the same array as you did in the previous
example.
Following is an example to assign a single element of the array:
balance[4] = 50.0;
The above statement assigns the 5th element in the array
with a value of 50.0.
All arrays have 0 as the index of their first element which is
also called the base index and the last index of an array will
be total size of the array minus 1.
Shown below is the pictorial representation of the array we
discussed above:
Examples:
int a[3] = {2, 3, 5};
char ch[20] = "Deesha" ;
float stax[3] = {5003.23, 1940.32, 123.20} ;
Total Size (in Bytes):
total size = length of array * size of data type
In above example, a is an array of type integer which has storage
size of 3 elements. The total size would be 3 * 2 = 6 bytes.
* Memory Allocation :
getch();
}
e.g. Write a program for addition & subtraction of one
dimensional matrix
void main()
{ int a[5],b[5],i,s=0,c[5],d[5],s1=0;
clrscr();
printf("enter 5 nos. of a[i]=\n");
for(i=0;i<5;i++)
scanf("%d" ,&a[i]);
printf("enter 5 nos. of b[i]=\n");
for(i=0;i<5;i++)
scanf("%d" ,&b[i]);
for(i=0;i<5;i++)
c[i]=a[i]+b[i];
printf("TOTAL SUM=\n");
for(i=0;i<5;i++)
printf("%d\n" ,c[i]);
printf("TOTAL SUBTRACTION=");
for(i=0;i<5;i++)
d[i]=a[i]-b[i];
for(i=0;i<5;i++)
printf("%d\n" ,d[i]);
getch();
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of 2nd 3*3 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("Summasion of matrices is :\n");
for(i=0;i<3;i++)
{
printf("\t\t\t\tÝ ");
for(j=0;j<3;j++)
printf("%3d ",c[i][j]);
printf("Ý \n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
}
Output:-
Ex:1
WAP to input an Array of 5 elements and display the sum of all
values
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5],sum=0,i;
clrscr();
printf("\n\t Enter 5 Array values :");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<5;i++)
{
sum = sum + arr [i];
}
printf("\n\t Addition = %d",sum);
getch();
}
Ex:2
WAP to input marks of 10 students and display the sum ,average
and variance of marks.