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

Lab on C 2

Uploaded by

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

Lab on C 2

Uploaded by

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

ABSTRACT (LAB TASK 02)

Linear Search, Finding the Maximum element of an


Array & Quadratic Equation solution Implementation

Md. Ahadur Rahman Munshi


EMCS-1108026

DATA STRUCTURE &


ALGORITHM LAB
EMCS-526
Problem Statement 01:
Implement Linear Search

Analysis:
Linear search is a type of sequential searching algorithm. In this method, every element within the
input array is traversed and compared with the key element to be found. If a match is found in the
array the search is said to be successful; if there is no match found the search is said to be unsuccessful
and gives the worst-case time complexity.
EXAMPLE
Let us look at the step-by-step searching of the key element (say 47) in an array using the linear
search method.

Step 1: The linear search starts from the 0th index. Compare the key element with the value in the
0th index, 34. However, 47 ≠ 34. So it moves to the next element.

Step 2: Now, the key is compared with value in the 1st index of the array. Still, 47 ≠ 10, making the
algorithm move for another iteration.

Step 3: The next element 66 is compared with 47. They are both not a match so the algorithm
compares the further elements.

Step 4: Now the element in 3rd index, 27, is compared with the key value, 47. They are not equal
so the algorithm is pushed forward to check the next element.

Step 5: Comparing the element in the 4th index of the array, 47, to the key 47. It is figured that both
the elements match. Now, the position in which 47 is present, i.e., 4 is returned.

The output achieved is “Element found at 4th index”.

Algorithm:
procedure linear_search (list, value)
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure

Source Code:
Input-Output

01

02
Problem Statement 02:
Find the maximum value of an Array

Analysis:
The simplest approach is to solve this problem is to traverse the whole list and find the maximum
among them using Linear Search technique.
Here’s a step-by-step process for finding the largest value in the array [10, 0, 100, 501, 1, 2000, 0,
0, 0, 10] without graphics:
Initialize: Set the first element, 10, as the initial maximum.
Step 2: Compare the second element, 0, with the current maximum (10). The maximum remains
10.
Step 3: Compare the third element, 100, with 10. Since 100 is greater, update the maximum to 100.
Step 4: Compare the fourth element, 501, with 100. Since 501 is greater, update the maximum to
501.
Step 5: Compare the fifth element, 1, with 501. The maximum remains 501.
Step 6: Compare the sixth element, 2000, with 501. Since 2000 is greater, update the maximum to
2000.
Step 7: Compare the seventh element, 0, with 2000. The maximum remains 2000.
Step 8: Compare the eighth element, 0, with 2000. The maximum remains 2000.
Step 9: Compare the ninth element, 0, with 2000. The maximum remains 2000.
Step 10: Compare the tenth element, 10, with 2000. The maximum remains 2000.
Result: The maximum value in the array is 2000.

Algorithm:
Create a local variable max and initiate it to arr[0] to store the maximum among the list
Iterate over the array
Compare arr[i] with max.
If arr[i] > max, update max = arr[i].
Increment i once.
After the iteration is over, return max as the required answer.

Source Code:
(P.T.O)

Input-Output

01

02
Problem Statement 03:
Implement Quadratic Equation to find the roots

Analysis:
A quadratic equation is a polynomial equation of the form: 𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0
Where:
• 𝑎, 𝑏, and 𝑐 are constants, with 𝑎 ≠ 0
• 𝑥 is the variable or unknown that we want to solve for
Solving a Quadratic Equation
−𝑏±√𝑏2 −4𝑎𝑐
To find the values of 𝑥 that satisfy the equation, we use the quadratic formula: 𝑥 = 2𝑎
where: 𝑏2 − 4𝑎𝑐 is called the discriminant. It determines the nature of the roots of the equation.
Types of Roots Based on the Discriminant
If 𝑏2 − 4𝑎𝑐 > 0, there are two distinct real roots. If 𝑏2 − 4𝑎𝑐 = 0, there is one real root (repeated
root). If 𝑏2 − 4𝑎𝑐 < 0, there are two complex roots (not real).
EXAMPLE: Let’s Consider the equation: 𝟐𝒙𝟐 − 𝟒𝒙 − 𝟔 = 𝟎
Here, the coefficients are: a=2, b=-4, c=-6
Step 1: Find the Discriminant (Calculate 𝑏2 − 4𝑎𝑐)
𝑏2 − 4𝑎𝑐 = (−4)2 − 4 ∗ 2 ∗ (−6) = 16 + 48 = 64
Since the discriminant is positive (64>0), there will be two distinct real roots.
−(−4)±√64 4±8
Step 2: Use the Quadratic Formula: 𝑥 = =
2∗2 4
Step 3: Simplify for Each Root
4+8 12
First Root: 𝑥 = 4 = 4 = 3
4−8 −4
Second Root: 𝑥 = 4 = 4 = −1
Solution: The two roots of the equation 2𝑥2 − 4𝑥 − 6 = 0 are 3 & -1

Algorithm:
START
Input a, b, c
If a = 0
Print "Not a quadratic equation"
STOP
END IF
discriminant = b^2 - 4 * a * c
IF discriminant > 0 THEN
root1 = (-b + √discriminant) / (2 * a)
root2 = (-b - √discriminant) / (2 * a)
Print "Two distinct real roots:", root1, root2
ELSE IF discriminant = 0 THEN
root = -b / (2 * a)
Print "One real root:", root
ELSE
realPart = -b / (2 * a)
imaginaryPart = √(-discriminant) / (2 * a)
Print "Two complex roots:", realPart + " + "
+ imaginaryPart + "i", realPart + " - " +
imaginaryPart + "i"
END IF
END

Source Code:
(P.T.O)
Input-Output

01

02

03

04

You might also like