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

3 - Arrays and Functions

Uploaded by

deepakyadav9xm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

3 - Arrays and Functions

Uploaded by

deepakyadav9xm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Arrays and Functions: Detailed

Explanation

1. One-Dimensional Arrays
• A one-dimensional array is a collection of elements
(usually of the same data type) that are stored in
contiguous memory locations.
• The elements in an array are accessed using indices,
which start from 0.
• For example, an array of integers might look like: int
arr[5] = {10, 20, 30, 40, 50};, where arr[0] = 10, arr[1] =
20, etc.
2. Array Manipulation
• Insertion: Adding an element to the array, typically
involves shifting elements to create space.
• Deletion: Removing an element from the array may
require shifting elements to close the gap.
• Accessing: Fetching the value at a specific index, like
arr[3] to get the 4th element.
• Updating: Modifying the value of an element in the
array by directly accessing its index.

3. Sorting
• Sorting arranges elements in a specific order, either
ascending or descending.
• Common sorting algorithms:
o Bubble Sort: Repeatedly swaps adjacent elements
if they are in the wrong order.
o Selection Sort: Selects the smallest (or largest)
element and swaps it with the first unsorted
element.
o Insertion Sort: Builds the sorted array one element
at a time by inserting each new element into its
correct position.
o Quick Sort: Uses a pivot to partition the array into
subarrays and sorts them recursively.
o Merge Sort: Divides the array into halves,
recursively sorts them, and then merges the sorted
halves.
4. Searching
• Searching algorithms help locate an element in the array.
o Linear Search: Iterates through the array element
by element to find the desired value.
o Binary Search: Efficient algorithm for sorted arrays.
It divides the search interval in half repeatedly,
discarding one half after each comparison.

5. Problem Solving Top-Down Approach


• This approach involves breaking a problem into smaller,
more manageable sub-problems.
• At each step, you focus on solving a higher-level
abstraction and gradually move towards lower-level
details.
• It aligns with modular programming, which enhances
code reusability, readability, and debugging efficiency.
6. Modular Programming and Functions
• Modular programming divides a program into
independent, interchangeable modules, where each
module performs a distinct function.
• Functions are a way to implement modular
programming. A function encapsulates a specific task
and can be called when needed, avoiding code
repetition.
• Passing Arguments:
o Call by Value: The function receives a copy of the
argument, so any changes made to the argument
within the function do not affect the original
variable.
o Call by Reference: The function receives a
reference (or address) of the original variable,
allowing changes made inside the function to affect
the original variable.
7. Recursive Functions
• A recursive function is a function that calls itself in order
to solve a problem.
• Recursive solutions often simplify problems that can be
broken down into smaller, similar problems, such as:
o Factorial Calculation: factorial(n) = n * factorial(n-1)
with factorial(0) = 1 as the base case.
o Fibonacci Series: fib(n) = fib(n-1) + fib(n-2) with
base cases fib(0) = 0 and fib(1) = 1.
8. Recursion
• Recursion involves two essential components:
1. Base Case: The simplest form of the problem,
which stops the recursion.
2. Recursive Case: A set of rules that reduces the
problem in complexity, eventually leading to the
base case.
Recursion offers an elegant solution for problems that have a
naturally recursive structure. However, recursive solutions
may lead to high memory usage if not managed carefully
(due to function call overhead), and sometimes iterative
solutions are preferred for efficiency.
Example of a Recursive Function: Factorial

You might also like