Algorithms Collection
Algorithms Collection
3. Matrix Operations
Algorithm:
Start
1. Declare 3x3 matrices A, B, and Result
2. Display menu:
a. Addition
b. Transpose
c. Multiplication
3. Input choice
4. If choice is Addition:
5. Input matrices A and B
6. For i = 0 to 2
7. For j = 0 to 2
8. Result[i][j] = A[i][j] + B[i][j]
9. Display Result
1
10. If choice is Transpose:
11. Input matrix A
12. For i = 0 to 2
13. For j = 0 to 2
14. Result[j][i] = A[i][j]
15. Display Result
4. String Operations
Algorithm:
Start
1. Input string str
2. Display menu:
a. Length
b. Reversal
c. Equality check
d. Palindrome check
3. Input choice
4. If choice is Length:
Without built-in:
5. Initialize count = 0
6. While str[count] != '\0'
7. Increment count
8. Print count
9. If choice is Reversal:
Without built-in:
10. Calculate length (len)
11. For i = 0 to len/2
12. temp = str[i]
13. str[i] = str[len-1-i]
14. str[len-1-i] = temp
15. Print reversed string
2
16. If choice is Equality:
Without built-in:
17. Input second string str2
18. Initialize flag = 1
19. For i = 0 until either string ends
20. If str[i] != str2[i]
21. flag = 0
22. break
23. Print result based on flag
fibonacci(n):
4. If n <= 1
5. Return n
6. Else
7. Return fibonacci(n-1) + fibonacci(n-2)
Stop
searchElement(array, n, key):
5. For i = 0 to n-1
6. If array[i] equals key
7. Return i
3
8. Return -1
7. Employee Management
Algorithm:
Start
1. Create Employee structure with fields:
- Name
- Designation
- Gender
- DOJ
- Salary
5. Function countEmployees():
6. Return total number of employees
7. Function highSalaryEmployees():
8. For i = 0 to n-1
9. If employee[i].salary > 20000
10. Print employee details
8. Number Swapping
Algorithm for Pointer Swap:
Start
1. Input numbers a and b
2. Create pointers p1 = &a, p2 = &b
3. temp = *p1
4. *p1 = *p2
5. *p2 = temp
6. Print swapped numbers
Stop
4
Algorithm for Call by Reference:
Start
1. Input numbers a and b
2. Call swap(&a, &b)
swap(x, y):
3. temp = *x
4. *x = *y
5. *y = temp