0% found this document useful (0 votes)
7 views96 pages

Module-3 Functions & Arrays

The document provides an overview of variable scope, storage classes, and various operations on arrays, including traversing, inserting, deleting, merging, searching, and sorting. It also covers two-dimensional arrays, their operations, and applications in programming. Additionally, it includes sample programs for implementing these concepts in a programming language.

Uploaded by

unicornteddy1421
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)
7 views96 pages

Module-3 Functions & Arrays

The document provides an overview of variable scope, storage classes, and various operations on arrays, including traversing, inserting, deleting, merging, searching, and sorting. It also covers two-dimensional arrays, their operations, and applications in programming. Additionally, it includes sample programs for implementing these concepts in a programming language.

Uploaded by

unicornteddy1421
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/ 96

Variable scope

Block scope
function scope
Program scope
Storage classes
Operations on arrays
• Traversing an array
• Inserting an element in an array
• Deleting an element from an array
• Merging two arrays
• Searching an element in an array-Linear search
and binary search
• Sorting an array in ascending or descending
order
Traversing an array

To traverse an array means to access


each element (item) stored in the array
so that the data can be checked or used
as part of a process
Write a program to initialize 5 integers in an
array a[] and display that elements.
Write a program to accept 5 integers in an
array and display that elements.
Write a program to accept N integers in an
array and display that elements
Inserting an element in an array

Inserting elements into a 1D array involves adding a new


element at a specific position within the array. This often
requires shifting the existing elements to make space for
the new element.
Write a program to insert a number at a
given location in an array
Write a program to insert a number at a
given location in an array
Deleting an element from an array

To delete a specific element from an array, a user


must define the position from which the array's
element should be removed. The deletion of the
element does not affect the size of an array.
Write a program to delete a number from a
given location in an array
Write a program to delete a number from a
given location in an array
Merging two arrays

Combining two arrays into a single array is known as


merging two arrays. For example, if the first array has 5
elements and the second array has 4, the resultant
array will have 9 elements. A merged array is the result
of this process.
Write a program to merge two
sorted/unsorted array
Write a program to merge two
sorted/unsorted array
Linear Search
• Linear search is a sequential searching algorithm where we
start from one end and check every element of the list until
the desired element is found.

• A linear search (often called a sequential search) is performed


by inspecting each item in a list one by one from one end to
the other to find a match for what you are searching for. The
benefit is that it is a very simple search and easy to program.
Write a program to implement Linear Search
Write a program to implement Linear Search
Binary search
• Binary search works on the divide and conquer approach,
• (i. e). the list from which the search is to be done is divided into two
halves, and then the searched element is compared with the middle
element in the array. If the element is found, then the index of the middle
element is returned.
Write a program to implement Binary Search
Write a program to implement Binary Search
Sorting-Bubble Sort
Sorting in 1-D arrays

Learning Objectives
• Analyze sorting techniques in 1D Array.
• Create Java program to sort an array using bubble
sort.
Sub concept
• Sorting– definition
• Bubble sort
• Example C programs
Sorting – definition

• Sorting of an array means arranging the


array elements in a specified order i.e.,
either ascending or descending.
Bubble Sort
• It is a sorting technique. The idea of bubble
sort is to move the largest element to the
highest index position in the array.

• To attain this, two adjacent elements are


compared repeatedly and exchanged if they
are not in correct order.
Passing arrays to functions
In C, the whole array cannot be passed as an argument to a
function. However, you can pass a pointer to an array without
an index by specifying the array’s name.

int array(int arr[],int


size)
Passing arrays to functions
Two dimensional array

• Declaration

• Initialization

int a[3][3]={{1,2,3},{1,2,3},{1,2,3}};
Write a 2D array program to initialize an
array a[][] and print that array elements
Write a 2D array program to accept an
array a[][] and print that array elements
Operations on two dimensional arrays
Transpose
The transpose of a matrix is found
by interchanging its rows into columns or
columns into rows. The transpose of the matrix
is denoted by using the letter “T” in the
superscript of the given matrix.
Write a program to transpose a 3x3 matrix.
Sum
Two matrices can be added together if
and only if they have the same dimension.
Their sum is obtained by summing each
element of one matrix to the corresponding
element of the other matrix.
Write a program to find sum of two 3x3 matrices
Difference
Subtraction of Matrices is possible when two matrices are of
the same order. The difference of the matrices A and B is
represented as Dij =Aij –Bij
Write a program to find difference of two 3x3 matrices
Product
The product of two matrices will be defined if
the number of columns in the first matrix is
equal to the number of rows in the second matrix. If
the product is defined, the resulting matrix will have
the same number of rows as the first matrix and the
same number of columns as the second matrix.
Write a program to find multiplication of
two 3x3 matrices
Passing 2D arrays to functions
Passing 2D arrays to functions
Multidimensional arrays
• A multi-dimensional array is an array that has
more than one dimension. It is an array of
arrays; an array that has multiple levels. The
simplest multi-dimensional array is the 2D
array, or two-dimensional array.
Applications of arrays
• Used to store values of the same data type.
• Used in mathematical problems.
• To implement Search Algorithms.
• To implement Sorting Algorithms.
• Arrays are used to implement Data structures, like
a
stack, queue, etc.,
• Used for implementing matrices

You might also like