Post 13 : Work Out Some nice Programming Challange


Problem 1. 
Input a range from user and print all the number that are palindromes in that range.
Hint: Use the logic of reversing the number.

Input: 1 to 22
Output: 11, 22.




Problem 2.
Write a Program to Produce the Following Output

                        1
                2                3
        4                5                6
7                8                9                10





Problem 3.
The probability that an individual telephone call will last less than t minutes can
be approximated by the exponential probability function
P(a call < t minutes) =1-  e^{-t/a}
where $a$ is the average call length and e = 2.71828. For example,
assuming that the average call length is 2.5 minutes, the probability that a call will
last less than one minute is calculated as 1 - e^{-1/2.5} = 0.3297.
Using this probability function, write a C program that calculates and displays a list
of probabilities of a call lasting less than one to less than 10 minutes, in one-minute
increments. Assume that the average call length is given as input.

Solution 3.

#include<stdio.h>
#include<math.h>
int main()
{
  double avgCallLength = 40,timeInitial = 1,increment = 1,timeFinal = 10;
  while( timeInitial <= timeFinal)
  {   
    double prob = 1 - pow(2.71828,(0-timeInitial)/avgCallLength);
    printf(" Probability of a call lasting less than %lf min is %lf   \n",timeInitial,prob);
    timeInitial = timeInitial + increment;
  }
  return 0;
}




Problem 4.
Input a positive number m (as double) and find its cube
root.  Use the Halley's Method for finding
successively better approximations to it.  In this method,
the approximation in the next step (denoted by X(n+1)) is a
function of the approximation in the current step (X(n)),
i.e., X(n+1) = f(X(n)).  For cube root, the estimates are
updated as:
X(n+1) = ( (a + 2 * m)/ (a + 0.5 * m) ) * (X(n)/2)
Here a = X(n) * X(n) * X(n)
Assume that the first term is m, i.e., X(0) = m.

Continue the estimation till the difference between two
successive approximations is less than 0.01. Print the
cube root thus obtained.

Problem 5.
Inversion Vector
An inversion vector, I(A) of an array A is an array of n-1 elements. The ith entry of the array I(A) is the number of elements
greater than A[i] preceding it in the original array A.
Clearly the first entry of I(A) is always zero as no element in an array precedes its first element. If the array is sorted then
all elements of inversion vector would be zero.
For e.g. in the array [25,7,52,3,21], the inversion vector would be [0,1,0,3,2].
You have to write a program which takes 7 distinct non-negative integers from the stdin. Store these numbers in an array.
It must then write a function which takes an int array as its argument and returns the inversion vector of the given array.
The program must also print each entry of the inversion array separated by a tab character.

Problem 6.
There is a 20x2 sized board. There are a n pieces of 2x1 sized blocks
available.  We need to fill the board with these blocks. Write a program to
calculate the number of ways in which the board can be filled with these
blocks.(Use Recursion)

Problem 7.
Flights option
Write a program which takes a time of day, t, from the user,
queries a known flight databse to output 2 flights with departure times
closest to the time provided by the user - one flight departing just
before time t and other one just after t. Each flight has a flight number
and there are at most 10 flights. That is flight nos range from 0-9.
The departure and arrival times are stored in two separate int arrays -
dep[] and arr[], such that flight with flight no. i has departure and
arrival time stored at dep[i] and arr[i] respectively. Use the number
of minutes elapsed since midnight as the measure of time. For e.g. if user
inputs the time 15:45, then the program should treat it as
15*60+45 = 945mins. The flight times should also to be stored in this
format. The code should print the arrival and departure times of the two
flights in 24hr clock format alongwith their flight nos in separate lines.
The prototype of the function should be
void getFlights(int time, int *dep, int *arr)

Output is printed in this function itself.

Sample Input/Output for a 5 flight database:
dep[] = {540, 720, 1050, 1260, 930} and
arr[] = {660, 870, 1200, 1410, 1080}.
(flight 1 takes 2hrs, flight 2 takes 2.5hrs and so on)

If the user inputs the time 15:45, the output shall be
flight 4 dep-15:30 arr-18:00

flight 2 dep-17:30 arr-20:00

Problem 8.
Rabbit and carrot

A rabbit walk is defined by a sequence of jumps which starts from a
location and returns back to the starting location. We can repesent such
a Rabbit walk by an array A of n numbers, where A[i] denotes the next
array index to which the rabbit should jumps from the location i.
For example, the sequence: 2 3 4 5 0 1 indicates that the Rabbit follows
the jump sequence: 0->2, 2->4, 4->0 and stops.

Your also given another array B of N numbers, such that
B[i] denotes the number of carrots available at location i.
Your task is to compute the number of carrots that a rabbit can collect
when it makes rabbit walk(s) from various start position(s).
A sample input is provided for more clarification(s).

Input:
The first line will contain a single integer N which is the size of the
    array (2<= N <= 10000).
The next line contains a series of N integers which define the array A.
The next line contains a series of N integers which define the array B.
You can be assured that all the calculations fit into a 32 bit signed
integer type(int in C).

The input is given such that, a rabbit walk will always exist with
   start position as 0.

Output:
It should contain the number of carrots collected
when 0 is treated as the start location.

Sample Input:
6
2 2 2 2 -4 -4
2 3 6 3 0 1

Sample Output:

8




Comments

Popular posts from this blog

Post 8 : Boot Into Mountain Lion 10.8.2

Post 3: Before Installation

Post 1: Requirements To Install Mac OS x on non Mac system