Arrays can be defined as a set of pair-index and the value. Two basic operations that can be performed on arrays are: Storing of data at desired location or index and retrieving data from desired location (index).
Arrays
AU
May-19, Marks 7
Arrays
can be defined as a set of pair-index and the value. Two basic operations that
can be performed on arrays are: Storing of data at desired location or index
and retrieving data from desired location (index).
Advantages
of Arrays
1.
Elements can be retrieved or stored very efficiently in array with the help of
index or memory location.
2.
All the elements are stored at continuous memory locations. Hence searching of
element from sequential organization is easy.
Disadvantages
of Arrays
1.
Insertion and deletion of elements becomes complicated due to sequential
nature.
2.
For storing the data large continuous free block of memory is required.
3.
Memory fragmentation occurs if we remove the elements randomly.
Syntax
The
syntax of declaring an array is
data_type
name_of_array [size];
For
example, int a [10]; double b[10] [10];
Here
'a' is the name of the array inside the square bracket size of the array is
given. This array is of integer type i.e. all the elements are of integer type
in array 'a'. Similarly arrays may be of user defined type, called as array of
structure C supports both one dimensional and multidimensional arrays.
•
It is possible to initialize an array during declaration only. For example
int
a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
•
The list of values, enclosed in braces {}, separated by commas, provides the
initial values for successive elements of the array.
•
If there are fewer initializers than elements in the array, the remaining
elements are automatically initialized to 0. For example,
int
a[10] = (0, 1, 2, 3, 4, 5, 6);
would
initialize a[7], a[8], and a[9] to 0.
.
When an array definition includes an initializer, the array dimension may be
omitted, and the compiler will infer the dimension from the number of
initializers. For example,
int
b[ ] = {10, 11, 12, 13, 14};
would
declare, define, and initialize an array b of 5 elements. Here only the
dimension is omitted; the brackets [] remain to indicate that b is in fact an
array.
Array
can be one dimensional and two dimensional.
For
example
You
can visualize one dimensional array as :
Note
that the elements in array are always stored in contiguous memory locations.
Now
let us see how to handle this array. We will write a simple C program in which
we are simply going to store the elements and then we will print those stored
elements.
C
Program
#include<stdio.h>
#include<conio.h>
main(
)
{
int
a[10],index;
clrscr(
);
printf("\n
Store The Elements In An Array a");
for(index=0;index
< = 9;index++)
scanf("%d",&a[index]);
printf("\n
You have stored these numbers in the array a");
for(index=0;index
< =9;index++)
printf("\n
%d", a[index]);
}
The
two dimensional array is something which you can compare with the two storied
building! Extra space which is arranged in rows and columns. Let us draw a
figure which will represent the two dimensional array.
The
syntax of two dimensional array is :
Data
_ type Name_Of_array[row_size][column_size];
For
example
int
a[10][10];
By
this allocation the memory block of 10 x 10 =100 is getting created. The nested
for loops can be effectively used to access all the elements of an two dimensional
array. We will take a sample example to explain this idea. bon atomolo orb
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
scanf("%d",&a[i][j]);
}
}
Execution
of Nested for Loop
Initially
the value of i=0 at that time
j-0 it will store the element in a[0][0]
j-1
it will store the element in a[0][1]
j-2
it will store the element in a[0][2]
Next
time i will be incremented by 1 and now i=1
Again
j=0
it will store the element in a[1][0]
j-1
it will store the element in a[1][1]
j-2
it will store the element in a[1][2]
Next
time i will be incremented by 1 and now i=2
j=0
it will store the element in a[2][0]
j-1
it will store the element in a[2][1]
j-2
it will store the element in a[2][2]
Since
now value of i and j has reached to 2 the scanning procedure will get
terminated as condition is i<=2 and j<=2. Thus all the elements from
a[0][0] to a[2][2] get scanned.
Ex.
1.10.1 How two dimensional arrays are created in C? Write a C program to
generate a population survey having citizen's record stored as a collection of
year-wise population.
Sol.
:
Two dimensional array - Refer section 1.10.3
#include
<stdio.h>
int
main( )
{
int
arr[3][3],i,j;
for
(i=0;i<3;i++)
{
printf("\nEnter
the population for country#%d", (i+1));
for
(j=0;j<3;j++)
{
printf("\n
Enter the population for Year#%d", (j+1));
scanf("%d",&arr[i][j]);
}
}
printf("\n
printing the population ....\n");
printf("\n\t
country#1 country#2 country#3\n");
printf("\n-----------------------\n");
for(i=0;i<3;i++)
{
printf("Year#%d
", (i+1));
for
(j=0;j<3;j++)
{
printf("\t%dL",
arr[i][j]);
}
printf("\n");
}
return
0;
}
C Programming and Data Structures: Unit I: C Programming Fundamentals : Tag: : Advantages, Disadvantages, Syntax, Example C programs, Initialization - Arrays
C Programming and Data Structures
CS3353 3rd Semester EEE, ECE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation