# Problem Solving Through Programming In C Week 11

Session: JULY-DEC 2023

Course Name: Problem Solving Through Programming In C

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

Q1. Interpolation provides a mean for estimating functions
a) At the beginning points
b) At the ending points
c) At the intermediate points
d) None of the mentioned

Answer: c) At the intermediate points

Q2. To solve a differential equation using Runge-Kutta method, necessary inputs from user to the algorithm is/are
a) the differential equation dy/dx in the form x and y
b) the step size based on which the iterations are executed.
c) the initial value of y.
d) all the above

Q3. A Lagrange polynomial passes through three data points as given below
The polynomial is determined as f(x) = Lo(x). (15.35) + L₁ (x). (9.63) + L₂(x). (3.74). The value of f(x) at x = 7 is

a) 12.78
b) 13.08
c) 14.12
d) 11.36

These are Problem Solving Through Programming In C Assignment 11 Answers

Q4. The value of f3.2 0 xex dx by using one segment trapezoidal rule is
a) 172.7
b) 125.6
c) 136.2
d) 142.8

Q5. Accuracy of the trapezoidal rule increases when
a) integration is carried out for sufficiently large range
b) instead of trapezoid, we take rectangular approximation function
c) number of segments are increased
d) integration is performedfor only integer range

Answer: c) number of segments are increased

Q6. Solve the ordinary differential equation below using Runge-Kutta4th order method. Step size h=0.2. dy 5 + xy³ = cos(x),y(0) = 3 dx The value of y(0.2) is (upto two decimal points)
a) 2.86
b) 2.93
c) 3.13
d) 3.08

These are Problem Solving Through Programming In C Assignment 11 Answers

Q7. Match the following
A. Newton Method 1. Integration
B. Lagrange Polynomial 2. Root finding
C. Trapezoidal Method 3. Differential Equation
D. RungeKutta Method 4. Interpolation

a) A-2, B-4, C-1, D-3
b) A-3, B-1, C-2, D-4
c) A-1, B-4, C-3, D-2
d) A-2, B-3, C-4, D-1

Answer: a) A-2, B-4, C-1, D-3

Q8. The value of fex (lnx) dx calculated using the Trapezoidal rule with five subintervals is (* range is given in output rather than single value to avoid approximation error)
a) 12.56 to 12.92
b) 13.12 to 13.66
c) 14.24 to 14.58
d) 15.13 to 15.45

Q9. Consider the same recursive C function that takes two arguments
unsignedintfunc(unsigned int n, unsigned int r) { if (n > 0) return (n%r + func (n/r, r)); else return 0; }
What is the return value of the function foo when it is called as func(513, 2)?

a) 9
b) 8
c) 5
d) 2

These are Problem Solving Through Programming In C Assignment 11 Answers

Q10. What will be the output?
a) 4
b) 8
c) 16
d) Error

### Assignment

Question 1
The velocity of a car at different time instant is given as
Time (t) 10 15 18 22 30
Velocity v(t) 22 26 35 48 68
A linear Lagrange interpolant is found using these data points. Write a C program to find the velocity of the car at different time instants. (Taken from test cases)

Solution:

``````int isa,j;
float b, c, k =0;
for(isa=0; isa < 5; isa++)
{
b=1;
c=1;
for(j=0; j < 5; j++)
{
if(j!=isa)
{
b=b*(a-t[j]);
c=c*(t[isa]-t[j]);
}
}
k=k+((b/c)*v[isa]);
}``````

Question 2
Write a C program to find f b a x² dx using Trapezoidal rule with 10 segments between a and b. The values of a and b will be taken from test cases

Solution:

``````int id;
float h,x, sum=0;
if(b > a)
h=(b-a)/n;
else
h=-(b-a)/n;
for(id=1;id < n;id++)
{
x=a+id*h;
sum=sum+func(x);
}
integral=(h/2)*(func(a)+func(b)+2*sum);
printf("The integral is: %0.6f\n",integral);
return 0;
}
float func(float x)
{
return x*x;
}``````

These are Problem Solving Through Programming In C Assignment 11 Answers

Question 3
Write a C program to solve the following differential equation using Runge-Kutta method. Step size h=0.3
10dy/dx + 3y³ = x(x + 1), y(0.3) = 5
Find y(x) for different values of x as given in the test cases.

Solution:

``````while(x0 < xn)
{
m1=func(x0,y0);
m2=func((x0+h/2.0),(y0+m1*h/2));
m3=func((x0+h/2.0),(y0+m2*h/2));
m4=func((x0+h),(y0+m3*h));
m=((m1+2*m2+2*m3+m4)/6);
y0=y0+m*h;
x0=x0+h;
}
printf("y=%.6f",y0);
return (0+0);
}
float func(float x,float y)
{
float m;
m=(x*(x+1)-3*y*y*y)/10;
return m;
}``````

Question 4
Write a C program to check whether the given input number is Prime number or not using recursion. So, the input is an integer and output should print whether the integer is prime or not.

Solution:

``````int checkPrime(int numBER, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (numBER % i == 0)
{
return 0;
}
else
{
return checkPrime(numBER, i - 1);
}
}
}``````

These are Problem Solving Through Programming In C Assignment 11 Answers

Session: JAN-APR 2023

Course Name: Problem Solving Through Programming In C

These are Problem Solving Through Programming In C Assignment 11 Answers

#### Q1. Interpolation is a process fora) extracting feasible data set from a given set of datab) finding a value between two points on a line or curve.c) removing unnecessary points from a curved) all of the mentioned

Answer: b) finding a value between two points on a line or curve.

Q2. Given two data points (a, f (a)) and (b, f(b). the linear Lagrange polynomial f (x) that passes
through these two points are given as

a) f(x) = (x-a/a-b)f(a) + (x-a/a-b)f(b)
b) f(x) = (x/a-b)f(a) + (x/b-a)f(b)
c) f(x) = f(a) + (f(b)-f(a)/b-a)f(b)
d) f(x) = (x-b/a-b)f(a) + (x-a/b-a)f(b)

Answer: d) f(x) = (x-b/a-b)f(a) + (x-a/b-a)f(b)

These are Problem Solving Through Programming In C Assignment 11 Answers

Q3. A Lagrange polynomial passes through three data points as given below

The polynomial is determined as f(x) = L0 (x). (15.35) + L₁ (x). (9.63) + L₂(x). (3.74) The value of f(x) at x = 7 is
a) 12.78
b) 13.08
c) 14.12
d) 11.36

Q4. The value of f 1.5 0 xe²x dx by using one segment trapezoidal rule is (upto four decimal places) The value of

These are Problem Solving Through Programming In C Assignment 11 Answers

Q5. Accuracy of the trapezoidal rule increases when
a) integration is carried out for sufficiently large range
b) instead of trapezoid, we take rectangular approximation function
c) number of segments are increased
d) integration is performed for only integer range

Answer: c) number of segments are increased

Q6. Solve the ordinary differential equation below using Runge-Kutta 4th order method. Step size
h=0.2.
5dy/dx +xy3 = cos(x),y(0) = 3
The value of y(0.2) is (upto two decimal points)

a) 2.86
b) 2.93
c)3.13
d) 3.08

These are Problem Solving Through Programming In C Assignment 11 Answers

Q7. Which of the following cannot be a structure member?
a) Another structure
b) function
c) array
d) none of the above

Q8. Match the following
A. Newton Method 1. Integration
B. Lagrange Polynomial 2. Root finding
C. Trapezoidal Method 3. Differential Equation
D. Runge Kutta Method 4. Interpolation

a) A-2, B-4, C-1, D-3
b) A-3, B-1, C-2, D-4
c) A-1, B-4, C-3, D-2
d) A-2, B-3, C-4, D-1

Answer: a) A-2, B-4, C-1, D-3

These are Problem Solving Through Programming In C Assignment 11 Answers

Q9. The value of (3 1 ex(In x) dx calculated using the Trapezoidal rule with five subintervals is (* range
is given in output rather than single value to avoid approximation error)

a) 12.56 to 12.92
b) 13.12 to 13.66
c) 14.24 to 14.58
d) 15.13 to 15.45

Q10. Consider the same recursive C function that takes two arguments unsigned int func(unsigned int n, unsigned int r)
{if (n >0) retum (n%r + func (n/r, r));
else return 0;
}
What is the return value of the function foo when it is called as func(513, 2)?

a) 9
b) 8
c) 5
d) 2

These are Problem Solving Through Programming In C Assignment 11 Answers

### Problem Solving Through Programming In C Programming Assignment

Question 1

The velocity of a car at different time instant is given as

A linear Lagrange interpolant is found using these data points. Write a C program to find the velocity of the car at different time instants. (Taken from test cases)

Solution:

``````int i,j;
float b, c, k =0;
for(i=0; i<5; i++)
{
b=1;
c=1;
for(j=0; j<5; j++)
{
if(j!=i)
{
b=b*(a-t[j]);
c=c*(t[i]-t[j]);
}
}
k=k+((b/c)*v[i]);
}``````

These are Problem Solving Through Programming In C Assignment 11 Answers

Question 2

Write a C program to find (ba x2dx using Trapezoidal rule with 10 segments between a and b. The values of a and b will be taken from test cases.

Solution:

``````int i;
float h,x, sum=0;
if(b>a)
h=(b-a)/n;
else
h=-(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+func(x);
}
integral=(h/2)*(func(a)+func(b)+2*sum);
printf("The integral is: %0.6f\n",integral);
return 0;
}
float func(float x)
{
return x*x;
}``````

These are Problem Solving Through Programming In C Assignment 11 Answers

Question 3

Write a C program to solve the following differential equation using Runge-Kutta method. Step size h=03
10dy/dx + 3y^3 = x(x+1),y(0.3)=5
Find y(x) for different values of x as given in the test cases.

Solution:

``````float m1,m2,m3,m4,m,h=0.3;
float x0 = 0.3, y0 = 5;

while (x0 < xn)
{
m1 = func(x0, y0);
m2 = func((x0 + h/2.0), (y0 + m1*h/2));
m3 = func(x0 + h/2.0, (y0 + m2*h/2));
m4 = func((x0 + h), (y0 + m3*h));
m=((m1+2*m2+2*m3+m4)/6);
y0=y0+m*h;
x0=x0+h;
}
printf("y=%.6f",y0);
return 0;
}
float func(float x,float y)
{
float m;
m=(x*(x+1)-3*y*y*y)/10;
return m;
}``````

These are Problem Solving Through Programming In C Assignment 11 Answers

Question 4

Write a C program to check whether the given input number is Prime number or not using recursion. The input is an integer and output should print whether the integer is prime or not. (Note that you have to use recursion).

Solution:

``````int checkPrime(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return checkPrime(num, i - 1);
}
}
}``````

These are Problem Solving Through Programming In C Assignment 11 Answers