Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1- Write a program that takes filename, count, minimum and maximum values from t

ID: 3737061 • Letter: 1

Question

1- Write a program that takes filename, count, minimum and maximum values from the user and writes count number of random integers between minimum and maximum values to the file specified by the filename. The resulting file should have a random integer at each line, as shown below: Your program should expect command-line arguments as given below: ProgramName filename count min max Your program should generate different random integers at each execution. After that write a program that compares execution times for the sort algorithms: Quick-sort, Selection-sort, Shell-sort and Bubble-sort (improved version). Some of these algorithms are already available in the codes section of the Blackboard. For others, you can refer to books or internet.

In order to compare the algorithms, first create three files having 1,000, 10,000, and 100,000 random integers, using the program you have coded in the previous question. You can name the files as “1000”, “10000”, and “100000”, respectively.

Your program should read the contents of the files into an array one by one (i.e. one file at a time), and sort the array using the algorithms listed above. You will measure the time elapsed by each algorithm and print it on the screen, as shown below. You do NOT need to write the sorted content back to files.

Each sort algorithm should be coded as a function having identical signature with the other sort functions. For example, the algorithms can be declared as given below:

Since sorting algorithms change the input array, you should not pass the array that is read from the file directly to the sorting function, but make a copy first and pass that copy to the sorting function. That way the sorting algorithms will work on the same input array.

Your program should define a function pointer array, pointing to the sort functions defined. That way you can call all sort functions with a for loop. For instance, you can use the definitions given below:

void (*pFunctions[FUNC_COUNT])(int *,int) =
{quickSort, shellSort, bubbleSort, selectionSort};

Defining the names of the functions in an array may also be useful:

char *sFunctions[FUNC_COUNT] =
{"quickSort", "shellSort", "bubbleSort", "selectionSort"};

Your program should check whether all the algorithms create the same sorted array. For this you can take the result of one algorithm and compare it with the previous result – you do not need to compare all the results with each other. If one of the results does not agree with others, you should print an appropriate message on the console.

Explanation / Answer

Program 1

#include<iostream>

#include<cstdlib>

using namespace std;

int main(int argc, char** argv)

{

     string file=argv[0];

    int count=argv[1];

     int max=agv[2];

    int min=argv[3];

     ofstream outfile;

     outfile.open(file);

     int diff,r;

     diff=max-min;

    for(i=;i<count;i++)

   {

            r= rand() % min + diff;

               outfile << r << endl;

}

outfile.open(“1000.txt”);

for(i=1;i<1000;i++)

{

          r=rand();

          outfile<<r<<endl;

      

}

outfile.open(“10000.txt”);

for(i=1;i<10000;i++)

{

          r=rand();

          outfile<<r<<endl;

      

}

outfile.open(“100000.txt”);

for(i=1;i<100000;i++)

{

          r=rand();

          outfile<<r<<endl;

      

}

}

Program 2

#include<iostream>

#include<cstdlib>

using namespace std;

int main()

{

   

  

    if(file.is_open())

    {

        int myArray[1000];

        for(int i = 0; i < 1000; ++i)

        {

            file >> myArray[i];

        }

    }

    int newArray[1000]=myArray;

quicksort(newArray,0,1000);

    int sortq[1000]= newArray;

selectionSort(newArray,1000);

    int sorts[1000]= newArray;

shellSort(newArray,1000);

    int sortsh[1000]= newArray;

bubbleSort(newArray,1000);

    int sortb[1000]= newArray;

                                    

                                                                                           

}

void quicksort(int arr[1000],int low,int high)

{

           

            If(low<high)

            {

                        Pi=partion(arr,low,high);

                        quicksort(arr,low,pi-1);

                        quicksort(arr,pi+1,high);

            }

}

void selectionSort(int arr[1000],int n)

{

            Int i,j;

            for(i=0;i<n;i++)

            {

                        for(j=i+1;j<n;j++)

                        {

                                    If(arr[i]>arr[j])

                                    {

                                                arr[i]=arr[i]+arr[j];

                                                arr[j]=arr[i]-arr[j];

                                                arr[i]=arr[i]+arr[j];

                                    }

                        }

            }

}

void shellSort(int arr[1000], int n)

{

    for (int gap = n/2; gap > 0; gap /= 2)

    {

        for (int i = gap; i < n; i += 1)

        {

            int temp = arr[i];

             int j;           

            for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)

                arr[j] = arr[j - gap];

                         arr[j] = temp;

        }

    }

    return 0;

}

void bubbleSort(int arr[1000], int n)

{

   int i, j;

   for (i = 0; i < n-1; i++)     

       for (j = 0; j < n-i-1; j++)

           if (arr[j] > arr[j+1])

              swap(&arr[j], &arr[j+1]);

}