Question

1. Exercise 3.7 of the textbook. An algorithm prints the following pattern: * * * *...

1. Exercise 3.7 of the textbook.

An algorithm prints the following pattern:

*

* *

* * *

* * * *

* * * * *

A. What are the basic operations performed by the algorithm that you would count towards its running time?

B. Count the number of these basic operations for the specific output shown above.

C. The number of lines printed in the preceding pattern is 5. Assume that the algorithm can extend this pattern for any number of lines (line number i has i stars). If the number of lines, n is the input to the algorithm, how many basic operations are performed as a function of n?

D. Write your answer to the above question as a big O order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A. The very basic operations performed by the algorithm that one would count towards its running time is NUMBER OF STARS PRINTED , NUMBER OF BLANKSPACE (BETWEEN EACH STAR) , NEWLINE . These all basic operations can be easily controlled by any loop (for loop is most appropiate).

B. there are 15 stars , 10 blankspace and 5 newlines .

c. If there is n as the number of lines given input then , Number of starts will be n.(n+1)/2 , it is nothing but sum of n numbers given as input ,

Numbers of blankspace will be n.(n-1)/2 , it is the sum of (n-1) number therfore each line will have one less blankspace as the line number(i.e line no 2 will have 1 blankspace and so on) .

Number of newline will be n ,i.e the number given as input .

D. O(n2) this is generally used for nested loop and hence here also nested loop is used therefore this will be the bigO order for the algorithm

As a BIg O order the above algorithm will be having O(n2) which is directly proportional to the size of the given input.

Add a comment
Know the answer?
Add Answer to:
1. Exercise 3.7 of the textbook. An algorithm prints the following pattern: * * * *...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT