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
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. 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
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
Write C Code for the following question which has two parts: a) Show how a typedef...
Write C Code for the following question which has two parts: a) Show how a typedef statement can be used to declare a new data type – to demonstrate this create a new type called Person which is the structure declared previously. b) Write C code to declare an array of 50 pointers to Persons. Using a loop, use malloc to dynamically allocate memory for each of these structures. There is no need to put any values into the structures....
Show, step by step, how the stack-based algorithm will transform the expression (1 + 2) *...
Show, step by step, how the stack-based algorithm will transform the expression (1 + 2) * (7 − 2) into a postfix expression, and then how a second stack-based algorithm will compute the value of this postfix expression.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT