Vous êtes sur la page 1sur 3

Assignment 1, KCS-101, CSE First Year

Q(1) The Bessel function of the first kind of order zero is defined by

Write a C program that accepts real x from the keyboard. Then it calculates and prints out the value of
J0(x) using the first 20 terms only.

Q(2) Let n be a positive real number, and let the sequence of real numbers xi be given by

1 𝑛
𝑥0 = 1, 𝑥𝑖+1 = 2
(𝑥𝑖 + 𝑥 ) for i = 0, 1, 2, 3, · · ·
𝑖

It can be shown xi →√𝑛 as i → ∞. Write a C program that calculates the square roots of a number n. (n is
the input given by user) The program should print the number, the square root of the number and the
number of iterations needed to compute it. We shall assume that the iteration converges to the root
when |n − x2|<10-5

Q(3). Given a square matrix, you have to write a program in C to print it in a counter-clockwise spiral
form. The first line of the input contains an integer number n which represents the number of rows and
columns in the matrix.
Example:
Input: Output:
5 11 16 21 26 31 32 33 34 35 30 25 20 15 14 13 12 17 22 27 28 29 24 19 18 23
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35

Q(4). Given a square matrix, you have to write a program in C to print it in a clockwise spiral form. The
first line of the input contains an integer number n which represents the number of rows and columns
in the matrix.
Example:
Input: Output:
5 11 12 13 14 15 20 25 30 35 34 33 32 31 26 21 16 17 18 19 24 29 28 27 22 23
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35

Q(5). Write a C program to print following pattern of prime numbers if input is no of lines. For example
if n=4 or n=5 output is shown
Input: Output: Input: Output:
4 2 4 2
35 35
7 11 13 7 11 13
17 19 23 29 17 19 23 29
Q(6). 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. Note
that you have to use recursion.

Q(7). The equation f(x) Ξ (1 -x) cos x sin x = 0 has at least one root between a = 0 and b = 1 since
f(a)f(b) < 0.
The bisection method of finding the root proceeds as follows:
a. It finds the midpoint r = (a + b)=2.
b. If f(r) = 0, then r is the root. If |b-a| is very small less than €, then also we can take r as the root. In
either of the cases, our job is done.

c. If f(r) ≠ 0 and f(a)f(r) < 0, then the root lies between a and r. We assign r to b and go to step a.
d. If f(r) ≠ 0 and f(b)f(r) < 0, then the root lies between r and b. We assign r to a and go to step a.
e. If the number of iterations is high, we may stop the process with appropriate message.

Write the following functions with the specification mentioned.


1. Function func takes a real number x as argument and returns the value of f(x).
2. Function cbracket takes two real numbers a and b as arguments and returns 1 if at least one real root
of f(x) lies between a and b, and 0 otherwise.

3. Function rootb that takes three real numbers a, b, eps and an integer Nmax as arguments. This function
returns the root of f(x) using bisection method. If the number of iteration is more than Nmax then the
function terminates with appropriate message.

Write a C program using the above functions. This program accepts a; b; eps and Nmax from the
keyboard and prints out the root (if any).

Test data and expected output:


Input: Output:
Test Case 1 Enter eps and Nmax :1.e-6 20 Root must be bracketed
Enter a, b :0 3
Input: Output:
Test Case 2 Enter eps and Nmax :1.e-6 10 Increase the max iteration
Enter a, b :0 2
Input: Output:
Test Case 3 Enter eps and Nmax :1.e-6 50 Root = 0.479731
Enter a, b :0 2

Q(8). Write a C program that accepts a positive integer n and a real number x from the keyboard and
prints out the sum of the n terms of the series
Test data and expected output:
Input: Output:
Test Case 1 Enter the value of n & x:0 1.0 Number of terms must be +ve

Input: Output:
Test Case 2 Enter the value of n & x:5 0.5 Sum of the series at x=0.50 with 5 terms is 0.47943

Input: Output:
Test Case 3 Enter eps and Nmax :1.e-6 50 Root = 0.479731
Enter a, b :0 2

Q(9) The following data of the velocity of a body is given as a function of time.
Time (s) 10 15 18 22 30
Velocity (m/s) 22 26 35 48 68

A linear Lagrange interpolant is found using three data points. Write a program in C to find the
velocity of car at given time instant (input).

Sample Test Cases

Input Output

Test Case 1 25 The respective value of the variable v is: 56.42

Test Case 2 16 The respective value of the variable v is: 28.74

𝑏
Q(10) Write a program to find ∫𝑎 𝑥 2 using Trapezoidal rule with 10 segments between a and b. The
values of a and b are input to the program.

Sample Test Cases


Input Output

Test Case 1 0 The integral is: 0.335000


1

Test Case 2 1 The integral is: 8.680000


3

Q(11) Write a program to solve the following differential equation using Runge-Kutta method. Step size
h=0.3
𝒅𝒚
10 + 3𝒚𝟑 = (𝒙)(𝒙 + 𝟏), Given y(0.3)=5
𝒅𝒙

Find y(x) for different values of x given as input to the program.

Sample Test Cases


Input Output

Test Case 1 0.9 y=1.4625

Test Case 2 1.2 y=-0.3062

Vous aimerez peut-être aussi