Sei sulla pagina 1di 10

World University of Bangladesh

Lab-Assignment
Subject Name : Computer Algorithms Lab
Subject Code : CSE 1002

Submitted To
Ratna Mudi
Lecturer, Department of CSE.
World University of Bangladesh

Submitted By
Ummay Sumaiya
Roll - 2204
Batch - 38(A)
Dept : CSE.
Assignment- Spring Semester- 2020
Computer Algorithms Lab
Batch: 38A
Course Teacher: Ratna Mudi
Submission Date: 03.05.2020

Answer all of the following questions


1. If you want to insert a data in a given position, how will you organize computer
program?
2. If you write C code for binary search, how will it work step by step? Explain in detail.
3. Name two problems which use recursion in solution. Describe the steps of both
program.
4. How does an update algorithm work? Discuss the program of an update algorithm.
5. Explain the working steps of a sorting code.
Ans to the Que No- 1
C program to insert data:
#include <stdio.h>
int main()
{
int num [20],i, size, position, value;
printf("Enter the size of array :");
scanf("%d", &size);
printf("Enter the data of array :");
for(i=0; i<size; i++)
scanf("%d",&num[i]);
printf("Enter the value to insert :");
scanf("%d",&value);
printf("Enter the position :");
scanf("%d",&position);
for(i=size-1; i>=position-1; i--)
num[i+1] = num[i];
num[position-1] = value;
for(i=0; i<=size; i++)
printf("%d\n",num[i]);
return 0;
}
Output:
Ans to the Que No-2
C program for Binary search:
#include <stdio.h>
int main(void)
{
int arr[50], n, num, i, flag = 0;
printf ("Enter the size of an array :");
scanf ("%d", &n);
printf ("Enter elements in an array :\n");
for (i = 0; i < n; i++) {
scanf ("%d", &arr[i]);
}
printf ("Enter the element to be searched for :\n");
scanf ("%d", &num);
for (i =0; i < n; i++) {
if (arr[i] == num) {
flag = 1;
break;
}
}
if (flag) {
printf ("Element is found at position %d :", i+1);
} else {
printf ("Not Found");
}
return 0;
}
Output:
Ans to the que no – 3
Recursion:
Let’s return to functions and study them more in-depth.

Our first topic will be recursion.

If you are not new to programming, then it is probably familiar and you could skip this chapter.

Recursion is a programming pattern that is useful in situations when a task can be naturally split into
several tasks of the same kind, but simpler. Or when a task can be simplified into an easy action plus
a simpler variant of the same task. Or, as we’ll see soon, to deal with certain data structures.

When a function solves a task, in the process it can call many other functions. A partial case of this is
when a function calls itself. That’s called recursion.

Two ways of thinking


For something simple to start with – let’s write a function pow(x, n) that raises x to a natural power
of n. In other words, multiplies x by itself n times.

pow(2, 2) = 4
pow(2, 3) = 8
pow(2, 4) = 16

There are two ways to implement it.

1. Iterative thinking: the for loop:

2. function pow(x, n) {
3. let result = 1;
4.
5. // multiply result by x n times in the loop
6. for (let i = 0; i < n; i++) {
7. result *= x;
8. }
9.
10. return result;
11. }
12.
alert( pow(2, 3) ); // 8

13. Recursive thinking: simplify the task and call self:

14. function pow(x, n) {


15. if (n == 1) {
16. return x;
17. } else {
18. return x * pow(x, n - 1);
19. }
20. }
21.
alert( pow(2, 3) ); // 8

Please note how the recursive variant is fundamentally different.

When pow(x, n) is called, the execution splits into two branches:

if n==1 = x
/
pow(x, n) =
\
else = x * pow(x, n - 1)

1. If n == 1, then everything is trivial. It is called the base of recursion, because it immediately


produces the obvious result: pow(x, 1) equals x.
2. Otherwise, we can represent pow(x, n) as x * pow(x, n - 1). In maths, one would
write xn = x * xn-1. This is called a recursive step: we transform the task into a simpler action
(multiplication by x) and a simpler call of the same task (pow with lower n). Next steps simplify it
further and further until n reaches 1.

We can also say that pow recursively calls itself till n == 1.

For example, to calculate pow(2, 4) the recursive variant does these steps:

1. pow(2, 4) = 2 * pow(2, 3)
2. pow(2, 3) = 2 * pow(2, 2)
3. pow(2, 2) = 2 * pow(2, 1)
4. pow(2, 1) = 2

So, the recursion reduces a function call to a simpler one, and then – to even more simpler, and so
on, until the result becomes obvious.
Ans To The Question No – 4

C code for Update element in an array :


#include<stdio.h>
int main() {
int n,a[10],pos,ele,i;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter Array element : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter The Position : ");
scanf("%d",&pos);
printf("Enter element");
scanf("%d",&ele);
a[pos-1]=ele;
printf("After Updating Array Element Are : ");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
Ans to the Que No- 5

Sorting is a process of arranging items in ascending or descending order. This process can be
implemented via many different algorithms.

Sort Algorithm Steps on how it works:

1. Find a “pivot” item in the array. This item is the basis for comparison for a single

round.

2. Start a pointer (the left pointer) at the first item in the array.

3. Start a pointer (the right pointer) at the last item in the array.

4. While the value at the left pointer in the array is less than the pivot value, move

the left pointer to the right (add 1). Continue until the value at the left pointer is

greater than or equal to the pivot value.


5. While the value at the right pointer in the array is greater than the pivot value,

move the right pointer to the left (subtract 1). Continue until the value at the right

pointer is less than or equal to the pivot value.

6. If the left pointer is less than or equal to the right pointer, then swap the values at

these locations in the array.

7. Move the left pointer to the right by one and the right pointer to the left by one.

8. If the left pointer and right pointer don’t meet, go to s

Below is an image of an array, which needs to be sorted. We will use the Quick Sort Algorithm,

to sort this array:

Potrebbero piacerti anche