Question

100n Run the attached program with the given values for N and R and record your results in the TT and T columns. The remainin

permute zip contains:

makefile:

DIR=${PWD}
ASST=$(notdir ${DIR})
MAINPROG=pdriver
ifneq (,$(findstring MinGW,$(PATH)))
DISTR=MinGW
EXE=.exe
LFLAGS=
else
DISTR=Unix
EXE=
LFLAGS=
endif
#
########################################################################
# Macro definitions for "standard" C and C++ compilations
#
CPPFLAGS=-g -D$(DISTR)
CFLAGS=-g
TARGET=$(MAINPROG)$(EXE)
CPPS=pdriver.cpp permute.cpp
LINK=g++ $(CPPFLAGS)
#
CC=gcc
CXX=g++
#
#
# In most cases, you should not change anything below this line.
#
# The following is "boilerplate" to set up the standard compilation
# commands:
#


OBJS=$(CPPS:%.cpp=%.o)
DEPENDENCIES = $(CPPS:%.cpp=%.d)

%.d: %.cpp
   touch $@

%.o: %.cpp
   $(CXX) $(CPPFLAGS) -MMD -o $@ -c $*.cpp

#
# Targets:
#
all: $(TARGET)

$(TARGET): $(OBJS)
   $(LINK) $(FLAGS) -o $(TARGET) $(OBJS) $(LFLAGS)

clean:
   -/bin/rm -f *.d *.o $(TARGET)

make.dep: $(DEPENDENCIES)
   -cat $(DEPENDENCIES) > $@

include make.dep

pdriver.cpp:

#include 
#include 
#include 
#include 

using namespace std;

void permute (int[], int);


int main(int argc, char** argv)
{
  if (argc != 3)
    {
      cerr << "When you run this program, you must supply two parameters.\n" 
           << "The first is the size of the array you want to permute.\n"
           << "The second is the number of trials you want to perform.\n"
           << "\n"
           << "For example, if you called this program pdriver, you\n"
           << "might invoke it as:\n"
           << "   pdriver 100 10 \n"
           << "to generate 10 random permutations of 100 elements each."
        << endl;
      return 1;
    }

  int N;
  int trials;

  {
    istringstream arg1 (argv[1]);
    arg1 >> N;

    istringstream arg2 (argv[2]);
    arg2 >> trials;
  }
  
  int *array = new int[N];

  srand(time(0));
  for (int t = 0; t < trials; t++)
    {
      permute (array, N);
      // for (int i = 0; i < N; ++i) cout << array[i] << ' '; cout << endl;
    }

  return 0;
}

permute.cpp:

#include 
#include 
#include 

using namespace std;

unsigned rnd(unsigned limit)
{
  return rand() % limit;
}

//  Generate a random permutation of the integers from
//  0 .. n-1, storing the results in array a.
//
void permute (int a[], int n)
{
  for (int i = 0; i < n; i++)
    {
      // Guess at a value to put into a[i]
      int guess = rnd(n);
      while (find(a, a+i, guess) != a+i)
        {
    // If it's one that we've already used, guess again.
         guess = rnd(n);
        }
      a[i] = guess;
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

So to complete the above problem, we need to find the time complexity of the permute function.

void permute (int a[], int n)
{
  for (int i = 0; i < n; i++)        //n times
    {
      // Guess at a value to put into a[i]
      int guess = rnd(n);           // Constant
      while (find(a, a+i, guess) != a+i) // Each find request will be proportional to n and finding the guess number which is not present will be of n complexity. So we will have n2 complexity here.
        {
    // If it's one that we've already used, guess again.
         guess = rnd(n);
        }
      a[i] = guess;
    }
}

So in total, the time complexity would be O(n3), Thus We can say that it would be proportional to \tfrac{T}{n^{3}}

So the solution is: Option E

Thanks:

Add a comment
Know the answer?
Add Answer to:
permute zip contains: makefile: DIR=${PWD} ASST=$(notdir ${DIR}) MAINPROG=pdriver ifneq (,$(findstring MinGW,$(PATH))) DISTR=MinGW EXE=.exe LFLAGS= else DISTR=Unix...
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 need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that...

    I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that array. It also measures the time that elapses during the calculation and returns it to the console along with the number of primes it finds. (a) Rewrite the program so that N threads are used to count the primes in the array. Each thread is...

  • Objectives The purpose of this lab is to help you become familiar with the development environment,...

    Objectives The purpose of this lab is to help you become familiar with the development environment, to reinforce the concepts covered in chapter 2, to reinforce the implementation of object oriented concepts in C++, and to reinforce the implementation of operator overloading in C++. Requirements Extend the implementation of the statistician class created in Lab 2 to overload operators +, *, and ==. The extended class definition is given in stats2.h. You need implement the three operator overloading functions in...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

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