# Problem Solving Through Programming In C Week 10

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

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={2,5,11,17,23,28}; printf(“%d,%d”, (num+2),*(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²)

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

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