0% found this document useful (0 votes)
21 views26 pages

Arrays 1

Uploaded by

suryafootball01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views26 pages

Arrays 1

Uploaded by

suryafootball01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

ARRAYS

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

• The lowest address corresponds to the first


element and the highest address to the last
element.
Advantages of C Array:
• Code Optimization
• Ease of Traversing
• Ease of Sorting
• Random Access

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:

• One – Dimensional Array(1D)


Elements(or components) are arranged in list
form
• Multi – Dimensional Array (2D, 3D, 4D, Etc.,)
Elements are arranged in tabular form
Declaring Arrays (1D)
• Syntax
data_type arrayName [ arraySize ];
• This is called a single-dimensional array.
• The arraySize must be an integer constant
greater than zero and type can be any valid C
data type.
• For example, to declare a 10-element array
called balance of type double, use this
statement − double balance[10];
• Here balance is a variable array which is
sufficient to hold up to 10 double numbers
Initializing Arrays(1D)
• 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 of elements
that we declare for the array between square
brackets [ ].
Cont…..

• 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 before:
Accessing Array Elements(1D)
• An element is accessed by indexing the array
name. This is done by placing the index of the
element within square brackets after the name
of the array.
• For example −
double salary = balance[9];
• The above statement will take the 10th element
from the array and assign the value to salary
variable.
• The following example Shows how to use all
the three above mentioned concepts viz.
declaration, assignment, and accessing arrays

#include <stdio.h>

int main () {

int n[ 10 ]; /* n is an array of 10 integers */


int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100; /* set element at location i to i + 100
*/
}

/* output each array element's value */


for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %d\n", j, n[j] );
}

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[43]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++)
{

printf("arr[%d] %d = %d \n",i,j,arr[i][j]);


}//end of j
}//end of i
return 0;
}
OUTPUT:
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
C 2D array example: Storing elements in a matrix and printing it.
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf(“\n Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
OUTPUT:
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34

Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78

printing the elements ....

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[545,67,34,78,90;
getarray(arr);
return 0;
}
OUTPUT:

Elements of array are : 45 67 34 78 90

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

You might also like