Question

In: Computer Science

Explain briefly what the following algorithm does and write the output below: (5 credits) Step 1:...

Explain briefly what the following algorithm does and write the output below: (5 credits)

Step 1: Set sum equal to 0, set n equal to 1

Step 2: Repeat steps 3, 4 and 5 until sum > 60

Step 3: Set sum equal to sum + n2

Step 4: Replace n with n +1

Step 5: Print n and sum on a new line

Step 6: Stop

Show output:

Solutions

Expert Solution

This algorithm is calculating the sum of square of numbers[ 1,2,3... ] and printing that number along with the sum before that number.

Algorithm Dry Run Explanation:

Step 1: sum=0, n=1

Step 2: sum<60 return True

Step 3: sum = sum+n2 =0 + (1)2 = 1

Step 4: n = n+1 = 1+1=2

Step 5: print n and sum, 2 1 will be printed

Step 2: sum<60 return True

Step 3: sum = sum+n2 =1 + (2)2 = 5

Step 4: n = n+1 = 2+1=3

Step 5: print n and sum, 3 5 will be printed

Step 2: sum<60 , 5<60 return True

Step 3: sum = sum+n2 =5 + (3)2 = 5+9 = 14

Step 4: n = n+1 = 3+1=4

Step 5: print n and sum, 4 14 will be printed

Step 2: sum<60, 14<60 return True

Step 3: sum = sum+n2 =14 + (4)2 = 14+16=30

Step 4: n = n+1 = 4+1=5

Step 5: print n and sum, 5 30 will be printed

Step 2: sum<60 return True

Step 3: sum = sum+n2 =30 + (5)2 = 30+25=55

Step 4: n = n+1 = 5+1=6

Step 5: print n and sum, 6 55 will be printed

Step 2: sum<60 , 55<60 return True

Step 3: sum = sum+n2 =55 + (6)2 = 55+36 = 91

Step 4: n = n+1 = 6+1=7

Step 5: print n and sum, 7 91 will be printed

Step 2: sum<60 , 91<60 return False

Step6: Stop

OUTPUT:

2 1

3 5

4 14

5 30

6 55

7 91


Related Solutions

What is TimSort and how does it work? TimSort example with step by step explanation. Algorithm...
What is TimSort and how does it work? TimSort example with step by step explanation. Algorithm analysis of Timsort with python implementation, including pseudo code and T(n) time complexity analysis
Write a program, using any language you want, that uses a Randomized-Select algorithm. Briefly explain what...
Write a program, using any language you want, that uses a Randomized-Select algorithm. Briefly explain what it does, how it does. Include code and show output.
Please show that answer step by step and explain clearly, thx!!!! 5. Does the accuracy of...
Please show that answer step by step and explain clearly, thx!!!! 5. Does the accuracy of a kNN classifier using the Euclidean distance change if you (a) translate the data (b) scale the data (i.e., multiply the all the points by a constant), or (c) rotate the data? Explain. Answer the same for a kNN classifier using Manhattan distance.
Briefly write 4-5 short sentences on each of the 6 topics below. Explain its relevance to...
Briefly write 4-5 short sentences on each of the 6 topics below. Explain its relevance to that of a financial manager. 1) Financial Statement Analysis 2) Working Capital Management 3) Time Value of Money 4) Risk and Return Tradeoff 5) Securities Valuation 6) Capital Budgeting
Conduct a regression and correlation hypothesis test step 1 to step 5 on the data below...
Conduct a regression and correlation hypothesis test step 1 to step 5 on the data below before you answer each question. The number of fat calories and number of saturated fate grams for a random selection of nonbreakfast entrees are shown below. Use a .05 level of significance. Y X 9 190 8 220 13 270 17 360 23 460 27 540 What is the null and alternative hypotheses step one? a. H0: There is no relationship. H1: There is...
1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5!...
1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5! – x7/7! +……. Stop when the term<0.0001. 2) An internet service provider charges its subscribers per month as follows: Data usage (n), in gbs charges (NIS) 0.0<n<=1.0 250 1.0<n<=2.0 500 2.0<n<=5.0 1000 5.0<n<=10.0 1500 n>10 2000 Write a C program to read the usage(n) from a file and print the charges to be paid by the subscribers. Your program must include the function calculate...
Answer the question below: · Explain what is K-mean Algorithm? · Explain what is KPI, design...
Answer the question below: · Explain what is K-mean Algorithm? · Explain what is KPI, design of experiments and 80/20 rule?
Briefly explain the rules of debits and credits as they relate to assets, liabilities, equity, revenue,...
Briefly explain the rules of debits and credits as they relate to assets, liabilities, equity, revenue, and expenses. URGENT: NEED ANSWER ASAP PLEASE RESPOND WITH COPY AND PASTE, NOT ATTACHMENT USE ORIGINAL CONTENT NOT USED BEFORE ON CHEGG PLEASE ANSWER THROUGHLY TO ALL ANSWER TO BEST ABILITES ORIGINAL SOURCE NEVER USED BEFORE!!!
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.
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT