Question: Provide the missing code to implement the Merge() algorithm where it says Student provides missing code... (C Programming) Please post the missing code into Problem35.c

Provide the missing code to implement the Merge() algorithm where it says "Student provides missing code..." (C Programming)

Please post the missing code into Problem35.c and provide screenshot which should look similar to the sample one provided.

All source files located below.

Provide the missing code to implement the Merge() algorithm where it says

"Student provides missing code..." (C Programming) Please post the missing code into

Problem35.c and provide screenshot which should look similar to the sample one

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

// Dr. Art Hanna

// Problem #35

// Problem35.c

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

#include

#include

#include

#include

#include

#include "..\Random.h"

#define TRACE_MERGESORT

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

int main()

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

{

void DoMergeSort(FILE *DATA,const int n);

void DisplayFile(FILE *DATA,const int n);

void DoBinarySearch(const int datum,FILE *AFILE,const int L,const int R,

bool *found,int *record,int *numberCompares);

int n,LB,UB;

int i;

FILE *AFILE;

printf("n? "); scanf("%d",&n);

printf("LB? "); scanf("%d",&LB);

printf("UB? "); scanf("%d",&UB);

// Build binary file contents with n randomly-chosen integers from the interval [ LB,UB ]

SetRandomSeed();

AFILE = fopen("AFile.dat","w+b");

assert( AFILE != NULL );

for (i = 0; i

{

int datum = RandomInteger(LB,UB);

fwrite(&datum,sizeof(int),1,AFILE);

}

printf("Unsorted: "); DisplayFile(AFILE,n);

DoMergeSort(AFILE,n);

printf(" Sorted: "); DisplayFile(AFILE,n);

// Use Binary Search algorithm to search sorted binary file for every datum in [ LB,UB ]

{

int datum;

for (datum = LB; datum

{

bool found;

int record,numberCompares;

numberCompares = 0;

DoBinarySearch(datum,AFILE,0,n-1,&found,&record,&numberCompares);

if ( found )

printf("%5d compares: %5d found at record %5d ",numberCompares,datum,record);

else

printf("%5d compares: %5d not found ",numberCompares,datum);

}

}

fclose(AFILE);

system("PAUSE");

return( 0 );

}

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

void DoMergeSort(FILE *DATA,const int n)

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

{

void Merge(FILE *BFILE,const int p,FILE *CFILE,const int q,FILE *AFILE);

void CopyFile(FILE *SFILE,const int L,const int R,FILE *TFILE);

static int suffix = 0;

#if defined(TRACE_MERGESORT)

printf("Sort %5d records in FILE *%p ",n,DATA);

#endif

if ( n > 1 )

{

char BFileName[80+1];

FILE *BFILE;

char CFileName[80+1];

FILE *CFILE;

int s = (int) floor(n/2);

suffix++;

sprintf(BFileName,"BFile%d.dat",suffix);

BFILE = fopen(BFileName,"w+b");

assert( BFILE != NULL );

CopyFile(DATA,0,s-1,BFILE);

sprintf(CFileName,"CFile%d.dat",suffix);

CFILE = fopen(CFileName,"w+b");

assert( CFILE != NULL );

CopyFile(DATA,s,n-1,CFILE);

#ifdef TRACE_MERGESORT

printf("%s (FILE *%p) contains %5d records ",BFileName,BFILE,(s-1)-0+1);

printf("%s (FILE *%p) contains %5d records ",CFileName,CFILE,(n-1)-s+1);

#endif

DoMergeSort(BFILE,(s-1)-0+1);

DoMergeSort(CFILE,(n-1)-s+1);

Merge(BFILE,(s-1)-0+1,CFILE,(n-1)-s+1,DATA);

fclose(BFILE); remove(BFileName);

fclose(CFILE); remove(CFileName);

}

}

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

void CopyFile(FILE *SFILE,const int L,const int R,FILE *TFILE)

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

{

void SET(FILE *DATA,const int record,const int datum);

int GET(FILE *DATA,const int record);

int record;

for (record = L; record

SET(TFILE,record-L,GET(SFILE,record));

}

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

void Merge(FILE *BFILE,const int p,FILE *CFILE,const int q,FILE *AFILE)

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

{

void SET(FILE *DATA,const int record,const int datum);

int GET(FILE *DATA,const int record);

Student provides missing code to implement the Merge() algorithm

found in Section 5.1 on page 72 the text book.

}

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

void DisplayFile(FILE *DATA,const int n)

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

{

int i;

for (i = 0; i

printf("%5d",GET(DATA,i));

printf(" ");

}

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

void SET(FILE *DATA,const int record,const int datum)

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

{

fseek(DATA,record*sizeof(int),SEEK_SET);

fwrite(&datum,sizeof(int),1,DATA);

}

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

int GET(FILE *DATA,const int record)

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

{

int datum;

fseek(DATA,record*sizeof(int),SEEK_SET);

fread(&datum,sizeof(int),1,DATA);

return( datum );

}

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

void DoBinarySearch(const int datum,FILE *DATA,const int L,const int R,

bool *found,int *record,int *numberCompares)

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

{

/*

Do binary search of records L through R, inclusive, searching for datum

in the binary file DATA. Return *found as true when datum is found, otherwise

return false. When datum is found, return *record [ L,R ], otherwise

*record is meaningless. Count three-way compares of datum with *numberCompares.

*/

if ( L > R )

*found = false;

else

{

*record = (L+R)/2;

*numberCompares += 1;

if ( datum

DoBinarySearch(datum,DATA,L,*record-1,found,record,numberCompares);

else if ( datum == GET(DATA,*record) )

*found = true;

else // if ( datum > GET(DATA,*record) )

DoBinarySearch(datum,DATA,*record+1,R,found,record,numberCompares);

}

}

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

// Dr. Art Hanna

// 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))] );

}

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

// Dr. Art Hanna

// 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 three integers n, LB, and UB. Randomly-choose n integers from the interval [ LB, UB1 and write them to the binary data file AFile.dat. Use the function DoMergeSort0 (prototyped below) to sort AFile.dat. void DoMergeSort (FILE DATA, const int, n); After the sort is complete, use the function DoBinarySearch0 void DoBinarySearch (const int datum, FILE AEILE, cons t int L,const int R bool *found, int *record,int numberCompa res); to search the sorted AFile.dat for every integer in the interval [ the format LB,UB 1 and report the results using 1111111111222222233333333333334444 1234567890123456789012345678901234567890123 XXXXX compares: XXXXX found at record XXXXX XXXxx compares: XXXXX not found

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!