Question

In: Computer Science

Q2: The following code is supposed to return the sum of the numbers between 1 and...

Q2:

The following code is supposed to return the sum of the numbers between 1 and n inclusive, for positive n. An analysis of the code using our "Three Question" approach reveals that:

int sum(int n){

if (n == 1)

    return 1;

else

    return (n + sum(n - 1));

}

Answer Choices:

it passes on all three questions and is a valid algorithm.

   

it fails the smaller-caller question.

   

it fails the base-case question.

   

it fails the general-case question.

Q3:

What does the following return, when passed the argument 1564?

int recur(int n)

{

if (n < 0) return -1;

else

    if (n < 10) return n;

    else

      return ((n % 10) + recur(n / 10));

}

Answer Choices:

160

   

16

   

-1

   

4



Q4:

The following code is supposed to return n!, for positive n. An analysis of the code using our "Three Question" approach reveals that:

int factorial(int n){

if (n == 0)

    return 2;

else

    return (n * factorial(n – 1));

}

Answer Choices:

it fails the base-case question.

   

it fails the general-case question.

   

it passes on all three questions and is a valid algorithm.

   

it fails the smaller-caller question.

Solutions

Expert Solution

Q2.

Ans: (a) it passes on all three questions and is a valid algorithm.

Because it satisfies all case.

Q3.

Ans: 16

It adds all the digits of the number.

Q4.

Ans: (a)  it fails the base-case question.

Here base case is 0!=1 and 1!=1

But these condtions are not correct in program . so base case fails.

Here base case fails . so entire program is wrong it does not satisfy any case.


Related Solutions

Sum of numbers between 1 and 10
Calculate the values from the  smallest number to the largest number
create two random numbers between 1 and 6. if when the sum of two numbers are...
create two random numbers between 1 and 6. if when the sum of two numbers are added togethere their sum is less than 5 or greater than 12, output to the console: "you win". if is not, output "you lose" C++
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Writing method in Java Given a string, return the sum of the numbers appearing in the...
Writing method in Java Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.) sumNumbers("abc123xyz") → 123 sumNumbers("aa11b33") → 44 sumNumbers("7 11") → 18
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers = {22, 33, 44}; for(int k = 0; k < 3; k++) { numbers[k] = numbers[k] + 5; } } 2. What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } 3.What is the value of numbers [3] after the following line...
Write assembly instructions that compute the following: The sum of all even numbers between 2 and...
Write assembly instructions that compute the following: The sum of all even numbers between 2 and 100 (inclusive) -- the answer should be 2550 Prompt the user for 2 values, put them in variables a and b, then find the sum of all odd numbers between a and b. The sum of all the squares between 1 and 100 (inclusive) . -- the answer should be 338350 All of the statements above should print the answers using print_int MUST BE...
QUESTION 23   What is the sum of numbers between 1 and 20? 210 250 neither A...
QUESTION 23   What is the sum of numbers between 1 and 20? 210 250 neither A or B QUESTION 24 What is 8 to the 3th power? 192 384 QUESTION 25 What is the 4th root of 765 (rounded)? 5.3 7.1 1) In the United States the foreign currency market would not be considered financial market. True False
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
C++ CODE TO FIND ALL Odd numbers between 1 and 70000.
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT