1)Place a red ball in a urn and all the further balls in the other urn.The probability for picking out the red ball is now greater than 0.5.
2)If v<=2V then the position is (v*L)/(2*V) from the starting point else it is 2*L -(v*L)/(2*V) from the starting point.
4)If we know the process then we can kill it by killall -9 "process name" else we can kill it using its process id obtained by the command ps -x by kill -9 "processid" .
5)Top command displays all the Linux tasks running at that particular time.It provides their running time and the resources used.
6)The number appearing 2 times is (sum of all the numbers in the array) - (sum of the numbers from 1 to n).
For floating numbers multiply it with 100 and proceed.
- C program to create mirror copy of a tree
Posted: Thu, 29 Nov 2007 23:10:59 -0600
Question:Write a C program to create a mirror copy of a tree left nodes become right and right nodes become left)
Solution:#include<stdio.h>
struct binarysearchtree{
int data;
struct binarysearchtree* left;
struct binarysearchtree* right;
};
typedef struct binarysearchtree* tree;
tree mirror_copy(tree T)
{
if(T==NULL)
return T;
else
{
tree temp1=mirror_copy(T->left);
tree temp2=mirror_copy(T->right);
T->left=temp2;
T->right=temp1;
return T;
}
} - Solutions to Questions on recursion
Posted: Tue, 04 Dec 2007 04:28:58 -0600
1)
int Fibinocci(int n)
{
if(n==1)
return 1;
else
n+Fibinocci(n-1);
}
2)
void reverse(char*str)
{
if(*str != '\0')
reverse(str+1);
}
3)
int Factorial(int n)
{
if(n==1)
return 1;
else
return(n*Factorial(n-1);
}
4)
void MoveTower(int disk, int source, int dest, int spare):
{
if(disk == 1)
printf("Move top disc from %d to %d\n",source,desc);
else
{
MoveTower(disk - 1, source, spare, dest);
printf("Move top disc from %d to %d\n",source,desc);
// Step 2
MoveTower(disk - 1, spare, dest, source);
}
}
5)
int gcd(int a,int b)
{
if(b==0)
return(a);
else
return(gcd(b,a(mod)b);
}
6)
arr is an array containing N integers.You can also change the program by keeping characters.k is initially 0.
void permutation(int *arr, int N, int k)
{
static level = -1;
level = level+1;
arr[k] = level;
if (level == N)
{
if(arr!=0)
for(int i=0; i < N;i++)
printf("%d",arr[i]);
}
else
for (int i = 0; i < N; i++)
if (arr[i] == 0)
permutation(arr, N, i);
level = level-1;
arr[k] = 0;
}
7)void combinations(char*str,int no)
{
int temp1=0;
int i,count,n=1,num,len;
for(i=0;*(str+i)!='\0';i++);
len=i;
for(i=0;i < len;i++)
n=2*n;
temp=(int*)malloc(len*sizeof(int));
for(i=0;i < len;i++)
*(temp+i)=0;
for(num=0;num <= n;num=num+1)
{
temp1=num;
for(i=0;i < len;i++)
{
*(temp+i) = temp1%2;
temp1=temp1/2;
}
count=0;
for(i=0;i < len;i++)
{
if(*(temp+i)==1)
count++;
}
if(count==no)
{
for(i=0;i < len;i++)
{
if(*(temp+i)==1)
printf("%c",*(str+i));
}
printf("\n");
}
}
8)
Mergesort:a is an array of n intergers,temp is just a temporary array.low is initially 0 and high is n-1.
Quicksort:a is an array of n integers.left is initially 0 and right is n-1. Posted: Thu, 29 Nov 2007 10:10:14 -0600
Question:Write a C program to find the minimum value in a binary search tree.
Solution: