BCSL-045 ASSIGNMENT SOLUTION (2020-21)

Q1. Write and test a program to sort the following array of 10 integer numbers using Selection Sort . Calculate the numbers of times the outer loop, inner loop, if statement and and swap operation will execute in this example. Show all the intermediate steps.

 15 45 60 25 35 8 40 5 10 27

#include<stdio.h>

int main()
{
int array[]={ 15, 45, 60, 25, 35, 8, 40, 5, 10, 27 };
int i, j, temp, index, length;
int outer_loop = 0, inner_loop = 0, if_statement = 0, swap_operation = 0;

length = sizeof(array) / sizeof(int);
for(i=0; i<(length - 1); i++)
{
index = i;
for(j=(i+1); j<length; j++)
{
if(array[index] > array[j])
{
index = j;
if_statement++;
}
inner_loop++;
}
if(index != i)
{
temp = array[i];
array[i] = array[index];
array[index] = temp;
if_statement++;
swap_operation++;
}
outer_loop++;
}

for(i=0; i<length; i++)
{
printf("\t%d", array[i]);
}
printf("\n\n outer-loop execute %d times", outer_loop);
printf("\n inner-loop execute %d times", inner_loop);
printf("\n if-statement execute %d times", if_statement);
printf("\n swap-operation execute %d times", swap_operation);
return 0;
}

Output :
 5 8 10 15 25 27 35 40 45 60

outer-loop execute 9 times
inner-loop execute 45 times
if-statement execute 22 times
swap-operation execute 8 times

Q2. Implement Binary Search algorithm to search for a number 70 in the following sorted array of 10 integer numbers. Calculate the total no of mid operations, if statement and else if comparison operations and the number of times the loop will execute.

 5 10 25 30 37 46 55 60 65 70

 #include int main() { int array[]={ 5, 10, 25, 30, 37, 46, 55, 60, 65, 70 }; int low = 0, mid, high, count = 0, number = 70; int mid_operation = 0, if_statement = 0, else_if_comparison = 0, loop = 0; high = (sizeof(array) / sizeof(int)) - 1; while(low <= high) { loop++; mid = (low + high) / 2; mid_operation++; if(array[mid] == number) { count = 1; if_statement++; break; } else if(array[mid] < number) { low = mid + 1; if_statement++; else_if_comparison++; } else if(array[mid] > number) { high = mid - 1; if_statement++; else_if_comparison++; } } if(count == 0) { printf("\n Number not Found"); if_statement++; } else { printf("\n Number Found"); } printf("\n\n mid-operation execute %d times", mid_operation); printf("\n if-statement Execute %d times", if_statement); printf("\n else-if-comparison execute %d times", else_if_comparison); printf("\n loop execute %d times", loop); return 0; } Output : Number Found mid-operation execute 4 times if-statement execute 4 times else-if-comparison execute 3 times loop execute 4 times

Q3. Write a program to traverse a graph using DFS. Apply this algorithm to the following graph and write the sequence of vertices to be travelled. Also calculate the number of times the for loop and if condition will execute in this example.

Answer : - Graph not present in the assignment's question paper. Therefore, solution is not possible.

If you want to learn DFS Algorithm and its Programming then click the link given below -
Depth First Search (DFS)

Q4. Write a program to find the maximum of the following list of integer numbers : 15, 20, 5, 4, 3, 17, 35.
Calculate (i) the number of times the loop will execute and (ii) the number of times the if statement will run in this example.

 #include int main() { int array[]={ 15, 20, 5, 4, 3, 17, 35 }; int i, max, length, loop = 0, if_statement = 0; max = array; length = (sizeof(array) / sizeof(int)); for(i=1; i

Q5. Implement Prim’s algorithm to find a minimum cost spanning tree of the following graph and print the result . Represent the graph through adjacency matrix,  Q6. Implement Horner's rule for evaluating the following polynomial expression at x = 5. Calculate the total number of times addition and multiplication operations will ocuur in this example

p(x) = 3x5 - 4x4 + 5x3 -6x + 9

Q7. Write a program to count the number of times an integer number 12 has occurred in the following array of 10 integer numbers.

15, 20, 35, 12, 11, 8 ,12, 7, 12, 16

Calculate the number of times (i) loop statement and (ii) increment operations will execute in this example.

 #include int main() { int array[]={ 15, 20, 35, 12, 11, 8, 12, 7, 12, 16 }; int i = 0, count = 0, length, number = 12; int loop_statement = 0, increment_operation = 0; length = (sizeof(array) / sizeof(int)); while(i < length) { loop_statement++; if(array[i] == number) { count++; increment_operation++; } i++; increment_operation++; } printf("\n 12 occurred %d times in the given array", count); printf("\n\n loop-statement execute %d times", loop_statement); printf("\n increment-operation execute %d times", increment_operation); return 0; } Output : 12 occurred 3 times in the given array loop-statement execute 10 times increment-operation execute 13 times

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.

MORE TOPIC

WHAT WE DO

 Follow Us 