Question

In: Computer Science

1 Draw a stack diagram to show how the following code is executed and write the...

1 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(a, b, c): if a < b < c: return a + b + c if a >= b: return sequence(a - 1, b, c) if a >= c: return sequence(c, b, a) if b >= c: return sequence(c, b, a + 2) return 0 print(sequence(10, 10, 10))

2 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(n, m): if n < 0 and m < 0: return n * m print(n, m) if m > n: return sequence(m, n) return sequence(n - 1, m) print(sequence(1, 0))

Solutions

Expert Solution

Q1.

def sequence(a, b, c):
    if a < b < c:
        return a + b + c
    if a >= b:
        return sequence(a - 1, b, c)
    if a >= c:
        return sequence(c, b, a)
    if b >= c:
        return sequence(c, b, a + 2)
    return 0

print(sequence(10, 10, 10))

Stack diagram for the code above:

Output:

30

Q2.

def sequence(n, m):
    if n < 0 and m < 0:
        return n * m
    print(n, m)
    if m > n:
        return sequence(m, n)
    return sequence(n - 1, m)
print(sequence(1, 0))

Stack diagram for the code above:


Related Solutions

Draw the stack (use, word/pdf) before every instruction that is marked red is executed to show...
Draw the stack (use, word/pdf) before every instruction that is marked red is executed to show your understanding of the call and return functions.   Use N/A to represent unpredictable values. Main Proc 4040018 mov ecx, 0000000Ch 404001C mov ebx, 0000000Bh 4040020 call FADD 4040026 mov eax, ebx … … Main EndP FADD PROC 4041040 Push ecx 4041044 Push ebx 4041048 mov eax, edx … … 404A060 Pop ebx 404A062 Pop ecx 404A064 ret FADD EndP
Draw the stack (use, word/pdf) before every instruction that is marked red is executed to show...
Draw the stack (use, word/pdf) before every instruction that is marked red is executed to show your understanding of the call and return functions.   Use N/A to represent unpredictable values. Main Proc 4040018 mov ecx, 0000000Ch 404001C mov ebx, 0000000Bh 4040020 call FADD 4040026 mov eax, ebx … … Main EndP FADD PROC 4041040 Push ecx 4041044 Push ebx 4041048 mov eax, edx … … 404A060 Pop ebx 404A062 Pop ecx 404A064 ret FADD EndP
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
Show the code how to add a node to the front of a list. Draw the...
Show the code how to add a node to the front of a list. Draw the picture also that goes with the code. NOW Show the code how to add a node to the end of a list. Draw the picture also that goes with the code. EXTRA CREDIT Show the code how to add a node to a specific spot in a list. Draw the picture also that goes with the code.
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...
Question 1 a. Draw a diagram to show the long run equilibrium condition of the perfectly...
Question 1 a. Draw a diagram to show the long run equilibrium condition of the perfectly competitive firm [5 marks] b. Explain three reasons why monopolies arise [3 marks] c. When does a perfectly competitive firm temporarily shut down production? When does it leave the industry all together? [4 marks] d. What obstacles might a group of oligopolists encounter in forming a cartel? [4 marks]
write instructions how to pipette 5 microliters of fluid AND draw a diagram that shows how...
write instructions how to pipette 5 microliters of fluid AND draw a diagram that shows how to complete the serial dilutions to conduct. Diagram of a Serial Dilution (use P-1000 set to 5,0,0)
1) What's the contents of array vals after the following code is executed? int[] vals =...
1) What's the contents of array vals after the following code is executed? int[] vals = {6, 1, 4, 5, 1}; int size = vals.length; for(int i = 0; i < size; i++) vals[i] = vals[i] * i; q) [ 6, 1, 4, 5, 1 ] b)[ 0, 1, 2, 3, 4] c)[0, 1, 8, 15, 4] d)[6, 2, 12, 20, 5] 2) Consider function foo defined below: static int[] foo(int vals[]) { int result[] = new int[vals.length - 1];...
the purpose of the code is to implement a stack how can i convert this code...
the purpose of the code is to implement a stack how can i convert this code in a way that these changes apply to it? // simplify the logic of the main // getline(cin, line) needs to be changed to a normal cin >> line; //make a function for 'list' // not allowed temporary stack (stack s2) //merge the catches (errors) // introduce another function for main // avoid repetitive code here is the code: #include #include #include #include #include...
1. draw a diagram to show the long run equilibrium condition of the perfectly competitive firm...
1. draw a diagram to show the long run equilibrium condition of the perfectly competitive firm 2. explain three reasons why monopolies arise
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT