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

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 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...
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.
void phase05(){ int n = atoi(next_input()); int m = atoi(next_input()); int t = (hash % 30)...
void phase05(){ int n = atoi(next_input()); int m = atoi(next_input()); int t = (hash % 30) + 21; int s = 0; while(n>1){ if(n & 1){ n = (n<<2) - n + 1; } else { n = n >> 1; } if(s == t && m == t){ return; } s++; } failure("Seems you forgatz the essence of Collatz"); } Hash value: 2002296385 what input values should n, m have in order for the function phase05 to work?
Question 2: Calculate the time complexity function T(n) and express it in terms of big-Oh for...
Question 2: Calculate the time complexity function T(n) and express it in terms of big-Oh for the following code: Part a       (hint: make use of sum of geometric progression): for (int i=1; i <= n ; i = i*2) {           for ( j = 1 ; j <= i ; j ++)             {                       cout<<”*”;             } } Part b      (hint: make use of sum of square of a number sequence): for (int i=1; i <= n ; i...
For each call to the following method, indicate what console output is produced: public void mystery2(int...
For each call to the following method, indicate what console output is produced: public void mystery2(int n) { if (n <= 1) { System.out.print(n); } else { mystery2(n / 2); System.out.print(", " + n); } } mystery2(1); mystery2(4); mystery2(16); mystery2(30); mystery2(100);
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions:...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions: Print to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //code here } public static void (int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { for(int j = i; j < array.length; j++) { for(int k = j; k < array.length; k++)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT