Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

DSA TUTORIAL : MISCELLANEOUS

NAME : SHASHI SAURABH


ID : 2020UCP1774.

Q1. Prove by induction.


(a). Σ x=n(n+1)/2 x>=1

Solution:-
Step 1:- “the check” for n=1, x=1(2)/2 = 1

Step 2:- “the induction step” assuming the result is true for n=k; i.e.,
assuming Σ x=k(k+1)/2 (Σ from 1 to k)

The sum for n = k+1 can be written as

Σx(for1tok+1)= Σx(for1tok)+(k+1)

= k(k+1)/2 + (k+1)

= (k+1)( k/2 + 1)

= (k+1)(k+2)/2

Which is the desired result for n=k+1.

(b).Σ x^2=n(n+1)(2n+1)/6 x>=1

Solution:-
Step 1:- “the check” for n=1, x=1(2)(3)/6 = 1
Step 2:- ‘the induction step” assuming the result is true for n=k; i.e.,
assuming

Σ x^2=k(k+1)(2k+1)/6 (Σ from 1 to k)

The sum for n = k+1 can be written as

Σ x^2(for 1 to k+1)

= Σ x^2(for 1 to k) + (k+1)^2

= k(k+1)(2k+1)/6 + (k+1)^2

= (k+1)(k(2k+1)/6 +(k+1))

= (k+1)(2k^2+k+6k+6)/6

Which is the desired result for n=k+1.

(c). Σ x^3= (n(n+1))^2/4 x>=1

Solution:-
Step 1:-”the check” for n=1, x=(1(2))^2/4 = 1

Step 2:- “the induction step” assuming the result is true for n=k; i.e.,
assuming

Σ x^3= (n(n+1))^2/4 n>=1 (Σ from 1 to n)

The sum for n = k+1 can be written as

Σ x^3(for 1 to k+1)

= Σ x^3(for 1 to k) + (k+1)^3

= (k(k+1))^2/4 + (k+1)^3

= (k+1)^2(k^2/4 +(k+1))

= (k+1)^2(k^2+4k+4)/4
=(k+1)^2(k+2)^2/4

=((k+1)(k+2))^2/4

Which is the desired result for n=k+1.

(d). Σ x^k = (x^(n+1) -1)/(x-1) x>1


Solution:-
Step 1:- “the check” for x=2, n=2, Σ x^k =((2)^3 - 1)/1 = 7

Step 2:- (the induction step) assuming the result is true for k=n; i.e.,
assuming

Σ x^k = (x^(n+1) -1)/(x-1) x>1 (Σ for k=0 to k=n)

The sum for k = n+1 can be written as

Σ x^k(Σ for k=0 to k=n+1) = Σ x^k(Σ for k=0 to k=n) + x^(n+1)

= (x^(n+1) -1)/(x-1) + x^(n+1)

= (x^(n+1) -1 + (x-1) x^(n+1))/(x-1)

= ((x)x^(n+1) -1)/ (x-1)

=(x^(n+2)-1)/(x-1)

Which is the desired result for n=k+1.

Q2. Write recursive procedure for :-


(a). factorial of n
Solution:-

int factorial (int n)


{
If(n==1)
{
return 1;
}
return n*factorial(n-1);
}

(b). computing binomial coefficient Cr


Solution:-
int factorial (int n)

If(n==1)

return 1;

return n*factorial(n-1);
}

Now input n and r


Int p = factorial(n);

Int q = factorial(r);

Int j=factorial(n-r);

Int :ans = p/(q*j);

Output the ans.

(c). number of substrings of a string of length n


Solution:-
int substrlen(string s)

If(s[0]==’\0’)

return 0;

Else if(s[0]!=’\0’ && s[1]==’\0’)

return 1;

int ans = substrlen(s [1]);

int j = 0;

for(int i=0;s[i]!=’\0’;i++)

j++;

return ans + j;
}

(d). powerset of a set S of cardinality n


Solution:-
void powerSet(string str, int index=0, string curr =""){

int n = str.length();

if(index==n)

cout<<curr<<"\n"; }

else{

powerSet(str, index+1, curr + str[index]); powerSet(str, index+1, curr);

Q3. Prove that


(a). ⌊−x⌋ = −⌈x⌉
Solution:-
⌊−x⌋ is the greatest integer function and −⌈x⌉ is the ceiling function
which

returns the next greater integer.

LHS=⌊-x⌋=-⌊x⌋-1 (GIFproperty)

RHS -⌈x⌉ = -(⌊ x ⌋ +1)


−⌈x⌉= -⌊x⌋-1

LHS = RHS

Hence proved.

(b). ⌈−x⌉ = −⌊x⌋


Solution:-
If x not equal to integer, then we can say that

⌈x⌉ - ⌊x⌋ = 1

Now we can replace x by –x

⌈−x⌉ - ⌊-x⌋ = 1

⌈−x⌉ = ⌊-x⌋ + 1.

⌈−x⌉ = -1 - ⌊x⌋ +1

⌈−x⌉ = −⌊x⌋

If x is integer, then it is obviously true.

c) ⌊−x⌋+⌊x⌋=0 if x∈Z else−1


Solution:-
Case 1:- If x∈Z, then

⌊−x ⌋ = -x

And, ⌊x⌋ = x

Now –x+ x =0 , hence proved

Case 2:- If x is not an integer, then

Let assume x = (N+F)


⌊−x ⌋ = ⌊−(N+F) ⌋

= ⌊−N-F⌋

= -N – 1

And,

⌊x ⌋ = N

where(N is an integer and F is the fractional part)

⌊−x⌋+⌊x⌋=-N–1+N= -1,

hence proved.

d)⌈-x⌉+⌈x⌉=0if x∈Z else1


Solution:-
Case 1:- If x∈Z, then

⌈-x⌉ = -x

And, ⌈x⌉ = x

Now –x+ x =0 , hence proved

Case 2:- If x is not an integer, then

Let assume x = (N+F) where(N is an integer and F is the fractional


part)

⌈-x⌉ = ⌈-(N+F) ⌉

= ⌈-N -F ⌉

= -N

And,

⌈x⌉ = N + 1

⌈-x⌉ + ⌈x⌉ = -N + N + 1 = 1,

hence proved.
e) ⌊x+n⌋=⌊x⌋+n
Solution:-
Here x = integer + fraction and n is an integer

x= p + q (here p is integer value and q is fractional value)

⌊x+n⌋ = ⌊p + q +n⌋

Now ⌊(p + n) + q⌋ = p + n

Now in rhs

⌊p + q⌋ + n

p+ n

Hence LHS = RHS.

f) ⌈x+n⌉=⌈x⌉+n
Solution:-
Since n is an integer and x = p + q

⌈p + q + n⌉ = ⌈( p + n )+ q⌉ = p + n +1

In RHS,

⌈p+q⌉+n. (⌈p+q⌉returnsp+1sincepisintegerandqisfractional)

p+ 1 + n

LHS = RHS

Hence proved.
g) ⌈x/2⌉+⌊x/2⌋=x given x€Z
Solution:-
Case 1:- Let x be an even integer

Then ⌈x/2⌉ = x/2 and ⌊x/2⌋ = x/2

=> LHS = x/2 + x/2=x

=> RHS = X

Since LHS = RHS hence proved.

Case 2:- Let x be an odd integer

Then ⌈ x/2 ⌉ = ⌊ x/2 ⌋ + 1

=> LHS = ⌊x/2⌋ + 1 + ⌊x/2⌋

= (2)⌊x/2⌋ + 1

= (2) ⌊(x-1)/2 + 1/2⌋ + 1 (since x is odd, therefore (x-1) will be an even no.)

= (2)((x-1)/2 ) + 1

=> x-1+1=x

=> RHS = X

Since, LHS = RHS

hence proved.

Q4. Write a C program to evaluate a polynomial


A(x)=anxn+ an-1xn-1+ ...+ a1x +a0 at point x0 using
Horner’s rule
A(x)=(...((anx0 +an-1)x0 + ... +a1)x0+a0
Solution:-

#include<stdio.h>

#define MAX_SIZE 100

int main(){

int arr[MAX_SIZE], deg, n, j, result=1,x;

printf("Enter the degree of the polynomial:");

scanf("%d", &deg);

n=deg+1;

j=deg;

printf("Enter all the coefficient of the array: ");

for(int i=0; i<n; i++){

printf("a(%d): ", j);

scanf("%d", &arr[i]); j--;

printf("Enter the point at which polynomial has to be evaluated: ");

scanf("%d", &x);

result=arr[0]*x + arr[1];

for(int j=2; j<n; j++){

result= result*x + arr[j];

printf("Value of the polynomial is: %d", result);

return 0;
}

Q5. Write a C program to evaluate sin(x)

Solution:-
#include<stdio.h>

#include<math.h>

float factorial(int);

void sin_value(float, float*);

int main(){

int x;

float radian, result = 0;

printf("Enter value of x in degrees\n");

scanf("%d", &x);

radian = x * (3.14159 / 180.0);

// Convert Degree To Radian

sin_value(radian, &result);

printf("The value of Sin(%d) = %f\n", x, result);

return 0;

void sin_value(float number, float *res){

int count, n = 1, sign = 1;


for(count = 1; (n <= 10); count += 2){

*res += sign * ( pow(number, count) / factorial(count) );

n +=1;

sign *= -1;

float factorial(int number){

int count;

float fact = 1;

for(count = 1; count <= number; count++){

fact *= count;

return(fact);

You might also like