I need to write a program that will open up a text file of w

I need to write a program that will open up a text file of words named \"shuffled.txt\", reads in each word into an array and sorts the array. Write the sorted array of words out to a file named sorted.txt . Time your program to figure out how long it takes to sort the words.

Solution

Code:

Method 1:

#include <bits/stdc++.h>
using namespace std;

int main(){
   ifstream inf(\"shuffled.txt\");
   ofstream ouf(\"sorted.txt \");
   int n;
   inf>>n;
   string arr[n];
   for(int i=0;i<n;i++){
       inf>>arr[i];
   }
   clock_t tStart = clock();
   sort(arr,arr+n);
   printf(\"Time taken: %.2fs\ \", (double)(clock() - tStart)/CLOCKS_PER_SEC);
   for(int i=0;i<n;i++){
       ouf<<arr[i]<<endl;
   }
   ouf.close();
   inf.close();
}

Method 2:

#include <bits/stdc++.h>
using namespace std;

int main(){
   ifstream inf(\"shuffled.txt\");
   ofstream ouf(\"sorted.txt \");
   string s;
   vector<string> arr;
   for(int i=0;i<n;i++){
       inf>>s;
       arr.push_back(s);
   }
   clock_t tStart = clock();
   sort(arr.begin(),arr.end());
   printf(\"Time taken: %.2fs\ \", (double)(clock() - tStart)/CLOCKS_PER_SEC);
   for(int i=0;i<arr.size();i++){
       ouf<<arr[i]<<endl;
   }
   ouf.close();
   inf.close();
}

NOTE:

Method 1 assumes that first line of the input file contain number of words in the file.

Method 2 doesn\'t assume that.

I need to write a program that will open up a text file of words named \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site