Average-case analysis for the permute function

Assignment Help C/C++ Programming
Reference no: EM132432197

Use the copy-and-paste method to give an average-case analysis for the permute function in permute.cpp.

  • Assume that the random number generator used in the algorithm produces truly random numbers, i.e, for any integer j in the range 0 .. RAND_MAX, any given call of rand() has a probability 1/RAND_MAX of returning j.
  • The rand() function runs in O(1) time (worst-case and average).
  • Show all work!
  • Remember that you are doing average case analysis.
#include <cstdlib>
#include <algorithm>
#include <vector>

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)
{
  // Array of booleans: used[k] is true if we have already
  // put k somewhere into the array a
  bool* used = new bool[n];
  fill (used, used+n, false);
  for (int i = 0; i < n; i++)
    {
      // Guess at a value to put into a[i]
      a[i] = rnd(n);
      while (used[a[i]])
        {
	 // If it's one that we've already used, guess again.
         a[i] = rnd(n);
        }
      used[a[i]] = true;
    }
  delete [] used;
}

Reference no: EM132432197

Questions Cloud

Calculate sales to unaffiliated customers within a country : Taft Corporation operates primarily in the United States. Calculate sales to unaffiliated customers within a country and as a percent of the consolidated sales
Description of social norms : Description of social norms. "Why do you think this particular theory of social norms fits your example?"what information is missing from our understanding
Functions declaration mandatory : Why are the functions declaration mandatory in C++ and not in C?
Whats major challenge of stemming tide of political violence : What do you think would be the most appropriate responses from counter-protestors to get their point across without escalation? Is this even possible
Average-case analysis for the permute function : Use the copy-and-paste method to give an average-case analysis for the permute function in permute.cpp.
Receiving a beneficial card : The version of the game will imagine only a single suit of cards, so 13 unique cards, {2,3,4,5,6,7,8,9,10,J,Q,K,A}. Given two cards from the set
Add the results of the array together : How do you add a function in an array to add the results of the array together? Also how do you alter the sort part of the function to sort from a different cat
What changed for when had to recalculate the budget : What does it mean to you that your job may not monetarily provide you with everything you need or want? How will you resolve this?
Present the adjustment transaction in a journal : Present the adjustment transaction in a journal. Indicate the type of adjustment that is carried out and present the daily transaction

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Make a class employee

Make a class EMPLOYEE with a name and salary. Make a class MANAGER inherit from EMPLOYEE. Add an instance field, named DEPARTMENT

  Print name on the screen

Write a C++ program to prompt the user to input her/his name and print name on the screen.

  Write a linux program that will calculate temperature

Write a Linux program that will calculate temperature in degrees F, given temperature in degrees C, where C is 34.5. (The formula is.. F = (9/5) * C + 32.0).

  The size ofthe character array holding your filename

How to express for code "THE SIZE OF THE CHARACTER ARRAY HOLDING YOUR FILENAME >=1024 (or use strings), we need to use long file names when grading your code."

  Program user to enter student names and final grades

Write a program that would allow a user to enter student names and Final grades (e.g. A,B,C,D,F) from their courses. You do not know how many students need to be entered. You also do not know how many courses each of the students completed.

  Linear programming feasible region

Enumerate all points in the linear programming feasible region in which both x1 and x2 are integers, and show that the feasible solution obtained in (c) is not optimal and that in fact the optimal integer is not obtained by any form of rounding.

  Firm after-tax cost of debt on the? bond

The company is in a 30 percent tax bracket. What will be the? firm's after-tax cost of debt on the? bond?

  Wap to calculate the area of the circle

Write a C++ program that prompts the user for the radius of a circle, then calls inline function circle Area to calculate the area of that circle.

  Write a program with a while loop to print 1 to n in square

Write a program with a while loop to print 1 to N in square brackets. N is an integer input from the user. (i) Write the same program using a for-loop

  What is difference between a reference vs a static variable

What is the difference between a Reference vs a Static Variable? When would you use either and why? Why and When would you use a VOID function but use a Refere

  Write a function named append that accepts three arguments

Write a function named append that accepts three arguments. The first two arguments passed to append are c-strings to be appended (the second c-string is appended to the first). The third argument passed should be the size of the character array ..

  Write a precursor to a menu-driven program

Write a precursor to a menu-driven program. The program should display a menu offering four choices, each labeled with a letter.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd