1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Volgvan
3 years ago
8

Consider the following algorithm: ```c++ Algorithm Mystery(n) { // Input:A nonnegative integer n S = 0; for i = 1 to n do { S =

S + i * i; } return S } ``` 1. What does this algorithm compute? 2. What is its basic operation? 3. How many times is the basic operation executed? 4. What is the efficiency class of this algorithm? 5. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try to prove that, in fact, it cannot be done.
Computers and Technology
1 answer:
jasenka [17]3 years ago
3 0

Answer:

See explanation

Explanation:

First let us find what this algorithm compute:

Algorithm Mystery(n) {

        S = 0;

        for i = 1 to n do

             { S = S + i * i; }

         return S }

1)

Let suppose n = 3

S = 0

The algorithm has a for loop that has a loop variable i initialized to 1

At first iteration:

i = 1

S = S + i * i;

   = 0 + 1 * 1

S = 1

At second iteration:

i = 2

S = 1

S = S + 2 * 2;

   = 1 + 2 * 2

   = 1 + 4

S = 5

At third iteration:

i = 3

S = 5

S = S + 3 * 3;

   = 5 + 3 * 3

   = 5 + 9

S = 14

Now the loop breaks at i=4 because loop iterates up to n and n =3

So from above example it is clear that the algorithm computes the sum of squares of numbers from 1 to n. Or you can say it compute the sum of first n squares.  Let us represent this statement in mathematical form:

∑\left \ {{n} \atop {i=1}} \right. i²

2)

From above example we can see that the basic operation is multiplication. At every iteration loop variable i is multiplied by itself from 1 to n or till n times. However we can also say that addition is the basic operation because at each iteration the value of S is added to the square of i. So it takes the same amount of time.

3)

In the for loop, the basic operation executes once. We can say that at each iteration of the loop, multiplication is performed once. Suppose A(n) represents the number of times basic operation executes then,

A(n) = ∑\left \ {{n} \atop {i=1}} \right. 1 = n

4)

Since for loop executes once for each of the numbers from 1 to n, so this shows that for loop executes for n times. The basic operation in best, worst or average case runs n times. Hence the running time of Θ(n) . We can say that A(n) = n ∈ Θ(n)  

If b is the number of bits needed to  represent n then

b = log₂n + 1

b = log₂n

So

n ≈ 2^{b}

A(n) ≈ 2^{b} ≈ Θ( 2^{b} )  

5)

One solution is to calculate sum of squares without using Θ(n) algorithm.  So the efficient algorithm that takes less time than the previous algorithm is:

Algorithm Mystery(n) {

        S = 0;

        S = (n * ( n + 1 ) (2n + 1) ) / 6 ;

        return S}

Now the sum can be calculated in Θ(1)  times. This is because regardless of the size and number of operands/operations, the time of arithmetic operation stays the same. Now lets find out how this algorithm works for

n = 3

S = (n * ( n + 1 ) (2n + 1) ) / 6 ;

 = (3 * ( 3 + 1 ) * (2(3) + 1) ) / 6

 = (3 * (4) * (6+1)) / 6

= (3 * (4) * (7)) / 6

= (3 * 4 * 7) / 6

= 84 / 6

S = 14

You might be interested in
Type a statement using srand() to seed random number generation using variable seedVal. Then type two statements using rand() to
Alik [6]

Answer:

#include<stdio.h>

#include<stdlib.h>

int main(void){

int seedval;

scanf ("%d", &seedval);

srand(seedval);

printf("%d\n", rand()%10);

printf("%d\n", rand()%10);

 return 0;

}

Explanation:

The given code is poorly formatted. So, I picked what is usable from the code to write the following lines of code:

#include<stdio.h>

#include<stdlib.h>

int main(void){

This line declares seedval as integer

int seedval;

This line gets user input for seedval

scanf ("%d", &seedval);

This line calls the srand function to generate random numbers

srand(seedval);

This prints a random number between 0 and 9

printf("%d\n", rand()%10);

This also prints a random number between 0 and 9

printf("%d\n", rand()%10);

 return 0;

}

8 0
3 years ago
X=10 ;
NNADVOKAT [17]

Answer:

The answer is x=10; y=15;

There is no change in the values of the variables x and y.

Explanation:

The integer variables x and y are initialized to 10 and 15 respectively as given.

x=10 ;

y=15 ;

The values of variables x and y are passed as parameters to another method, as shown.

mystery (x, y);

The mystery method given in the question is used to assign the same value to both the variables. The value of both the variables hold the value of x, i.e., both the variables hold the value of 10.

void Mystery ( int one , int two )  

{  

// temporary

int temp;  

// temp initialized to value of variable one which contains the value of variable x

// temp = x or temp = 10

temp = one;

// one is initialized to the value of itself or value of x

// one = x or one = 10

one = one ;

// temp is assigned value of variable x through variable one

// temp = 10

// y is also assigned value of temp variable

// y = 10

two = temp;  

}

As shown in the above steps, the mystery method is used to assign same values to variables one and two.

The values of original variables x and y are not affected since x and y are not utilized or called inside the mystery method.

This type of passing of parameters to a method can be described as passing a copy of the values of the parameters to the method.

Value of variable x is passed into variable one while value of variable y is passed into variable two.

Hence, values of variables one and two can be changed and modified without affecting the values of original variables x and y.

3 0
3 years ago
What problems does the swim coach have? Use details from the story to support your answer.
kobusy [5.1K]
Swimming questions that you need to know how to do
5 0
3 years ago
Compare and contrast how the roles of women and men in society are changing​
Tems11 [23]

Answer:

Society is run with the everyday changing and evolving roles of men and women. Taking instances from the past, it can be very well said that the society has seen an evolution in the changing roles of men and women.

In ancient times, men were dominant. Men were only meant to earn and provide for their family and women were only meant to cook, clean their house and raise the kids and looking after their domestic animals. But with the advancement of time, women began to grow in the industry and business field. Slowly and gradually taking baby steps, women have managed to climb a mountain facing each and every hurdle of stereotypes and rituals to prove their worth. Now it can be very well said that women are no less than men and have proper significance in growth of society ethically and economically as well.

4 0
3 years ago
When you insert an object in a document, word always inserts it as a floating object. true or false.?
wolverine [178]
Not always. If you have text wrapping on, it will snap to the text.
3 0
3 years ago
Other questions:
  • What does the e in email stand for
    8·2 answers
  • The process of providing and denying access to objects is called:
    5·1 answer
  • What is the importance of personal computers in connecting to the internet ?
    13·2 answers
  • There are three main components to economic growth. Which of the following is NOT a component of economic growth?
    7·1 answer
  • On the server side, the database environment must be properly configured to respond to clients' requests in the fastest way poss
    12·1 answer
  • Https://intro.edhesive.com/courses/17288/assignments/2313298?module_item_id=4864185
    9·1 answer
  • Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicatin
    6·1 answer
  • B. Directions: Fill in the blanks with the correct answer.
    13·1 answer
  • can people be nice and just r;p with me once im getting sad because everyone is rude about it im not a creep im really nice and
    6·1 answer
  • When adopting and implementing a software as a service (saas) platform such as salesforce for your business, which responsibilit
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!