Question

5. (10 points) For each C++ function below, give the tightes can determine. t asymptotic upper bound that you void mochalatte(int n) t for (int i O; i <n; i++)C cout <<iteration: << i <endl; Answer for (a): void nanaimobar(int n) for (int i = 1; i < 2*n ; i-2+i) { cout <<iteration: << i << endl; Answer for (b) void appletart (int n) f 0; i < 3*n ; i j*) cout くく iteration: << j くく endl; i+3) { j++) { for (int i for (int j 0; < 2*n ;

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

Answer is as follows:

The Asymptotic Upper bound is represend in bigO notation.

A)

The function has a for loop that work for i to n times mean it runs n times.

for other statements it is O(1)

So O(n) + O(1)

the largest part is O(n)

So the Big O notation for this is function is O(n).

B)

The function has a for loop that work for i to 2*n times mean it runs 2n times.

for other statements like cout function it is O(1)

So, O(1) + O(2n)

the largest part is O(2n)

According to rules O(n) = O(2n)

Proof:

if O(n) is subset of O(2n) => Let g be the function in O(n) . There are N and c>0 such that g(n) < c*n for all n>N. Thus g is in O(2n).

if O(2n) is subset of O(n) => Let g be the function in O(n) . There are N and c>0 such that g(n) < c*2n for all n>N. So g(n) < 2c * n for all n > N. Thus g is in O(n).

So the Big O notation for this is function is O(2n) as well as O(n).

C)

The function cotains the two for loops i.e. it works for n x n times or 3n x 2n times as state above n=2n=3n are equal.

So we the Big O notation for nested For loops are n2 .

For other statements it isO(1).

So the BigO notation for this is O(n2 )

if there is any query please ask in comments...........

Add a comment
Know the answer?
Add Answer to:
For each C++ function below, give the tightest can asymptotic upper bound that you can determine....
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
  • I have written this function in C++. A string of integers is passed in as dString...

    I have written this function in C++. A string of integers is passed in as dString and an integer is passed in as count. each dString integer is then indexed and passed into strTolnt integer array. each strTolnt integer is then passed through an equation and stored in temp array. I have printed each index in the array and the correct values are displayed, but when I print the entire array the value is displayed as hex. temp array is...

  • Problem 1. Select the running time of each function. void print_array (int* A, int n) for...

    Problem 1. Select the running time of each function. void print_array (int* A, int n) for (int í 0; i < n; ++i) cout << A[i] << endl; void print_array pairs (int* A, int n) for (inti 0; i < n; ++i) for (int j 0; j < n; ++j) cout << Ai] ALj]< endl; void print_array_start(int* A, int n) for (int i 0; i < 100 ; ++i) cout << A[i] << endl; void print_array_alt (int* A, int n)...

  • C++ Object Oriented assignment Can you please check the program written below if it has appropriately...

    C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...

  • 4. [10pts Find a tight asymptotic upper bound for the function T(n) defined by the recurence...

    4. [10pts Find a tight asymptotic upper bound for the function T(n) defined by the recurence relation T(2)-2 T(n) = T(n/2) + Tuv )) + n Assume that n is a power of 2

  • (10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity...

    (10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity i) for (int i = 0; ǐ < n/2; i++) for (int j -0: ni j++) count++ i) for (int í = 0; i < n; i++) for (int ni j0 - for (int k j k ni kt+) count++ İİİ) for (int í ー 0; i < n; i++) for(int j = n; j > 0; j--) for (int k = 0; k...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • 1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ)....

    1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

  • How can I write this code (posted below) using vectors instead of arrays? This is the...

    How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

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