Arrays 1
Arrays 1
Topics To be Cover
• Introduction
• Advantages & Dis-advantage
• Types of Arrays.
• Declaring Arrays
• Initializing Arrays
• Accessing Array Elements
INTRODUCTION
Array: collection of fixed number of components
(elements), wherein all of components have same
data type
• Consecutive group of memory locations that all
have the same type
• The collection of data is indexed, or numbered,
and at starts at 0
• Position number is formally called
the subscript or index
• First element is subscript 0 (zero), sometimes
called the zeroth element.
• The highest element index is one less than the
total number of elements in the array
Dis-advantages:
• Fixed Size: Whatever size, we define at the
time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size
dynamically like LinkedList which we will
learn later.
Types of Arrays:
#include <stdio.h>
int main () {
return 0;
}
• When the above code is compiled and executed, it
produces the following result
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Using Bubble Sort Method To Sort The Array In Ascending Order
#include<stdio.h>
void main ()
{
int i, j,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}
OUTPUT:
Printing Sorted Element List...
101
78
44
34
23
23
12
10
9
7
Multi – Dimensional Array:
• The two-dimensional array can be defined as an array of
arrays.
• 2D arrays are created to implement a relational database
lookalike data structure.
DECLARATION(2D)
• Syntax
data_type array_name[rows][columns];
INITIALIZATION(2D)
• Ex:
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
Two Dimensional Array Example In C
#include<stdio.h>
int main()
{
int i=0,j=0;
int arr[43]1,2,3,2,3,4,3,4,5,4,5,6;
//traversing 2D array
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78
56 10 30
34 21 34
45 56 78
Passing Array to a Functions:
#include <stdio.h>
void getarray(int arr[])
{
printf("Elements of array are : ");
for(int i=0;i<5;i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[545,67,34,78,90;
getarray(arr);
return 0;
}
OUTPUT:
In the above program, we have first created the array arr[] and
then we pass this array to the function getarray().
The getarray() function prints all the elements of the array arr[].
Applications Of An Array:
• Arrays are the simplest data structures
that stores items of the same data type. A
basic application of Arrays can be storing
data in tabular format. For example, if we
wish to store the contacts on our phone,
then the software will simply place all our
contacts in an array.
• 2D arrays, commonly known as, matrix,
are used in image processing.
• A simple question Paper is an array of
numbered questions with each of them
assigned to some marks.
THANK YOU