Using C++ You should start with your solution to Activity #2 and rename the file
ID: 3589864 • 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
Timings with call by reference
real 0m0.066s
user 0m0.032s
sys 0m0.015s
Timings with call by value
real 0m0.091s
user 0m0.054s
sys 0m0.016s
#include<string>
#include<fstream>
using namespace std;
bool swapNeeded(string &first, string &second){
if (second < first)
return true;
else
return false;
}
bool swapNeeded(string first, string second){
if (second < first)
return true;
else
return false;
}
void swapper(string &a, string &b){
string temp;
temp = a;
a = b;
b = temp;
}
int main(){
ifstream fin;
string data[1000];
fin.open("input54.txt");
if (!fin){
cout << "Error opening file ";
return 0;
}
int count = 0;
while (fin >> data[count]){
if (count == 999)
break;
count++;
}
fin.close();
if (count < 1000){
for (int i = count; i<1000; i++)
data[i] = "";
}
for (int i = 0; i<1000; i++){
for (int j = 0; j < 1000-i-1; j++){
if (swapNeeded(data[j],data[j+1])){
swap(data[j], data[j+1]);
}
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.