ITFN 2313 Mid-Term Practice (Fall 2009)

 

 

  1. Write a recursive function that displays the numbers counting up by 2 (0, 2, 4, 6, …) to 100.  Be sure you indicate how it would be invoked to get the desired results.  [15 Points]

static void RecursiveFunction(int x)

        {

            if (x > 100)  //Condition to stop recursion     

                return;  //Exit out of function

            else

            {

                Console.WriteLine(x);                        

                RecursiveFunction(x + 2);  //Call myself     

            }

        }

 

        static void Main(string[] args)

        {

            RecursiveFunction(0);                            

            Console.Read();

        }

 

  1. Write a snippet of code that asks the user to enter a number.  Then, create an exception handler that responds if the user enters something other than a number.  Hint: Your answer should include a try block and a catch block. (15 Points)

static void Main(string[] args)

        {

            int num;

            Console.WriteLine("Enter a number");

 

            try

            {

                num = Convert.ToInt32(Console.ReadLine());

            }

            catch (FormatException)

            {

                Console.WriteLine("That is not a number!");

            }

 

        }

  1. What is the output of the following code assuming it is called as f(1)?

 

11, 9, 7, 5, 3, 1

 

      {static void f(int i)

        {

            if (i < 10)

                f(i+2);

            Console.WriteLine(i);

        }  

 

  1. Write a recursive function that displays the numbers counting up by 3 (0, 3, 6, 9, …) to 99.
    static void RecursiveFunction(int x)

        {

            if (x > 99)  //Condition to stop recursion     

                return;  //Exit out of function

            else

            {

                Console.WriteLine(x);

                RecursiveFunction(x + 3);  //Call myself

 

            }

        }

 

        static void Main(string[] args)

        {

            RecursiveFunction(0);

            Console.Read();

        }

 

  1. Write a recursive function that takes in as a parameter N, a number (int), and prints out the numbers from N down to 1 (inclusively) in descending order and then in ascending order.  You must use recursion in your solution.  [15 Points]  Here is the function header:

 

void Print_N_to_1_to_N(int n)

 

For example, Print_N_to_1_to_N(6)would print 6 5 4 3 2 1 2 3 4 5 6.

 

 

public static void Print_N_to_1_to_N(int n)

        {

            if (n > 0)                     

            {

 

                Console.Write(n);          

                Print_N_to_1_to_N(n - 1);  

                Console.Write(n);          

            }

   }

 

 

  1. The following lines of code will throw what exception?  What is the throw point?  Write a try/catch to handle this exception.  [15 Points]

int numOne = 4;
int numTwo = 0;

        int sum=(numOne/numTwo);  Throw point – DivideByZero exception

try

            {

                int sum = (numOne / numTwo);           

            }

 

            catch (DivideByZeroException)

            {

                Console.WriteLine("You cannot divide by zero");            

 

          }

  1. Write a recursive function that takes in as a parameter N, a number (int), and returns the sum of the numbers from 1 to N (inclusively).  You must use recursion in your solution.  [15 Points] Here is the function header:

 

int Sum_1_to_N(int n)

 

      For example, Sum_N_to_1(5)would be 15 (1 + 2 + 3 + 4 + 5).

 

 

public static int Sum1_to_n(int n)

        {

            int answer = n;

 

            if (n > 0)         

            {

                answer = n + (Sum1_to_n(n - 1));    

                              

            }

           

            return answer;    

 }   

 

 

  1. What does the following code accomplish if is called as follows:  testString(s, 0, s.length-1)?  Briefly explain what happens with each line. [15 Points]

 

PALINDROME TEST

public static bool testString(string s, int left, int right)        function that takes in a string and two ints.  The two ints                       represent the start of the string and the end of the string

 

        {

            if (left >= right)                                                         terminating condition tests # of first letter against # of last letter

                return true;                                                                    e.g.  if string is “Sandra” left = 0 and right is = 5; makes progress toward middle of string

 

            else if (s[left] !=  s[right])                                         terminating condition tests if first letter is not equal to last letter

                return false;

 

            else                           

                return TestString(s, left + 1, right -1);                 recursive call, increases left by one, reduces right by one.

            }