Question

In: Computer Science

What is the output of the following codes, consider each case independently. I have an example...

What is the output of the following codes, consider each case independently. I have an example of Case 1 to show you how to complete each case at the end of this Module.

Case 1: What will be the value of x after the following loop is executed:

x = 0

For i = 1 To 2

For j = 1 To 3

     x = x * i * j

Next j

Next i

Case 2: What will be the value of x after the following loop is executed:

x = 1

For i = 1 To 2

For j = 1 To 3

     x = x * i * j

Next j

Next i

Case 3: What will be the value of x after the following loop is executed:

x = 0

For i = 1 To 2

For j = 1 To 3

     x = x * i + j

Next j

Next i

Case 4: What will be the value of x after the following loop is executed:

x = 0

For i = 1 To 2

For j = 1 To 3

     x = x + i * j

Next j

Next i

Please see below for an example of completed Case 1. Follow the same method to complete the other three cases. Let me know your questions.

x

i

j

NOTES

0

x is initialized to 0

x = 0*1*1, the new value for x is 0

1

1

First we start with the outer loop (for I ) and continue with inner loop (for J ).

x = 0*1*2, the new value for x is 0

1

2

Still in the inner loop when J becomes 2, note I is 1

x = 0*1*3, the new value for x is 0

1

3

Still in the inner loop when J becomes 3, note I is 1

x = 0*2*1, the new value for x is 0

2

1

Here program continues with the outer loop, where I is 2 and moves to inner loop where J is 1

x = 0*2*2, the new value for x is 0

2

2

Program continues with the inner loop when J becomes 2, note I is 2

x = 0*2*3, the new value for x is 0

2

3

Program continues with the inner loop when J becomes 3, note I is 2

X = 0

Is the final value for x when both loops are completed

Solutions

Expert Solution

case 1 is already solved so i start with case 2

Case 2:

x

i

j

NOTES

1

x is initialized to 1

x = 1*1*1, the new value for x is 1

1

1

First we start with the outer loop (for I ) and continue with inner loop (for J ).

x = 1*1*2, the new value for x is 2

1

2

Still in the inner loop when J becomes 2, note I is 1

x = 2*1*3, the new value for x is 6

1

3

Still in the inner loop when J becomes 3, note I is 1

x = 6*2*1, the new value for x is 12

2

1

Here program continues with the outer loop, where I is 2 and moves to inner loop where J is 1

x = 12*2*2, the new value for x is 48

2

2

Program continues with the inner loop when J becomes 2, note I is 2

x = 48*2*3, the new value for x is 288

2

3

Program continues with the inner loop when J becomes 3, note I is 2

X = 288

Is the final value for x when both loops are completed

Case 3:

x

i

j

NOTES

0

x is initialized to 0

x = 0*1+1, the new value for x is 1

1

1

First we start with the outer loop (for I ) and continue with inner loop (for J ).

x = 1*1+2, the new value for x is 3

1

2

Still in the inner loop when J becomes 2, note I is 1

x = 3*1+3, the new value for x is 6

1

3

Still in the inner loop when J becomes 3, note I is 1

x = 6*2+1, the new value for x is 13

2

1

Here program continues with the outer loop, where I is 2 and moves to inner loop where J is 1

x = 13*2+2, the new value for x is 28

2

2

Program continues with the inner loop when J becomes 2, note I is 2

x = 28*2+3, the new value for x is 59

2

3

Program continues with the inner loop when J becomes 3, note I is 2

X = 59

Is the final value for x when both loops are completed

Case 4:

x

i

j

NOTES

0

x is initialized to 0

x = 0+1*1, the new value for x is 1

1

1

First we start with the outer loop (for I ) and continue with inner loop (for J ).

x = 1+1*2, the new value for x is 3

1

2

Still in the inner loop when J becomes 2, note I is 1

x = 3+1*3, the new value for x is 6

1

3

Still in the inner loop when J becomes 3, note I is 1

x = 6+2*1, the new value for x is 8

2

1

Here program continues with the outer loop, where I is 2 and moves to inner loop where J is 1

x = 8+2*2, the new value for x is 12

2

2

Program continues with the inner loop when J becomes 2, note I is 2

x = 12+2*3, the new value for x is 18

2

3

Program continues with the inner loop when J becomes 3, note I is 2

X = 18

Is the final value for x when both loops are completed


Related Solutions

4. Other things equal, what effect will each of the following changes independently have on the...
4. Other things equal, what effect will each of the following changes independently have on the equilibrium level of real GDP in the private closed economy? 1. A decline in the real interest rate. 2. An overall decrease in the expected rate of return on investment. 3. A sizable, sustained increase in stock prices. (answer in your own words)
Consider each of the following independent and material situations, identified below (i-v). In each case: •...
Consider each of the following independent and material situations, identified below (i-v). In each case: • the balance date is 30 June 2020; • the field work was completed on 12 August 2020; • the Directors’ Declaration and the Audit report were signed on 19 August 2020; • the completed financial report accompanied by the signed Audit report was mailed to the shareholders on 25 August 2020. (i) On 29 September 2020, you discovered that a debtor at 30 June...
Problem: Consider each of the following situations independently. Fill in the blanks with the appropriate information....
Problem: Consider each of the following situations independently. Fill in the blanks with the appropriate information. 1. selling price per unit Variable cost percentage number of units sold total contribution margin Total fixed costs Net Income (Loss) $90.00 20,000 $750,000 ($30,000) 2. Number of Units sold Total Variable Cost Variable Cost percentage Total Contribution Margin Total Fixed Costs Net Income (Loss) 1,800 30% $756,000 $54,000 3. Selling Price per unit Total Sales Number of Units Sold Variable Cost per unit...
Illustrate each of the following scenarios. In each case, assume that the corresponding output market is...
Illustrate each of the following scenarios. In each case, assume that the corresponding output market is competitive. a. Equilibrium in a perfectly competitive labor market (be sure to draw both a market and a firm graph). b. Equilibrium in a monopsony labor market. c. Equilibrium in a monopoly labor market (for example, a labor market with a union maximizing economic rent).
PYHTON A company uses codes to represent its products, for example ABC2475A5R-14. Valid codes must have:...
PYHTON A company uses codes to represent its products, for example ABC2475A5R-14. Valid codes must have: At least 10 characters (some codes have more than 10 characters) Positions 4 through 7 must be digits (and represents the country in which the product will be sold) The character in the 10th position must be a capital letter and represents the security level of the product. Write a Python program that will read 10 product codes from a text file (Codes.txt -...
Consider a population in which there are no deaths and each individual independently gives birth at...
Consider a population in which there are no deaths and each individual independently gives birth at rate 1. Independently of the other events, immigrants arrive in the population at times of a Poisson process with rate 2. Let X(t) denote the number of individuals in the population at time t. The pure birth process (X(t),t ≥ 0) is called a Yule process with immigration. (a) Give the birth rates for the process (X(t),t ≥ 0). (b) Find P(X(2) = 1|X(0)...
Consider each of the following situations independently. Annual cash inflows from two competing investment opportunities are...
Consider each of the following situations independently. Annual cash inflows from two competing investment opportunities are given below. Each investment opportunity will require the same initial investment. Compute the present value of the cash inflows for each investment using a 20% discount rate. Year Investment X Investment Y 1 $1,000 $4,000 2 2,000 3,000 3 3,000 2,000 4 4,000 1,000 Total $10,000 $10,000 At the end of three years, when you graduate from college, your father has promised to give...
Assign the correct CPT codes for each case study. Case Study # 1: Preoperative Diagnosis: Angina...
Assign the correct CPT codes for each case study. Case Study # 1: Preoperative Diagnosis: Angina Postoperative Diagnosis: Patent coronary arteries and grafts, ASHD present Procedure: Right and left heart cath with coronary angiography The patient was brought to the cath lab in a fasting state. The right going was prepped and draped in the usual sterile fashion. After local anesthesia was administered, sheaths were placed percutaneously into the right femoral artery and vein. IV heparin 3,000 units were given....
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
Consider each of the following independent and material situations. In each case: • the financial report...
Consider each of the following independent and material situations. In each case: • the financial report date is 31 December 2019; • the field work was completed on 12 February 2020; • the directors declaration and the audit report were signed on 19 February 2020; and • the completed financial report accompanied by the signed audit report were mailed to shareholders on 18 March 2020 A. You are an auditor pf PP Limited (PP), a company specialising in industrial property...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT