Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

DATA STRUCTURES & ALGORITHM LESSON REVIEWER, Study notes of C programming

A Study Tool for Data Structures & ALgorithm in C Programming Language

Typology: Study notes

2022/2023

Available from 08/07/2024

princess-anne-soriano-dayrit
princess-anne-soriano-dayrit 🇵🇭

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LESSON 1 DATA STRUCTURES
Arrays
Functions
Conditional Statements
Switch Case
Printf/Scanf
Stacks- “last in, first out”
Queues- “first come, first serve” / “first in first out”
Trees- “root is on top”
Graphs- “relation”
Maps- “routes”
Sets- “collection”
DATA STRUCTURE
Data
- “raw” / “unprocessed facts”
- may be arranged in many dierent ways.
- logical or mathematical model.
a.1) Linear Data Structure
Static Data Structure
- Array (“store multiple items”
Dynamic Data Structure
- Queue (“follows particular order” FIFO)
- Stack (“follows particular order” LIFO or FILO)
- Linked List (elements linked using pointers)
a.2) Non-linear Data Structure
Tree (“finite set of data items” “top/root nodes”)
Graph
LINEAR DATA STRUCTURE
- said to be linear if its elements combine to form any
specific order.
- There are basically two techniques of representing
such linear structure within memory.
FIRST WAY
- provide the linear relationships among all the elements
represented by means of “linear memory allocation”
- These linear structures are termed as “arrays”.
SECOND WAY
- to provide the linear relationship among all the
elements represented by using the “concept of pointers
or links”.
- These linear structures are termed as “linked lists”.
NON-LINEAR DATA STRUCTURE:
- (One to many, many to many)
Tree
- Represented by a pointer to the topmost node
tree.
- No value = “null root”
- data often contain a hierarchical relationship
among various elements.
- a data structure that reflects this relationship is
termed as a rooted tree graph or a tree.
Graph
- Capable of representing many kinds of
physical structures.
- data sometimes hold a relationship between the
pairs of elements which is not necessarily
following the hierarchical structure.
- Such data structure is termed as a Graph.
a.) Edge
b.) Vertices
a.) Directed Graph b.) Undirected Graph
TYPES OF GRAPHS
a.) Directed
b.) Undirected
c.) Simple
d.) Weighted
e.) Connected
f.) Non Connected
ARRAY
Declaration (int age;)
- Specify the type of its elements & the array
name.
- Existence of an entity in the program and its
location.
- Syntax: arrayName[arraysize];
Initialization (age = 20;)
- Process of “assigning value to the variable”.
- You can initialize an array at the time of
declaration by providing a comma-separated
list of values enclosed in curly braces {}.
- Number of values should match the size of the
array.
Arrayname = name of the array
Index = the position of the element starts w/ (0)
(elements)
0, 1, 2, 3, 4, (index)
ARRAY
- “container of data types”
- a container which can hold a fix number of items and
these should be of the same type.
- “store multiple items”
IMPORTANT TERMS TO UNDERSTAND THE
CONCEPT OF ARRAY.
Element
- Each item stored in an array is called an
element.
Index
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download DATA STRUCTURES & ALGORITHM LESSON REVIEWER and more Study notes C programming in PDF only on Docsity!

LESSON 1 DATA STRUCTURES

● Arrays ● Functions ● Conditional Statements ● Switch Case ● Printf/Scanf Stacks- “last in, first out” Queues- “first come, first serve” / “first in first out” Trees- “root is on top” Graphs- “relation” Maps- “routes” Sets- “collection” ➢ DATA STRUCTURE ● Data

- “raw” / “unprocessed facts”

  • may be arranged in many dierent ways.
  • logical or mathematical model. a.1) Linear Data Structure ● Static Data Structure
    • Array (“store multiple items” ● Dynamic Data Structure
    • Queue (“follows particular order” FIFO)
    • Stack (“follows particular order” LIFO or FILO)
    • Linked List (elements linked using pointers) a.2) Non-linear Data Structure ● Tree (“finite set of data items” “top/root nodes”) ● Graph ➢ LINEAR DATA STRUCTURE
  • said to be linear if i ts elements combine to form any specific order.
  • There are basically two techniques of representing such linear structure within memory. ● FIRST WAY
  • provide the linear relationships among all the elements represented by means of **“linear memory allocation”
  • These linear structures are termed as “arrays”.** ● SECOND WAY
  • to provide the linear relationship among all the elements represented by using the “concept of pointers or links”.
  • These linear structures are termed as “linked lists”. ➢ NON-LINEAR DATA STRUCTURE: - (One to many, many to many) ● Tree
    • Represented by a pointer to the topmost node tree. - No value = “null root”
    • data often contain a hierarchical relationship among various elements.
    • a data structure that reflects this relationship is termed as a rooted tree graph or a tree. ● Graph - Capable of representing many kinds of physical structures. - data sometimes hold a relationship between the pairs of elements which is not necessarily following the hierarchical structure. - Such data structure is termed as a Graph. a.) Edge b.) Vertices a.) Directed Graph b.) Undirected GraphTYPES OF GRAPHS a.) Directed b.) Undirected c.) Simple d.) Weighted e.) Connected f.) Non Connected ➢ **ARRAY Declaration (int age;)
  • Specify the type of its elements & the array name.
  • Existence of an entity in the program and its location.**
  • Syntax: arrayName[arraysize]; **Initialization (age = 20;)
  • Process of “assigning value to the variable”.
  • You can initialize an array at the time of declaration by providing a comma-separated list of values enclosed in curly braces {}.
  • Number of values should match the size of the array. ● Arrayname = name of the array ● Index = the position of the element starts w/ (0) (elements) 0, 1, 2, 3, 4, (index)** ➢ ARRAY
  • “container of data types”
  • a container which can hold a fix number of items and these should be of the same type.
  • “store multiple items” ➢ IMPORTANT TERMS TO UNDERSTAND THE CONCEPT OF ARRAY. ● Element
  • Each item stored in an array is called an element. ● Index
  • Each location of an element in an array has a numerical index, which is used to identify the element. ➢ ARRAY
  • Array Representation: (Storage Structure)
  • can be declared in various ways in dierent languages
  • C array declaration ➢ C ARRAY DECLARATION ● Datatype varname [size]; ● Datatype varname [] = {elmt1, elmt2, elmt3};C ARRAY DECLARATION - as per above illustration,, following are the important points to be considered.
  • Index starts with 0.
  • Array length is 10 which means it can store 10 elements
  • Each element can be accessed via its index. ➢ ARRAY BASIC OPERATIONS
  • Following are the basic operations supported by an array. ● Traverse
  • Print all the array elements one by one. ● Insertion
  • Adds an element at the given index. ● Deletion
  • Deletes an element at the given index or by the value. ● Update
  • Updates an element at the given index. ➢ ARRAY ADVANTAGES ● Random access ● Easy sorting and iteration ● Replacement of multiple variables

➢ ARRAY DISADVANTAGES

● Size is fixed ● Dicult to insert and delete ● If capacity is more and occupancy less, most of the array gets wasted ● Needs contiguous memory to get allocated.

LESSON 2 ARRAY TRANSVERSAL

➢ ARRAY TRANSVERSAL

  • the process of accessing each element in an array one by one.
  • Traversing an array is a common task in programming, as it allows you to perform operations on individual elements or analyze the entire array.TRAVERSING ARRAYS USING LOOPS
  • To transverse an array, you can use loops such as the for loop or while loop.
  • These loops provide a convenient way to iterate through the array and access each element. ➢ USING A FOR LOOP
  • Loop is widely used for array transversal, as it allows you to specify the start and conditions as well as the increment/decrement for the loop variable.USING A WHILE LOOP
  • Alternatively, you can use a while loop for array transversal.
  • The while loop is suitable when you have a specific condition for terminating the loop.
  • Here’s an example of using loop to transverse an array:

➢ SEARCHING AN ARRAY

  • Searching algorithms are fundamental techniques used in computer science to find a specific value within a collection of data, such as an array.
  • The goal of searching is to determine whether a given element exists in the array, if so, its position or index. ➢ IMPORTANCE OF SEARCHING IN COMPUTER SCIENCESearching is a critical operation in computer science as it underpins many other algorithms and applications. Some key reasons why searching is vital include: ● Data Retrieval: Searching enables ecient retrieval of specific data from large datasets, allowing for quick access and manipulation of information.Database Queries: Databases utilize search algorithms to quickly locate records based on specified criteria, ensuring faster response times for queries.Sorting Algorithms: Searching is o ften employed in sorting algorithms to determine the correct position of elements during the sorting process.Information Retrieval: In search engines and information retrieval systems, searching algorithms help users find relevant information quickly.Optimization Problems: Searching is a crucial component in solving optimization problems, such as finding the minimum or maximum value in a dataset.Sorting Algorithms: Searching is often employed in sorting algorithms to determine the correct position of elements during the sorting process.Information Retrieval: In search engines and information retrieval systems, searching algorithms help users find relevant information quickly.Optimization Problems: Searching is a crucial component in solving optimization problems, such as finding the minimum or maximum value in a dataset.DIFFERENT TYPES OF SEARCHING METHODS ● Linear search
    • Linear search, also known as sequential search, is a simple and straightforward searching algorithm. It involves sequentially checking each element in the array until the target element is found or the end of the array is reached. ● Binary search
    • Binary search is a more ecient searching algorithm, but it requires the array to be sorted in ascending order. It works by repeatedly dividing the search interval in half and narrowing down the search space until the target element is found. ➢ LINEAR SEARCH Steps to follow while using Search Algorithm
  1. Initially, we transverse the array of elements
  2. Now compare the array element with the search element in each iteration
  • If it’s a correct match, then the index of the corresponding array element will return.
  • If it doesn’t match, then it will switch to the next element.
  1. If the search element doesn’t exist in the array or there is no match, the given array returns. ➢ LINEAR SEARCH - ALGORITHMS Linear Search (Array A, Value X) Step 1: Set i to I Step 2: if i > n, then jump to step 7 Step 3: if A [i] = x then jump to step 6 Step 4: Set i to i + I Step 5: Go to step 2 Step 6: Print element x found at index i and jump to step 8 Step 7: Print element not found Step 8: Exit ➢ LINEAR SEARCH - ALGORITHMS ● Let K = 17 be the element to be searched ● We will compare K with each of the elements until we find K= ➢ DIFFERENT TYPES OF SEARCHING METHODS ● Linear Search ● Implementation in C:

➢ BINARY SEARCH

  • Binary Search is defined as a searching algorithm used in a sorted array by repeatedly dividing the search interval in half.Conditions for when to apply Binary Search in a Data Structure:
    • To apply Binary Search algorithm:
    • The data structure must be sorted
    • Access to any element of the data structure takes constant time. ➢ BINARY SEARCH - ALGORITHMS
  1. Divide the search space into two halves by finding the middle index “mid”.
  2. Compare the middle element of the search space with the key.
  3. If the key is found at the middle element, choose which half will be used as the next search space.
    1. If the key is not found at the middle element, choose which half will be used as the next search space.
    • If the key is smaller than the middle element, then the left side is used for the next search.
    • If the key is larger than the middle element, then the right side is used for the next search.
    1. This process is continued until the key is found or the total search space is exhausted.

➢ SEARCHING ALGORITHMS

- Essential tools in computer science, enabling ecient data retrieval and influencing various applications and algorithms. ● LINEAR SEARCH

  • Suitable for “small unsorted arrays” ● BINARY SEARCH
  • Provides significant performance improvements for “large sorted arrays”.

Examples: ➢ ACCESSING ARRAY ELEMENTS

  1. To create an array of integers named numbers that can store 5 elements, you can use the . Code: (creates an array of integers with a size of 5)
  2. To access an element of an array, you use the following syntax: ● arrayName= name of the array ● Index= position of the element you want to access.
  3. To actress the first element of the numbers array created earlier, you can use the following code: ➢ MODIFYING ARRAY ELEMENTS
  4. To modify an element, you can use the following syntax: ● arrayName= name of array ● Index= position of the element you want to modify ● newValue= the new value you want to assign
  5. Change the value of the second element of the numbers array to 10? ➢ **TRAVERSING ONE-DIMENSIONAL ARRAYS
  6. Traversing an array means accessing and** processing each element of the array.
    • You can use loops, such as the for loop or the while loop, to iterate over the elements of an array and perform operations on them.
    • Example that uses a for loop to print the elements of the numbers array: ● Printf statement prints each element of the array on the console. ➢ CREATING & MODIFYING ARRAY ELEMENTS:TRAVERSING & PRINTING ARRAY ELEMENTS:ONE-DIMENSIONAL ARRAYS in C
  • are powerful tools for storing and manipulating collections of data.
  • by understanding how to create, manipulate, traverse, and modify array elements, you can eciently work with large amounts of data and perform various operations on them. ➢ ARRAYS
  • are widely used in programming and are essential concepts for building complex algorithms and data structures.

LESSON 3 MULTI-DIMENSIONAL ARRAY

CREATING MULTI-DIMENSIONAL ARRAYS

  • To create a multidimensional array in C:
  • Declare the array and specify its dimensions.
  • The general syntax for creating a two-dimensional array is as follows: Examples: ➢ CREATING MULTI-DIMENSIONAL ARRAYS
  • Create a two dimensional array of integers named matrix with 4 rows and 4 columns.

LESSON 3 ACCESSING & MANIPULATING

ELEMENTS IN MULTIDIMENSIONAL

ARRAYS

1. Access an element in a multidimensional array: ● To access an element in a multidimensional array: ● Specify both the row index and the column index. ● The indices start from 0 for the first row/column and go up to rowSize-1 and columnSize-1 respectively.

  1. Access the element in the second row and third column of the matrix array?
  2. Modify an element in a multidimensional array, you can use the same syntax:
  3. Change the value of the element in the first row and second column of the matrix array to 10?
  4. Creating and accessing elements in a two-dimensional array:
  5. Traversing and printing elements in a two-dimensional array: ➢ IMPORTANCE OF MULTI-DIMENSIONAL ARRAYS
  • Multi-dimensiona l arrays are essential for storing and manipulating structured data in programming.
  • They allow you to represent complex data structures such as matrices, tables, grids, and more.
  • With multi-dimensional arrays, you can eciently organize and access data in a tabular format, making it easier to perform calculations, analyze data, and implement algorithms. ➢ MULTIDIMENSIONAL arrays in C programming allow you to store and manipulate data in a tabular format.
  • Created by declaring the array with specified row and column sizes.
  • Accessing and modifying elements in a multi-dimensional array is done by specifying the row and column indices.
  • Important for organizing and working with structured data, such as matrices, tables, and grids.
  • Widely used in various fields, including scientific computing, image processing, game development, and data analysis, providing an ecient way to handle structured data and solve complex problems.

FUNCTION DEFINITION & DECLARATIONCALLING FUNCTIONS ● To call a function, use its name followed by parentheses (). ● If the function takes arguments, provide the values inside the parentheses. ● If the function returns a value, you can store it in a variable or use it directly in an expression. ● By breaking down a program into smaller functions, developers can easily manage and update specific parts of the code without aecting the entire program.Proper usage of functions enhances code modularity and promotes good programming practices. EXAMPLES: OUTPUT: Result is : 8 OUTPUT: Result is : 8

LESSON 4 ACTUAL VS. FORMAL

ACTUAL PARAMETERS

  • also known as “arguments” , are the values that are passed to a function when it is called.
  • These values can be literals, variables, expressions, or even other function calls.
  • Actual parameters are used to provide the necessary data to perform the desired operations within the functions.FORMAL PARAMETERS
  • also referred to as “parameters or function parameters”, are placeholders defined in the function declaration or definition.
  • they act as local variables within the function and receive the values passed as actual parameters during function calls.
  • Formal parameters ar e “used to receive the data needed to perform operations within the function.”PASSING VALUES BY REFERENCE OR BY VALUE ● PASSING BY VALUE
    • By default, C passes parameters by value. - Passing by value means that a copy of the value is made and passed to the function.
    • Any changes made to the formal parameters within the function do not aect the original value of the actual parameters. ● PASSING BY REFERENCE
    • Passing by reference allows functions to modify the original value of the actual parameters directly.
    • To pass by reference in C, pointers are used.
    • By passing the memory address (reference) of a variable to a function,
    • Any changes made to the formal parameters within the function will aect the original values of the actual parameters. EXAMPLES: OUTPUT: Value after function call: 20 OUTPUT: 7 OUTPUT: 5

LESSON 5 FUNCTIONS WITH PARAMETERS

AND RETURN VALUES

SYNTAX OF FUNCTIONS W/ RETURN VALUESFUNCTION WITH MULTIPLE ARGUMENTS & RETURN VALUESLOCAL VS. GLOBAL VARIABLES

  • Variables in programming are used to store and manipulate data.
  • In C programming, variables can have dierent scopes, which determine their accessibility and lifespan within a program.
  • Two common types of variable scopes are local and global. Understanding the scope of variables is crucial for writing ecient and maintainable code. ➢ LOCAL VARIABLES
  • Local variables are declared within a specific block of code, typically within a function.
  • They are only accessible within that block and are destroyed once the block of code is exited. Local variables are ideal for storing temporary data that is required within a specific function. ➢ DECLARING LOCAL VARIABLES
  • To declare a local variable, you specify its data type and name within the function block.LIFESPAN OF LOCAL VARIABLES
    • Lifespan of a local variable is tied to the execution of the block of code in which it is declared.
    • When the clock of code is entered, the local variable is created, and when the block is exited, the variable is destroyed.
    • This means that local variables are created and destroyed each time the block of code is executed.
    • It is important to note that the value of a local variable is not preserved between functions.GLOBAL VARIABLES - Global variables are declared outside of any function and have a global scope.
    • They can be accessed by any function within the program.
    • Unlike local variables, global variables persist throughout the entire execution of the program.DECLARING GLOBAL VARIABLES
    • To declare a global variable, you place its declaration outside of any function, typically at the beginning of the program.ACCESSIBILITY OF GLOBAL VARIABLES - Global variables can be accessed from any function within the program.
    • WHile this can make sharing data between functions more convenient, it can also lead to potential issues, such as unintended modifications of the variable’s value.
    • Hence, it considered good practice to limit the use of global variables and favor local variables where appropriate. ➢ LOCAL VARIABLES
  • oer encapsulation and temporary storage within specific functions. ➢ GLOBAL VARIABLES
  • provide access from any part of the program.