Question

In: Computer Science

Determine the output of the following pseudo-code. Also, explain how did you get this output. fun(int...

Determine the output of the following pseudo-code. Also, explain how did you get this output.

fun(int n){
   if (n==1) then return;
   println(n);
   if (n%2=0) then
     fun(n/2);
   else
     fun(3*n + 1);
}
Main(){
  fun(14);
}

Solutions

Expert Solution

In writing pseudocode you have a syntax error at line 4 "if(n%2=0) then".

I am assuming the line is "if(n%2==0) then".

Output of the code:

14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2

Now let's check how we got the output:

Note: There are two recursive calls in function one if n is even (fun(n/2)) and one if n is not even (f(n*3+1)). Recursion terminating condition is if n is 1.

Initially function is called at value 14:

fun(14): according to the function definiton if n == 4 control should be returned from the function, but 14 is not equals to 1 so this condition fails.

Now function is printing n in new line (assuming println is printing in new line), So 14 will be printed to the output screen.

Now function is cheking if n is even or odd, 14 is even so fun(14/2) (fun(7)) is called.

Similarly:

fun(7):

prints 7 and calls f(3*7+1) is called which is fun(22). (because 7 is odd in function definiton on odd condition f(n*3+1) is called)

fun(22):

  prints 22 and calls fun(22/2) => fun(11)

Similarly,

fun(11): prints 11, calls fun(11*3+1) => fun(34)

fun(34): prints 34, calls fun(34/2) => fun(17)

fun(17): prints 17, calls fun(17*3+1) => fun(52)

fun(52): prints 52, calls fun(52/2) => fun(26)

fun(26): prints 26, calls fun(26/2) => fun(13)

fun(13): prints 13, calls fun(13*3+1) => fun(40)

fun(40): prints 40, calls fun(40/2) => fun(20)

fun(20): prints 20, calls fun(20/2) => fun(10)

fun(10): prints 10, calls fun(10/2) => fun(5)

fun(5): prints 5, calls fun(5*3+1) => fun(16)

fun(16): prints 16, calls fun(16/2) => fun(8)

fun(8): prints 4, calls fun(4/2) => fun(2)

fun(2): prints 2, calls fun(2/2) => fun(1)

  fun(1): Here control is returned from the function.


Related Solutions

[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer a = 5 Declare Integer b = 2 Declare Integer c = 3 Declare Integer result = 0 Display "The value of result is" Display result Set result = a + b * c - a Display "Changed value is: ", result End Module
please explain how does the following C code work. a) int function1(int num1, int num2) {...
please explain how does the following C code work. a) int function1(int num1, int num2) { int num = num1 ^ num2; int result = 0; while(num > 0) { result += (num & 1); num >>= 1; } return result; } b) int function2(unsigned int num) { return num & ~(num - 1); } c) int f1(unsigned int x) { int count = 0; while(x) { count++; x = x&(x-1); } return count; } d) double ldexp(double x, int...
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
Determine statistically which two bands are the most correlated? Explain how did you get your answer...
Determine statistically which two bands are the most correlated? Explain how did you get your answer and include intermediate calculations. 105 97 85 82 82 81 108 104 93 82 81 81 106 109 102 88 81 81 106 108 103 89 84 81 105 106 104 95 89 83 104 102 98 94 90 86 Green 128 115 95 89 89 90 129 124 109 94 89 89 128 133 125 102 93 89 129 134 124 101 95...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
What is the output of the following piece of Java code? int id = 4; double...
What is the output of the following piece of Java code? int id = 4; double grossPay = 81.475; String jobType = "Intern"; System.out.printf("Id: %d, Type: %s, Amount:%.2f", id,grossPay,jobType); Select one: Error Id: 4, Type: Intern, Amount: $81.475 Id: 4, Type: $81.475, Amount: Intern Id: 4, Type: $81.48, Amount:Intern Id: 4, Type: Intern, Amount: $81.48
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT