Question

In: Computer Science

C++ Lexicographical Sorting Given a file of unsorted words with mixed case: read the entries in...

C++ Lexicographical Sorting

Given a file of unsorted words with mixed case: read the entries in the file and sort those words lexicographically. The program should then prompt the user for an index, and display the word at that index. Since you must store the entire list in an array, you will need to know the length. The "List of 1000 Mixed Case Words" contains 1000 words.

You are guaranteed that the words in the array are unique, so you don't have to worry about the order of, say, "bat" and "Bat."

For example, if the array contains ten words and the contents are

cat Rat bat Mat SAT Vat Hat pat TAT eat

after sorting, the word at index 6 is Rat

You are encouraged to use this data to test your program.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

==============================

// mixedCaseWords.txt (input file)

cat Rat bat Mat SAT Vat Hat pat TAT eat Bat

==============================

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;

bool isUnique(string words[],int cnt,string word);
void sort(string words[],int cnt);
int main() {
   const int SIZE=1000;
   string words[SIZE];
   string word;
   int cnt=0,index;
   //Declaring variables
ifstream dataIn;
dataIn.open("mixedCaseWords.txt");
   //checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **";
return 1;
}
else
{
//Reading the data from the file
while(dataIn>>word)
{
  
   bool b=isUnique(words,cnt,word);
   if(!b)
   {
   words[cnt]=word;
   cnt++;        
       }

   }
  

dataIn.close();

  
sort(words,cnt);

  
  

while(true)
{
cout<<"Enter Index :";
cin>>index;
   if(index<0 || index>cnt)
   {
       cout<<"** Invalid.Must be between 0-"<<cnt<<" **"<<endl;
       }
       else
       break;  
   }

  

cout<<"After sorting, the word at index "<<index<<" is "<<words[index]<<endl;
  
}
   return 0;
}
bool isUnique(string words[],int cnt,string word)
{
   string str1,str2;
  
       for(int k=0;k<word.length();k++)
       {
           str2+=tolower(word.at(k));
       }
  
   for(int i=0;i<cnt;i++)
   {
       str1="";
      
      
       for(int j=0;j<words[i].length();j++)
       {
           str1+=tolower(words[i].at(j));
       }
  
      
       if(str1.compare(str2)==0)
       {
      
           return true;
       }
      
   }
   return false;
}
void sort(string array[],int cnt)
{
       //This Logic will Sort the Array of elements in Ascending order
   string temp,mixedTemp;
  
       string str1="";
       string lower[cnt];
       for(int m=0;m<cnt;m++)
   {
       for(int s=0;s<array[m].length();s++)
       {
       str1+=tolower(array[m].at(s));  
               }
       lower[m]=str1;
       str1="";
      
           }

   for (int i = 0; i < cnt; i++)
{
  
      
for (int j = i + 1; j < cnt; j++)
{
  
if (lower[i].compare(lower[j])>0)
{
  
temp = lower[i];
lower[i] = lower[j];
lower[j] = temp;
  
mixedTemp=array[i];
array[i]=array[j];
array[j]=mixedTemp;
}


}
  
}
  
}

===========================================

Output:

=====================Could you plz rate me well.Thank You


Related Solutions

Done in c++, Read an unsorted keywords file once to determine how many words are in...
Done in c++, Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check...
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
In this lab, you will read and write to a file, as well as sorting the...
In this lab, you will read and write to a file, as well as sorting the information. Copy and paste the following into a file named "inputs.txt": 7 199922007 C.J. Cregg Political_Science 200822013 Olivia Dunham Criminal_Justice 199822007 Josh Lyman Law 199922006 Toby Ziegler Communications 200922015 Leslie Knope Public_and_Environmental_Affairs 199922004 Sam Seaborn Law 200722013 Walter Bishop General_Sciences The input file provides details for a student database in the following format: Number_of_students ID_Number Student_First_Name Student_Last_Name Major …… ID_Number Student_First_Name Student_Last_Name Major Your...
Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those...
Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those integers into an array and sorts the integers in the array. The program should then prompt the user for an index, and then display the integer at that index. For example, if the array contains ten integers and the contents are 79 4 42 51 12 22 33 17 91 10 after sorting, the integer at index 6 is 42 You are encouraged to...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
* readCsvFile() -- Read in a CSV File and return a list of entries in that...
* readCsvFile() -- Read in a CSV File and return a list of entries in that file.    * @param filePath -- Path to file being read in.    * @param classType -- Class of entries being read in.    * @return -- List of entries being returned.    */    public <T> List<T> readCsvFile(String filePath, Class<T> classType){        return null;    } implement this class. Return a list of T type. dont worry about CSV format. Just assume...
Read from a file that contains a paragraph of words. Put all the words in an...
Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will contain a sentence with words separated...
Write a program that displays all the cars in the given file (no sorting). Each car...
Write a program that displays all the cars in the given file (no sorting). Each car property is separated by a tab character: \t Given file: cars.txt make model year Ford Expedition 2003 Mazda B-Series 1989 Ford Freestar 2003 Hyundai Elantra 2001 Hyundai Entourage 2008 Chevrolet Camaro 2011 Chevrolet Monte Carlo 2006 Chevrolet Blazer 1996 Chevrolet Aveo 2005 Chevrolet Corvette 1999 Mercedes-Benz E-Class 2006 Dodge Avenger 1995 Pontiac Grand Prix 1973 Mitsubishi Outlander 2011 MINI Clubman 2011 Suzuki Aerio 2007...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT