# Problem Solving Through Programming In C Week 10

Session: JULY-DEC 2023

Course Name: Problem Solving Through Programming In C

#### These are Problem Solving Through Programming In C Assignment 10 Answers

Q1. 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) The root of a function
d) None of the above

Answer: c) The root of a function

Q2. In ……., the search starts at the beginning of the list and checks every element in the list.
a) Linear search
b) Binary search
c) Hash search
d) Binary tree search

Answer: a) Linear search

Q3. What is the worst-case time complexity of Linear Search?
a) O(1)
b) O(logn)
c) O(n)
d) O(n²)

These are Problem Solving Through Programming In C Assignment 10 Answers

Q4. What is the worst-case complexity of bubble sort?
a) O(N log N)
b) O(log N)
c) O(N)
d) O(N²)

Q5. What maximum number of comparisons can occur when a bubble sort is implemented? Assume there are n elements in the array.
a) (1/2)(n-1)
b) (1/2) n(n-1)
c) (1/4) n(n-1)
d) None of the above

Answer: b) (1/2) n(n-1)

Q6. What are the correct intermediate steps of the following data set when it is being sorted with the bubble sort? 7,4,1,8,2
a) 4,7,1,8,2⇒4,1,7,2,8⇒4,1,2,7,8⇒1,4,2,7,8⇒1,2,4,7,8
b) 4,7,1,8,2⇒4,1,7,8,2⇒4,1,7,2,8⇒1,4,7,2,8⇒1,4,2,7,8⇒1,2,4,7,8
c) 4,7,1,8,2⇒1,4,7,8,2⇒1,4,2,7,8⇒1,2,4,7,8
d) 4,7,1,8,2⇒4,7,1,2,8⇒1,4,7,2,8⇒1,4,2,7,8⇒1,2,4,7,8

These are Problem Solving Through Programming In C Assignment 10 Answers

Q7. What is the main disadvantage of the Bisection Method?
a) It is computationally expensive
b) It cannot find complex roots
c) It requires the function to be differentiable
d) It is not guaranteed to converge

Answer: b) It cannot find complex roots

Q8. What will be the output of the following snippet?
int arr[] = {10, 20, 30, 40, 50};
int *ptr1 = arr;
int *ptr2 = ptrl + 3;
printf(“%d”, *ptr2 – *ptr1);

Q9. What is the solution of the equation given below using the Bisection Method up to four decimal places? (Consider the root lying on positive quadrant only and compute the root till five iterations only)
f(x) = xe2x – 3x² – 5

These are Problem Solving Through Programming In C Assignment 10 Answers

Q10. What will be the output?
a) Option (a)
b) Option (b)
c) Option (c)
d) Option (d)

Answer: d) Option (d)

### Assignment

Question 1
Write a C program to find the root of the equation using bisection method for different values of allowable error of the root.
f(x) = 2x³ – 3x – 5

Solution:

``````do
{
if (fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if (((x1-x) < 0 && -(x1-x) <  allerr) || ((x1-x) > 0 && (x1-x) < allerr))
{
printf("Root = %1.4f", x1);
return 0;
}
x=x1*1;
}
while (itr <  maxmitr);
return 1;
}
float fun (float z)
{
return (z*z*z*2 - z*3*1 - 5);
}
void bisection (float *x3, float a, float b, int *itr)
{
*x3=(a+b)/2;
++(*itr);
}``````

Question 2
Write a C code to check if a 3 x 3 matrix is invertible. A matrix is not invertible if its determinant is 0.

Solution:

``````determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
* a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
if ( determinant == 0)
{printf("The given matrix is not invertible");}
else
{
printf("The given matrix is invertible");
}
return 0;
}``````

These are Problem Solving Through Programming In C Assignment 10 Answers

Question 3
Write a C program to sort a given 1D array using pointer in ascending order.

Solution:

``````int k,pk2;
for (i=0; i < (n-1); i++)
{
for (k=i+1; k < n; k++)
{
if (*(a+i) > *(a+k))
{
pk2=*(a+i);
*(a+i)=*(a+k);
*(a+k)=pk2;
}
}
}``````

Question 4
Write a C program to sort a 1D array using pointer by applying Bubble sort technique.

Solution:

``````void sort(int *a, int n)
{
int ipc,temp,j2;
for(ipc=1;ipc < n;ipc++)
{
for(j2=0;j2  < n-ipc;j2++)
{
if(*(a+j2) >= *(a+j2+1))
{
temp = *(a+j2);
*(a+j2)= *(a+j2+1);
*(a+j2+1)= temp;
}
}
}
}``````

These are Problem Solving Through Programming In C Assignment 10 Answers

More Weeks of Problem Solving Through Programming In C: Click here

Session: JAN-APR 2023

Course Name: Problem Solving Through Programming In C

These are Problem Solving Through Programming In C Assignment 10 Answers

#### Q1. The bisection method is used to finda) Derivative of a function at a given pointb) Numerical integration of a function within a rangec) Root of a functiond) None of the above

Answer: c) Root of a function

Q2. In ……………, the search starts at the beginning of the list and checks every element in the list.
a) Linear search
b) Binary search
c) Hash search
d) Binary tree search

Answer: a) Linear search

These are Problem Solving Through Programming In C Assignment 10 Answers

Q3. What is the advantage of a recursive approach over an iterative approach?
a) Consumes less memory
b) Less code and easy to implement
c) Consumes more memory
d) More code has to be written

Answer: b) Less code and easy to implement

Q4. What would be the equivalent pointer expression for referring to the array element a[i][j][k][l]?
a) (((*(a+i)+j)+k)+l)
b) *((((a+i)+j)+k)+l)
c) ((*(a+i)+j)+k+l)
d) *((a+i)+j+k+l)

These are Problem Solving Through Programming In C Assignment 10 Answers

Q5. What will be output when you will execute the following C code? #include 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

Q6. Assume size of an integer and a pointer is 4 bytes. What is the output? #include #define A 5 #define B 8 #define C 2 int main() { int (x)[A][B][C]; printf(“%ld”, sizeof(x)); return 0;}

These are Problem Solving Through Programming In C Assignment 10 Answers

Q7. Which of the following is not a requirement for binary search algorithm to work correctly?
a) The array must be sorted
b) The array must be of even length
c) The elements in the array must be distinct
d) The array must be stored in contiguous memory locations

Answer: b) The array must be of even length

Q8. What is the time complexity of binary search algorithm in the worst-case scenario?
a) O(1)
b) O(n)
c) O(log n)
d) O(n²)

Answer: c) O(log n)

These are Problem Solving Through Programming In C Assignment 10 Answers

Q9. What happens if an unsorted array is used in binary search algorithm?
a) The algorithm will still work correctly
b) The algorithm will return an error message
c) The algorithm will give a wrong output
d) The algorithm will run infinitely

Answer: c) The algorithm will give a wrong output

Q10. What will be the output? #include int main() { } int x = 5, y = 10; int *p=&x, *q=&y; *p = *q; *q = 20; printf(“%d %d”, x, y); return 0;}
a) 5 10
b) 10 20
c) 20 10
d) Compilation error

Answer: b) 10 20

These are Problem Solving Through Programming In C Assignment 10 Answers

### Problem Solving Through Programming In C Programming Assignment

Question 1

Write a C program to find the root of the equation using bisection method for different values of allowable error of the root.
f(x) = 2x³-3x – 5

Solution:

``````do
{
if (fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if (((x1-x)<0 && -(x1-x)< allerr) || ((x1-x)>0 && (x1-x)< allerr))
{
printf("Root = %1.4f\n", x1);
return 0;
}
x=x1;
}
while (itr < maxmitr);
return 1;
}
float fun (float x)
{
return (2*x*x*x - 3*x - 5);
}
void bisection (float *x, float a, float b, int *itr)
{
*x=(a+b)/2;
++(*itr);
}``````

Question 2

Write a C program to find the root of the equation using Newton Raphson method.
f(x) = x³ – 2x – 3
The maximum number of steps are taken as input.

Solution:

``````float h;
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
x0=x1;
}
printf("Root = %8.6f\n", x1);
return 0;
}
float f(float x)
{
return x*x*x - 2*x  - 3;
}
float df (float x)
{
return 3*x*x-2;
}``````

These are Problem Solving Through Programming In C Assignment 10 Answers

Question 3

Write a C program to sort a given 1D array using pointer in ascending order.

Solution:

``````int j,t;
for (i=0; i<(n-1); i++)
{
for (j=i+1; j<n; j++)
{
if (*(a+i)>*(a+j))
{
t=*(a+i);
*(a+i)=*(a+j);
*(a+j)=t;
}
}
}``````

Question 4

Write a C program to sort a 1D array using pointer by applying Bubble sort technique.

Solution:

``````void sort(int *a, int n) {
int i,j,temp;
for(i=0;i<n-1;i++) {
for(j=0;j<n-i-1;j++) {
if(*(a+j)>*(a+j+1)) {
temp=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=temp;
}
}
}
}
``````

These are Problem Solving Through Programming In C Assignment 10 Answers

More Weeks of Problem Solving Through Programming In C: Click Here

More Nptel courses: https://progiez.com/nptel