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

Using C++ You should start with your solution to Activity #2 and rename the file

ID: 3589856 • Letter: U

Question

Using C++

You should start with your solution to Activity #2 and rename the file assignment1 You need to modify the program to sort an array of words (strings) in ascending order.

CODE:

*************************************

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

// function that swaps 2 numbers


void swapper(int& a, int& b);
int main()

{

// declaring variables

int i, j;

cout<<"Enter the integers: ";

const int n = 10000;

int fiveInts[n];

srand(time(NULL));

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

fiveInts[i] = rand();

// store time in milliseconds

int start = time(0) * 10000;

for (int i = 0; i < 10000; i++)
{
cout << fiveInts[i] << endl;
}

// Bubble sort technique to sort elements

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

{

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

{

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

{

swapper(fiveInts[j],fiveInts[j+1]);

}

}

}

//for (int i = 0; i < 100; i++)
//{
//cout << fiveInts[i] << endl;
//}

// store time in milliseconds

clock_t start_t = clock();

// printing output

//cout<<"The array of integers sorted in ascending order is: " ;

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

{

cout<<fiveInts[i]<<" " << endl;

}


clock_t stop_t = clock();
cout << "the time is" <<(float)(stop_t - start_t)/CLOCKS_PER_SEC;

}

void swapper(int& a, int& b)

{

int temp = a;

a = b;

b = temp;

}

*************************************

Next, you need to modify the program to input a file_name from the user. Once the file file_name is opened your program should read in the first 1000 words (strings) in the file into an array and sort it into ascending order. If the file contains less than 1000 words then your program should add the words into the array and fill the remainder of the array with empty strings before sorting.

Your program will use a swapNeeded function whenever you want to determine if two strings need to be switched during the array sorting. You need to overload the swapNeeded function with the following two new versions:

boolswapNeeded(string&first,string&second)boolswapNeeded(stringfirst,stringsecond)

The swapNeeded function will return true if the second is less than the first and false otherwise.

Use the “time” command in Linux to detect how long it takes to sort 1000 strings using your program with the call-by-value swapNeeded in comparison to the swapNeeded with call-by-reference parameters. Include both time results as comments in your program.

Explanation / Answer

NOTE: Use time functions provided in the problem to get time of execution whenever necessary.

using namespace std;

int main (int argc, char *argv[])

{

            std : : string words[1000];

            string temp;                            

            char c;

            int wordCount=0;

            // Check if file name is provided or not

            if ( argc != 2 )

            {

                        cout<<”No File Specified”<<endl;

                        exit(1);

            }

            ifstream inFile;

            inFile.open(argv[1]);                                                   // open input file

            if (!inFile) {

            cerr << "Error reading File!!"<< endl;

            exit(1);

            }         

            // read file contents until its end of file or the number of words read are 1000

            while( ! inFile.eof() && wordCount<1000 ) {

                        c = getc(inFile);

                        while ( c != ' ' )

                        {

                                    words[wordCount].push_back(c);

                                    c = getc(inFile);

                        }

                        wordCount++;

            }

            if (wordCount < 999){

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

                        words[i] = “ ” ;                        // empty strings added if number of words < 1000

            }

           

// Sorting words in the array

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

for(j=0; j<1000-i; j++){

            if (swapNeeded(words[j], words[j+1]))

            {

                        temp = words[j];

                        words[j] = words[j+1];

                        words[j+1] = temp;

            }

    }

    }

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

            cout<<words[i]<<endl;

}

// function with call by value, for call by reference add string &first, string &second

bool swapNeeded(string str1, string str2)               {

            if(str1.compare(str2) > 0)

                        return true;

            else

                        return false;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote