Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming only. Please fill in the missing code in the three designated areas in source file Problem51.c that says Student provides missing code...... below.

C Programming only.

Please fill in the missing code in the three designated areas in source file Problem51.c that says "Student provides missing code......" below.

There are three source files below which are Problem 51.c, Random.c, and Random.h..

Please also provide screenshot of the compiled output once complete. I have provided a sample one which should give an idea on how it should look.

image text in transcribed

image text in transcribed

//-------------------------------------------------------------

// Problem #51

// Problem51.c

//-------------------------------------------------------------

#include

#include

#include

#include "..\Random.h"

//-------------------------------------------------------------

int main()

//-------------------------------------------------------------

{

/*

minimum maximum average n!

-------------------- -------------------- ------------- --------------------

XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XXXXXXXXXX.XX XXXXXXXXXXXXXXXXXXXX

*/

void RunOneTrial(unsigned long long int *count,int n,

bool (*EQ)(int xs1[],int xs2[],int n));

bool EQ(int xs1[],int xs2[],int n);

signed long long int Factorial(int n);

int T,n;

SetRandomSeed();

printf("T n? ");

while ( scanf("%d%d",&T,&n) != EOF )

{

int i;

unsigned long long count,minCount,maxCount,sumCount;

RunOneTrial(&count,n,EQ);

minCount = count;

maxCount = count;

sumCount = count;

for (i = 2; i

{

RunOneTrial(&count,n,EQ);

if ( count

if ( count > maxCount ) maxCount = count;

sumCount += count;

}

printf(" minimum maximum average n! ");

printf("-------------------- -------------------- ------------- -------------------- ");

printf("%20lld %20lld %13.2f %20llu ",minCount,maxCount,(double) sumCount/T,Factorial(n));

printf(" T n? ");

}

system("PAUSE");

return( 0 );

}

//-------------------------------------------------------------

bool EQ(int xs1[],int xs2[],int n)

//-------------------------------------------------------------

{

Student provides missing code #1

}

//-------------------------------------------------------------

long long int Factorial(int n)

//-------------------------------------------------------------

{

Student provides missing code #2

}

//-------------------------------------------------------------

void RunOneTrial(unsigned long long int *count,int n,

bool (*EQ)(int xs1[],int xs2[],int n))

//-------------------------------------------------------------

{

void RandomlyFillWithoutReplacement(int xs[],int n);

int *xs1 = (int *) malloc(sizeof(int)*(n+1));

int *xs2;

xs2 = (int *) malloc(sizeof(int)*(n+1));

*count = 0;

do

{

RandomlyFillWithoutReplacement(xs1,n);

RandomlyFillWithoutReplacement(xs2,n);

(*count)++;

} while ( !EQ(xs1,xs2,n) );

free(xs1);

free(xs2);

}

//-------------------------------------------------------------

void RandomlyFillWithoutReplacement(int xs[],int n)

//-------------------------------------------------------------

{

bool XInXs(int xs[],int n,int x);

int i;

for(i = 1; i

{

int x;

do

{

x = RandomInteger(1,n);

} while ( XInXs(xs,i-1,x) );

xs[i] = x;

}

}

//-------------------------------------------------------------

bool XInXs(int xs[],int n,int x)

//-------------------------------------------------------------

{

Student provides missing code #3

}

//--------------------------------------------------

// Random.c

//--------------------------------------------------

#include

#include

#include

#include

#include ".\Random.h"

//--------------------------------------------------

void SetRandomSeed(void)

//--------------------------------------------------

{

srand(time(NULL));

}

//--------------------------------------------------

int RandomInteger(int LB,int UB)

//--------------------------------------------------

{

return( (int) (RandomReal()*(UB-LB+1)) + LB );

}

//--------------------------------------------------

double RandomReal()

//--------------------------------------------------

{

int R;

do

R = rand();

while ( R == RAND_MAX );

return( (double) R/RAND_MAX );

}

//--------------------------------------------------

bool RandomBoolean(double bias)

//--------------------------------------------------

{

return( RandomReal()

}

//--------------------------------------------------

char RandomCharacter(char characters[],int size)

//--------------------------------------------------

{

return( characters[RandomInteger(0,(size-1))] );

}

//--------------------------------------------------

// Random.h

//--------------------------------------------------

#ifndef RANDOM_H

#define RANDOM_H

#include

// initialize the "seed" used by the "rand()" function in

void SetRandomSeed(void);

// return uniformly, randomly chosen integer from [ LB,UB ]

int RandomInteger(int LB,int UB);

// return uniformly, randomly real number chosen from [ 0.0,1.0 )

double RandomReal(void);

// return randomly-chosen boolean with bias from { false,true }

bool RandomBoolean(double bias);

// return uniformly, randomly-chosen character from characters[i], i in [ 0,(size-1) ]

char RandomCharacter(char characters[],int size);

#endif

Problem Input 2 integers, (1) T that specifies the number of times the RunOneTrial) executes before results are reported; and (2) n that specifies the size of the integer vectors (a 1-dimensional arrays), xs1[] and xs2 1. Execute the RunOneTrial) algorithm (shown below) T times while collecting 3 statistics, minimum-of-count, maximum-of-count, and the average-of-count. Simulation results must be reported using this format 12345678901234567890123456789012345678901234567890123456789012345678901234567890 minimum maximum average Algorithm RunOneTrial (OUT count,IN n) // Count the number of times xs1 [] and xs2 [] are re-filled with integers randomly-chosen without replacement from 1,n until (xsl-xs2). 1. dynamically-allocate an (n+1) -sized vector of integers named xs1 (ignore xs1 [0] and xs 2 (0]) 2. dynamically-allocate an (n+1) -sized vector of integers named xs2 3. set count to 0 4. LOOF 5. fill xsllij i 1,2,n with integers randomly-chosen without replacement from [ 1, n 6. fill xs2 [i] 1, 2, , n with integers randomly-chosen without replacement from [ 1, n ] 7.increment count 8. UNTIL ( xsl = xs2 ) 9. dynamically-deallocate vectors xs1 and xs2 Note There are n! permutations of the contents of xs1 [] and of xs2 [], there are (n!)2 distinct combinations of the permutations of xs1[] and xs2].n! of these (n!)2 combinations are equal, so theoretically, the "average" should compute to be n! for a large enough value of T. For example, for n - 3, the 3!- 3*2*1 6 permutations are 123, 132, 213, 231, 312, 321 and "average" should be about 6.0 for a large enough value of T

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions