Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given a file of unsorted words with mixed case: read the entries in the file and sort those words into alphabetical order. The program should

Given a file of unsorted words with mixed case: read the entries in the file and sort those words into alphabetical order. 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. Note that some of these "words" have spaces, so read them one line at a time.
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
the sorted list is
bat cat eat Hat Mat pat Rat SAT TAT vat
and after sorting, the word at index 6 is Rat
You are encouraged to use this data to test your program.
The code I have is saying exception thrown at line 34 and the Int Main is underlined in green saying that the amount of bytes is too large. How do I fix this?
#include
#include
#include
#include
#include
#include
#include // Include the vector library
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 count =0, index;
ifstream dataIn;
dataIn.open("MixedCaseWords.txt");
if (dataIn.fail())
{
cout <<"** File Not Found **";
return 1;
}
else
{
while (dataIn >> word)
{
bool b = isUnique(words, count, word);
if (!b)
{
words[count]= word;
count++;
}
}
dataIn.close();
sort(words, count);
while (true)
{
cout << "Enter Index :";
cin >> index;
if (index<0|| index>count)
{
cout <<"** Invalid.Must be between 0-"<< count <<"**"<< 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)
{
string temp, mixedTemp;
string str1="";
vector lower(cnt); // Use a vector instead of an array
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;
}
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions

Question

How do epinephrine and cortisol enhance memory storage?

Answered: 1 week ago

Question

Describe four common misunderstandings of Gestalt psychology.

Answered: 1 week ago

Question

How organized or ready for action on this issue is this public?

Answered: 1 week ago

Question

What does this public know about your organization?

Answered: 1 week ago

Question

What does this public expect from your organization?

Answered: 1 week ago