Question

In: Computer Science

What is time Complexity each of the following function? 1- void function(int n) {             for (int...

What is time Complexity each of the following function?

1-

void function(int n)

{

            for (int i=n/2; i<=n; i++)  

                        for (int j=1; j<=n/2; j++)

                                    for (int k=1; k<=n; k = k * 2)

                                                print ”Hello”;

}

2-

void function(int n)

{

            for (int i=n/2; i<=n; i++)  

                        for (int j=1; j<=n; j = 2 * j)

                                    for (int k=1; k<=n; k = k * 2)

                                                print ”Hello”;

}

3-

void function(int n)

{

            for (int i=1; i<=n; i++)  

                        for (int j=1; j<=n; j = j + i)  

                                                print ”Hello”;

}

4-

Int n=22^k

for (int i=1; i<=n; i++)

{

             J=2

            While(j<=n)

            {

                        j=j2

                        print ”Hello”;

            }

}

5=

If n>=2

While(n>1)

{

            n=n/2;

            print ”Hello”;

}

Solutions

Expert Solution

1-

void function(int n)

{

            for (int i=n/2; i<=n; i++)  

                        for (int j=1; j<=n/2; j++)

                                    for (int k=1; k<=n; k = k * 2)

                                                print ”Hello”;

}

ANSWER:n3

---------------------------------------------------------------------------------------------------------------------------

2-

void function(int n)

{

            for (int i=n/2; i<=n; i++)  

                        for (int j=1; j<=n; j = 2 * j)

                                    for (int k=1; k<=n; k = k * 2)

                                                print ”Hello”;

}

ANSWER:n3

---------------------------------------------------------------------------------------------------------------------------------------------------

3-

void function(int n)

{

            for (int i=1; i<=n; i++)  

                        for (int j=1; j<=n; j = j + i)  

                                                print ”Hello”;

}

ANSWER:n2

---------------------------------------------------------------------------------------------------------------------------------------------------

4-

Int n=22^k

for (int i=1; i<=n; i++)

{

             J=2

            While(j<=n)

            {

                        j=j2

                        print ”Hello”;

            }

}

ANSWER:n2

---------------------------------------------------------------------------------------------------------------------------------------------------

5=

If n>=2

While(n>1)

{

            n=n/2;

            print ”Hello”;

}

ANSWER:n

---------------------------------------------------------------------------------------------------------------------------------------------------


Related Solutions

) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int n, char from_rod,                     char to_rod, char aux_rod) {     if (n == 1)     {         cout << "Move disk 1 from " << from_rod << " to " << to_rod<<endl;         return;     }     towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);     cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;     towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); }
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT