0% found this document useful (0 votes)
19 views2 pages

DSA HU Assig..

Uploaded by

dagimnega66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

DSA HU Assig..

Uploaded by

dagimnega66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Data structures and algorithms

I, Individual assignment
Q. Find a recursive algorithm equivalent and it’s output for the following iterative algorithm with
necessary comments for every statement.

#include <stdio.h>

int binarySearch(int nums[], int n, int target)


{
int low = 0, high = n - 1;

while (low <= high)


{

int mid = (low + high)/2;


if (target == nums[mid]) {
return mid;
}

else if (target < nums[mid]) {


high = mid - 1;
}

else {
low = mid + 1;
}
}

return -1;
}

int main(void)
{
int nums[] = { 2, 5, 6, 8, 9, 10 };
int target = 5;

int n = sizeof(nums)/sizeof(nums[0]);
int index = binarySearch(nums, n, target);

if (index != -1) {
printf("Element found at index %d", index);
}
else {
printf("Element not found in the array");
}

return 0;
}
II, Group assignment
Q. Write a program to solve the Tower of Hanoi problem with 6,7,8 and 9 discs. The
objective is to move all the discs from the first tower to the third tower while following
three rules:

 Only one disc can be moved at a time.


 A larger disc cannot be placed on top of a smaller disc.
 You must use at least three towers (including the source tower, destination
tower, and an auxiliary tower) to solve the problem.

Your program should output a sequence of moves that will solve the Tower of Hanoi
problem for each given number of discs (6,7, 8 and 9) by moving the discs from the
source tower to the destination tower. Each move should be represented as a pair of
tower numbers, where the first number is the source tower and the second number is
the destination tower.

For example, if you are solving the Tower of Hanoi problem for 5 discs, the output
should be a sequence of moves like this: (1, 3), (1, 2), (3, 2), (1, 3), (2, 1), (2, 3), (1,
3), ... (where (1, 3) represents moving a disc from tower 1 to tower 3).

Remember to implement the solution algorithmically, taking into account the three
rules mentioned above.

Submit your program code along with the output sequence of moves for each
number of discs.

 The deadline for submitting the assignment is May 1, 2024


 Each group should consist of a maximum of five students.
 Directly copying code or solutions from external sources or other students
is strictly prohibited.

You might also like