Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Put comments on the following routines to identify the base and general cases an

ID: 3732205 • Letter: P

Question

Put comments on the following routines to identify the base and general cases and explain that each routine does.

      a.     int Power (int base, int exponent)

{

     if (exponent == 0)

          return 1;

     else

          return base * Power (base, exponent -1);

}

       b.     int Factorial (int number)

{

     if (num > 0)

          return num * Factorial (num – 1);

     else

           if (num == 0);

                return 1;

}

       c.     void Sort (int values[], int fromIndex, int toIndex)

{

     int maxIndex;

     if (fromIndex != toIndex)

    {

          maxIndex = MaxPosition (values, fromIndex, toIndex);

          Swap (values[maxIndex], values[toIndex]);

          Sort(values, fromIndex, toIndex – 1);

      }

}

Explanation / Answer

a. int Power (int base, int exponent)

{

if (exponent == 0) //Base Case

return 1;

else

return base * Power (base, exponent -1); //General Cases

}

Explanation: If Exponent values are not equal to 0, then recursively value is calculated with each time exponent value reduced by 1. A time will come when exponent value will become 0 and the base condition will execute returning 1 to its previous call. This condition will lead to termination of recursion and once all function call pops out from memory stack, the final value is returned.

b. int Factorial (int number)

{

if (num > 0) //general Case

return num * Factorial (num – 1);  

else

if (num == 0); //Base case

return 1;

}

Explanation: If num value is not 0, then recursively num is multiplied by its value with its value decreasing by 1 with each function call. Once the value of num becomes 0 then the base condition is executed returning 1 to its previous call. This condition will lead to termination of recursion and once all function call pops out from memory stack, the final value of num is returned.

c. void Sort (int values[], int fromIndex, int toIndex)

{

int maxIndex;

if (fromIndex != toIndex) //general case

{

maxIndex = MaxPosition (values, fromIndex, toIndex);

Swap (values[maxIndex], values[toIndex]);

Sort(values, fromIndex, toIndex – 1);

}

}

Here sorting is carried out with given general condition that if fromIndex is not equal to toIndex. While this condition is not fulfilled till then maxIndex is calculated using MaxPosition function, then Swapping of values is carried out using Swap function where 2 values of the array are passed. Once swapping is done then again Sort function is called with toIndex value reduced by 1. A time will come when the toIndex value will become equal to fromIndex and then the if statement will become false. Since the return type of function is void so no value will be returned and therefore there is no need for base condition here. Once the if statement becomes false it will automatically pass control to the previously calling recursive function and in this way, all function call will pop out from memory stack and the initial calling function will complete its execution.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote