0% found this document useful (0 votes)
179 views4 pages

Assignment 10 Solution

This document provides solutions to 10 questions about data structures and algorithms concepts. Key points addressed include: 1) Linear search is performed by checking every element in a list from beginning to end. 2) The bisection method can be used to find the root of a function. 3) To improve the best-case efficiency of bubble sort on an already sorted input, a boolean variable tracks whether any swapping occurred in an iteration. 4) If f(a) × f(b) > 0 for a continuous function f(x) on the range [a,b], there is no root where f(x) = 0. 5) Using the bisection method on a

Uploaded by

vinith
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)
179 views4 pages

Assignment 10 Solution

This document provides solutions to 10 questions about data structures and algorithms concepts. Key points addressed include: 1) Linear search is performed by checking every element in a list from beginning to end. 2) The bisection method can be used to find the root of a function. 3) To improve the best-case efficiency of bubble sort on an already sorted input, a boolean variable tracks whether any swapping occurred in an iteration. 4) If f(a) × f(b) > 0 for a continuous function f(x) on the range [a,b], there is no root where f(x) = 0. 5) Using the bisection method on a

Uploaded by

vinith
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/ 4

ASSIGNMENT 10 SOLUTION

1. In ……………, search start at the beginning of the list and check every element in the
list.
a) Linear search
b) Binary search
c) Hash search
d) Binary tree search
Solution: (a) linear search

2. Bisection method is used to find


a) Derivative of a function at a given point
b) Numerical integration of a function within a range
c) Root of the function
d) None of the above

Solution: (c) Root of the function

3. How can you improve the best-case efficiency in bubble sort? (The input is already
sorted)
a) boolean swapped = false;
for(int j=arr.length-1; j>=0 && swapped; j--)
{
swapped = true;
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = false;
}
}
}
b) boolean swapped = true;
for(int j=arr.length-1; j>=0 && swapped; j--)
{
swapped = false;
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}
}
ASSIGNMENT 10 SOLUTION

c) boolean swapped = true;


for(int j=arr.length-1; j>=0 && swapped; j--)
{
swapped = false;
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = true;
}
}
}

d) boolean swapped = true;


for(int j=arr.length-1; j>=0 && swapped; j--)
{
for(int k=0; k<j; k++)
{
if(arr[k] > arr[k+1])
{
int temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = true;
}
}
}

Solution: (c)
A boolean variable ‘swapped’ determines whether any swapping has happened in a particular
iteration, if no swapping has occurred, then the given array is sorted and no more iterations
are required.

4. If for a real continuous function f(x), f(a) × f(b) > 0, then in the range of [a,b] for f(x) =
0, there is (are)
a) Exactly one root
b) no root exists
c) at least one root
d) roots are undermined

Solution: (b). If, f(a) × f(b) < 0 then they have opposite signs; only then root(s) exists.
Since f(x) is continuous between a and b, the function needs to cross the x-axis to have f(x)
solvable. Here, as f(a) × f(b) > 0, so the function does not cross x-axis. Hence, no root
exists.
ASSIGNMENT 10 SOLUTION

5. Assuming an initial range [1,5], the second (at the end of 2 iterations) iterative value of
the root of te−t − 0.3 = 0 using the bisection method is (Note: you need to find the root,
not the function value)
Solution: 2 (short answer type)
t u = 5 and f(t u ) = −0.2663
t l = 1 and f(t l ) = 0.0679
f(t u ) × f(t l ) = 0.0181 < 0
Therefore, at least one root exists between [1,5]
t +t 5+1
Iteration1: t m = u l = =3 Thus, f(t m ) = −0.1506 < 0
2 2
Therefore, at least one root exists between [1,3] and we make t u = 3
tu +tl 3+1
Iteration2: t m = = =2 Hence, the root after the second iteration is 2.
2 2

6. What is the output?


#include <stdio.h>
int main()
{
char *s = "programming";
char *p = s;
printf("%c,%c", *(p + 3), s[3]);
return 0
}
a) o,o
b) p,g
c) g,g
d) g,r
Solution: (c) g,g
p points to the base address of ‘programming’ i.e. p. so *(p+3)= the fourth character i.e. g.
Similarly s[3] is also g. This is a simple example of pointer arithmetic on strings.

7. What will be output when you will execute following c code?


#include<stdio.h>
int main()
{
short num[3][2]={2,5,11,17,23,28};
printf("%d,%d",*(num+2)[0],**(num+1));
return 0;
}
a) 23,11
b) 23,23
c) 11,17
d) 17,17
Solution: (a) 23,11

*(num+2)[0]=*(*((num+2)+0))=*(*(num+2))=*(num[2])=num[2][0]=23
And **(num+1)=*(num[1]+0)=num[1][0]=11
This is example of pointer arithmetic on array.

8. Find the output of the C code given below


ASSIGNMENT 10 SOLUTION

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int *p;
p = ary + 3;
*p = 5;
printf("%d\n", ary[3]);
return 0;
}
a) 2
b) 4
c) 7
d) 5
Solution: (d) The pointer p contains the address of the last element of the array. Assigning the
value at that address to 5 changes the content of the array ary. The last element is replaced
with 5. Thus, ary[3] is 5.

9. Find the output of the following program


#include <stdio.h>
int main()
{
int *ptr, a = 7;
ptr = &a;
*ptr =*ptr - 2;
printf("%d,%d ", *ptr, a);
return 0;
}
Solution: 5,5 (short answer type)
The pointer variable ptr contains the address of the variable a. Incrementing the value at
address also modifies the content of a. Thus, both will be containing 5.

10. What is the solution of the equation given below using Bisection Method upto four
decimal places? (Consider the root lying on positive quadrant only and compute the root
till five iterations only)
f(x) = xe2x − 3x 2 − 5
Solution: 1.0312 (short answer type)
The root lies between 1 and 2. Using bisection method, we can find the root as 1.0312 (upto
three decimal accuracy)

You might also like