Question

For each implementation, answer the following question: Suppose I run this with a particular value of...

For each implementation, answer the following question: Suppose I run this with a particular value of n, and it takes 10 seconds. Suppose I run it again, but I double the value of n. How long should I expect it to run?

Implementation 1
void Q4(vector <int> &v, int n) {
    int i;
    v.clear();
    for (i = 0; i < n; i++) v.push_back(i);
    for (i = 0; i < n; i++) v.push_back(i);
    for (i = 0; i < n; i++) v.push_back(i);
    for (i = 0; i < n; i++) v.push_back(i);
}

Implementation 2
void Q4(vector <int> &v, int n) {
    int i;
    v.clear();
    for (i = 0; i < n; i++) v.push_back(i);
  
Implementation 3
int Q4(int n) {
    if(n == 0) return 1;
    return 1 + Q4(n-1);
}

Implementation 4
int Q4(int n) {
    if(n == 0) return 1;
    return Q4(n-1) + Q4(n-1);
  
Implementation 5
void Q4(vector <int> &v, int n) {
    int i, j;
    v.clear();
    for (i = 0; i < n; i++) {
        for (j = 0; j < i; j++) v.push_back(i);
    }
    for (i = 0; i < n; i++) {
        for (j = 1; j < n; j *= 2) v.push_back(i+j);
    }
}

Choose your answers from the following multiple choice and explain why: Possible Answers: 1.) 20 seconds 2.) 10 seconds 3.) >60 seconds 4.) 44 seconds 5.) 40 seconds

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
For each implementation, answer the following question: Suppose I run this with a particular value of...
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
  • For each implementation, answer the following question: Suppose I run this with a particular value of...

    For each implementation, answer the following question: Suppose I run this with a particular value of n, and it takes 10 seconds. Suppose I run it again, but I double the value of n. How long should I expect it to run? Implementation 1 void Q4(vector <int> &v, int n) {     int i;     v.clear();     for (i = 0; i < n; i++) v.push_back(i);     for (i = 0; i < n; i++) v.push_back(i);     for (i =...

  • Provide a "big oh" run-time analysis for each of the following. When a value of “n”...

    Provide a "big oh" run-time analysis for each of the following. When a value of “n” is used, it is the size of the input. 4.) void problem 40 cin n min max for (int i min i n, i++) for (int j- 1: j< max, j++) tota while (total n tota total 2 total 5.) void problem 50 cin n; for (int i 0: i n, i++) for (int j 0; j i2; j++) for (int k 0; k...

  • hey I need help finishing this simplex program. public class Main {       /**       *...

    hey I need help finishing this simplex program. public class Main {       /**       * @param args       */       public static void main(String[] args) {             // TODO Auto-generated method stub             //FAIRE LES TODO dans Simplex             test1();test2();                   }       private static void test1() {             double[][] A = {                         { -1, 1, 0 },                         { 1, 4, 0 },                         { 2, 1, 0 },                         { 3, -4, 0 },                        ...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • I am having trouble understanding how this code is able to use the contents of the...

    I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide comments in the main code to describe what is happening? (especially on the bool isNumber) THE MAIN CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) {    if(!isdigit (s[0]))    {        if(s[0] != '-')        return false;               else if(s.length() == 1)        return false;...

  • I am having trouble understanding how this code is able to use the contents of the...

    I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide brief comments in the top code to show what is happening? THE CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) {    if(!isdigit (s[0]))    {        if(s[0] != '-')        return false;               else if(s.length() == 1)        return false;    }       for...

  • Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

    Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...

  • Question 4 1 pts Which of the following is an edge in this graph? (select all...

    Question 4 1 pts Which of the following is an edge in this graph? (select all that apply) 0 (4,5) O (3,5) O (2,4) (0,3) (3,1) (1,4) Question 5 1 pts Given the following vector v, what will the final value of v be? std::vector<int> y = {6,4,81,99,17}; v.pop_back(); v.pop_back(); v.push_back(32); v.pop_back(); v.push_back(12); v.pop_back() v.push_back(18); v.push_back(21); v.pop_back() O V = {17,32,18,21} O v = {21,18,12,32} O V = {32,12,18,21} O v = {6,4,81,18} Question 6 1 pts Given the following...

  • Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n)...

    Example program #include <string> #include <iostream> #include <cmath> #include <vector> using namespace std; vector<int> factor(int n) {     vector <int> v1;     // Print the number of 2s that divide n     while (n%2 == 0)     {         printf("%d ", 2);         n = n/2;         v1.push_back(2);     }     // n must be odd at this point. So we can skip     // one element (Note i = i +2)     for (int i = 3; i <=...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

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