Data Structure and Algorithm
Maningding, Rose Faye F.
BSIT 1st Year
Mr. Darrell Flores
ASSIGNMENT #1
1. What are some things that algorithms can do in data structures?
Searching:
Algorithms like binary search and linear search help locate specific elements
within data structures such as arrays or linked lists.
Sorting:
Algorithms such as quick sort, merge sort, and bubble sort organize data in a
specific order (ascending or descending) within structures like arrays or lists.
Insertion:
Algorithms facilitate adding new elements to data structures, such as inserting
a node in a linked list or adding an element to a binary search tree.
Deletion:
Algorithms enable the removal of elements from data structures, such as
deleting a node from a linked list or removing an element from a hash table.
Traversal:
Algorithms like in-order, pre-order, and post-order traversal are used to visit all
the nodes in tree structures systematically.
Merging:
Algorithms can combine two or more data structures, such as merging two
sorted arrays into a single sorted array.
Graph Algorithms:
Algorithms like Dijkstra's and Kruskal's help in finding the shortest path or
minimum spanning tree in graph data structures.
Dynamic Programming:
Algorithms utilize data structures to store intermediate results, optimizing
problems like the Knapsack problem or Fibonacci sequence calculations.
Hashing:
Algorithms implement hash functions to efficiently store and retrieve data in
hash tables, allowing for quick access.
Balancing:
Algorithms maintain the balance of data structures like AVL trees or
Red-Black trees to ensure efficient operations.
2. Why is it important for an algorithm to be independent of programming code?
It's important for an algorithm to be independent of programming code
because it allows for greater flexibility and reusability. Imagine an algorithm
like a recipe - it outlines the steps to achieve a desired outcome. The recipe
itself doesn't care if you use a gas stove or an electric oven, just that you
follow the instructions.
Similarly, an algorithm can be implemented in different programming
languages, on different platforms, and even adapted to solve new problems
without needing to be rewritten from scratch. This makes algorithms incredibly
powerful and valuable tools!
3. How does knowing the problem domain help in writing an algorithm?
Defining the Scope: Knowing the problem domain helps you define the
boundaries of your algorithm. What are the specific inputs and outputs? What
are the constraints and assumptions?
Choosing the Right Approach: Different problem domains call for different
algorithmic approaches. For example, a sorting algorithm for a small list of
numbers might be different from one used to sort a massive database.
Identifying Relevant Data: Understanding the problem domain helps you
identify the data you need to solve the problem. This data might come from
different sources and require specific formatting or processing.
Evaluating Solutions: Knowing the problem domain allows you to evaluate the
effectiveness of your algorithm.
Write a C program to check if a number is even or odd.
Algorithm:
Step 1 − START
Step 2 − declare an integer variable num
Step 3 − Prompt the user to enter a number
Step 4 − Read the entered number and store it in num
Step 5 − Calculate the remainder when num is divided by 2
Step 6 − If the remainder is 0: - Print "num is even."
Step 7 − Else: - Print "num is odd."
Step 8 − STOP
Write a C program to find the largest of three numbers using Conditional statement(IF and
else).
Algorithm:
Step 1 − START
Step 2 − Declare four integer variables: num1, num2, num3, and largest
Step 3 − Prompt the user to enter three numbers
Step 4 − Read the three numbers entered by the user and store them in num1, num2, and
num3 respectively
Step 5 − If num1 is greater than or equal to both num2 and num3: - Assign the value of
num1 to largest
Step 6 − Else if num2 is greater than or equal to both num1 and num3: - Assign the value of
num2 to largest
Step 7 − Else: - Assign the value of num3 to largest
Step 8 − Print the value of largest as "The largest number is: [largest]"
Step 9 − STOP
Write a C program to find the area of a rectangle using Scanf().
Algorithm:
Step 1 − START
Step 2 − Declare three floating-point variables: length, width, and area
Step 3 − Prompt the user to enter the length of the rectangle
Step 4 − Read the length entered by the user and store it in the length variable
Step 5 − Prompt the user to enter the width of the rectangle
Step 6 − Read the width entered by the user and store it in the width variable
Step 7 − Calculate the area of the rectangle by multiplying length and width and store the
result in the area variable
Step 8 − Print the calculated area of the rectangle, formatted to two decimal places
Step 9 − STOP