0% found this document useful (0 votes)
5 views

PPCm3c12

This document provides a comprehensive overview of arrays in programming, covering their definition, declaration, initialization, and various operations such as insertion, deletion, merging, searching, and sorting. It also discusses two-dimensional arrays and their applications in data structures and mathematical computations. Additionally, it includes example programs demonstrating the usage of arrays in C programming.

Uploaded by

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

PPCm3c12

This document provides a comprehensive overview of arrays in programming, covering their definition, declaration, initialization, and various operations such as insertion, deletion, merging, searching, and sorting. It also discusses two-dimensional arrays and their applications in data structures and mathematical computations. Additionally, it includes example programs demonstrating the usage of arrays in C programming.

Uploaded by

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

Module 3

Chapter 12 Arrays
12.1 to 12.12
Introduction
• An array is a collection of similar data
elements of the same data type.
• Stored in consecutive memory locations and
are referenced by an index(subscript).
• Subscript- ordinal number used to identify an
element of array.
12.2 Declaration of Arrays
• Like how we declare every variable we use, we
have to declare array variables too.
• Syntax:
type name[size];
• Example:
int marks[10];
Applications of Arrays

• Storing multiple values: Useful for handling


data like marks, temperatures, etc.
• Matrices and mathematical computations: 2D
arrays represent matrices.
• Data structures: Arrays form the basis of
advanced structures like stacks and queues.
• String manipulation: Strings are essentially
arrays of characters.
Points to remember
• int arr[];
• int n, arr[];
• C never checks validity of array index.
• int i=10;
int arr[N+10], my_arr[i-5*10];

• #include<stdio.h>
#define N 100
main()
{
int arr[N];
..
12.3 Accessing the elements of an array

• Use array subscript.


• To access fifth element…..
• No single statement that can
read,access or print all elements of
the array.
12.3.1 Calculating address of array
elements
• Address of data element,
A[k]=BA(A)+w(k-lower_bound)
 where k is the index of element for which
we have to calculate address
 BA is base address of the array A
 w is the word size
 lower_bound is index of first element in
array
Example:
• Given an array int marks[]= { 99,……}.
Calculate address of marks[4] if
BA=1000.
• Same example for float…
12.3.2 Calculate length
• No of elements stored
• Length= upper_bound- lower_bound+1
12.4 Storing Values in arrays
12.4.1 Initialising Arrays during declaration

• By default arrays not initialised , contains


garbage values.
• type array_name[size]={list of values};
• Example: int marks[5]={99,98,75};
• or int marks[]={99,98,75};
• If number of values are less than number of
elements in array-> unassigned elements filled
with zeros
• int marks[3]={1,2,3,4};
12.4.2 Inputting Values from keyboard
12.4.3 Assigning values to individual
elements
• Use assignment operator
• Example: marks[3]=100;
• a2[3]=a1[2];// is it possible
12.5 Operations on Arrays
• Traversing an array
• Inserting an element in an array
• Deleting an element in an array
• Merging two arrays
• Searching an element in an array
• Sorting an array in ascending or descending
order
12.5.1 Traversing an array

• Traversing means accessing each and every


element of the array.
Program to read and display n numbers
using an array
int main()
{
int i=0,n,arr[20];
printf(“Enter the no of elements”);
scanf(“%d”,&n);
printf(“Enter the elements);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
printf(“Array elements are”);
for(i=0;i<n;i++)
printf(“%d\n”, arr[i]);
return 0;
}
Output
Enter the number of elements: 5
Enter the elements:
Arr[0]=1
.
.
Array elements are
Arr[0]=1 Arr[1]=2……….
12.5.2 Inserting an element in an array
• Adding a new data element to an already
existing array.
Algorithm to insert an element in the middle
of an array/specifies position
• INSERT(A,N,POS,VAL)
INSERT(Data,6,3,100)
Program to insert a number at a given
location in an array
int main()
{
int i,n,num,pos,arr[10];
printf(“Enter the no of elements”);
scanf(“%d”,&n);
printf(“Enter the values”);
for(i=0;i<n;i++)
{
scanf(“%d”,&arr[i]);
}
printf(“Enter the value to be
inserted”);
scanf(“%d”,&num);
printf(“Enter the position”);
scanf(“%d”,&pos);
for(i=n-1;i>=pos;i--)
arr[i+1]=arr[i];
arr[pos]=num;
n++;
printf(“Array after insertion”);
for(i=0;i<n;i++)
printf(“\t%d”,arr[i]);
return 0;
}
12.5.3 Deleting an element from an array
DELETE(A,N,POS)
DELETE(Data,6,2)
Program to delete a number from a given
location in an array.
//size
//elements
//pos
for(i=pos;i<n-1;i++)
arr[i]=arr[i+1];
n--;
//array after deletion
Enter the size of array:5
Enter the elements of array:
12345
Enter the position to be deleted: 3
Array after deletion:
Arr[0]= 1
Arr[1]=2
Arr[2]=3
Arr[3]=5
12.5.4 Merging Two Arrays
• Merging two arrays in a third array means:
– Copying contents of first array into third array
– Copying contents of second array into third array
Write a program to merge two unsorted
arrays
int main()
{
int ……;
printf(“Enter the size of array1”);
scanf(“%d”,&n1);
printf(“Enter the elements in array1”);
for(i=0;i<n1;i++)
scanf(“%d”,&arr1[i]);
//2nd array
m=n1+n2;
for(i=0;i<n1;i++)
{
arr3[j]=arr1[i];
j++;
}
for(i=0;i<n2;i++)
{
arr3[j]=arr2[i];
j++;
}
printf(“Merged Array :”);
for(i=0;i<m;i++)
printf(“Arr[%d]=%d”,i,arr3[i]);
return 0;
}
12.5.5 Searching For a value in an array
• Find whether a particular value is present in
array or not.
• Search is successful or unsuccessful
• Linear search
• Binary search
Linear Search
• Also called sequential search
• Used to search an unordered list
• Compare every element of array one by one in
sequence until a match is found.
• Best case
• Worst case
• Performance can be improved if we use sorted
array
int A[]={10,8,2,7,3,4,9,1,6,5};
VAL=7
POS=?
• Disadvantage of linear search?
Write a program to implement linear search
int arr[10],num,I,n,found=0,pos=-1;
//no of elements
//elements
// no to be searched
for (i=0;i<n;i++)
{
if(arr[i]==num)
{
found=1;
pos=i;
printf(“%d is found in array at position %d”,num,i);
break;
}
}
if(found==0)
printf(“%d does not exist”,num);
return 0;
}
Output:?
Binary Search
• Sorted list
• Telephone directory
• A Binary Search is a sorting algorithm, that is used
to search an element in a sorted array.
• A binary search technique works only on a sorted
array, so an array must be sorted to apply binary
search on the array.
• It is a searching technique that is better then the
liner search technique as the number of iterations
decreases in the binary search.
• The logic behind the binary search is that there is a key.
• This key holds the value to be searched.
• The highest and the lowest value are added and divided by 2.
• Highest and lowest and the first and last element in the array.
• The mid value is then compared with the key.
• If mid is equal to the key, then we get the output directly.
• Else if the key is greater then mid then the mid+1 becomes the
lowest value and the process is repeated on the shortened array.
• Else if the key value is less then mid, mid-1 becomes the highest
value and the process is repeated on the shortened array.
• If it is not found anywhere, an error message is displayed.
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements");
scanf("%d",&n);
printf("Enter %d integers", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list", key);
return 0;
}
Ouput:

Array size: 10

Elements : 3 4 7 6 5 1 2 8 10 9

Ascending : 1 2 3 4 5 6 7 8 9 10

Descending : 10 9 8 7 6 5 4 3 2 1
12.5.6 Sorting an array in ascending or
descending order
C program to sort an array in an ascending order:
#include <stdio.h>
#include<conio.h>
int main()
{
int a[100],n,i,j;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++) //Loop for ascending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] > a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++) //Loop for descending ordering
{
for (int j = 0; j < n; j++) //Loop for comparing other values
{
if (a[j] < a[i]) //Comparing other array elements
{
int tmp = a[i]; //Using temporary variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nDescending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing
array data after sorting
{
printf(" %d ", a[i]); //Printing data
}

return 0; //returning 0 status to system


getch();
}
Output
enter number of elements in an array
5
Enter the elements
12
23
89
11
22
The numbers in ascending order is:
11
12
22
23
89
12.6 Passing arrays to functions
Passing individual data Values
Passing addresses

&
Passing entire array
12.7 Two dimensional arrays
12.7.1Declaring two dimensional arrays
• Syntax:
• data_type array_name[row_size][col_size];
• m(rows)xn(col)array accessed using i and
j,i<=m and j<=n
• Marks of 3 students in 5 subjects..?
• marks[0][0]-1st element
• marks[1][0]-2nd element
Collection of 1D arrays
• Two ways of storing a 2D array in computer
memory:
– Row major order
– Column major order
Address
• Column major order
– Address(A[I][J])=B_A+w{M(J-1)+(I-1)}
• Row major order
• Address(A[I][J])=B_A+w{N(I-1)+(J-1)}
12.7.2 Initializing 2D arrays
• int marks[2][3]={10,20,30,40,50,60};
or
• int marks[2][3]={{10,20,30},{40,50,60}};
• marks[0][0]=10 marks[0][1]=10
• marks[0][2]=30 marks[1][0]=40
• marks[1][0]=50 marks[1][0]=60
Omitting size
• int marks[][3]={{10,20,30},{40,50,60}};
• int marks[2][3]={0};
• int marks[2][3]={{40,50,60}};
• marks[1][2]=79;
12.7.3 Accessing the elements of two
dimensional arrays
• Subscript
• for loop
• 1D array
• 2D array
12.8 Operations on two dimensional arrays

• Transpose
Bi,j=Aj,i
• Sum
Ci,j=Ai,j + Bi,j
• Difference
Ci,j=Ai,j - Bi,j
• Product
Ci,j=ΣAi,k Bk,j for k=1 ,k<n
12.9 Passing 2D arrays to functions
• Passing individual elements/ passing elements
of 1D array
• Passing a row/ passing entire 1D array
• Passing an entire 2D array
12.9.1 Passing a row
12.9.2 Passing an entire 2D array
• Use array name as actual parameter
• Program examples
12.10 Multidimensional Arrays
•Array of arrays
•n indices in an n-dimensional array/multidimensional array
•m1 x m2 x m3 x….mn array is a collection of m1 * m2 * m3 *… mn
elements.
•A[I1][I2][I3]….[In], I1<= M1….
12.12 Applications of Arrays
• Implement vectors,matrices and other
rectangular tables
• Databases- 1D-records
• Implement data structures strings, stacks,
queues, heaps….
• Used for sorting and searching

You might also like