In: Computer Science
Done in C++ please. And use #include iostream, string, and fstream. do not use #include algorithm.
Question 1 In this question, you will read words from a file and place them into an array of type string.
1- Make a data text file “words.txt” – that contains one word on each line. Use at least 20 words.
2- Now write a program that reads the words in the file into an array of strings (a repeated word should not be inserted into the array – your program should not allow that and you should make sure your data file has duplicate words to test this functionality). Make your array size enough to hold 1000 words. To read the words into the array, you should make a function that takes a string array, a data size integer (dsize) by reference and an ifstream – please look at the example we did in class.
3- Declare your array and file streams in main and call the function that reads the data into the array.
4- Write a printArray function that takes a string array, the dsize and an ostream object so that you could print to the console or to an output file.
5- Print your array from main by calling the printArray function – once to the console and once to a file “wordsoutput.txt”.
6- Use the selectionSort Algorithm that we covered in class to sort the array.
7- Repeat #5 and make sure the array is sorted.
8- Find the maximum string and the minimum string in the array (remember arrays are compared based on the ASCII value).
9- Write a function that takes a string and converts every character of it to uppercase. Now call that function from a function that you pass the array and dsize to, to uppercase all words in the array (convert all words in the array to uppercase letter) and call that from main passing your array to that function. Print the array.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// words.txt
beautiful
zone
lemon
rabbit
console
ambrane
quit
beautiful
zone
rabbit
console
brain
quality
price
increment
quit
beautiful
zone
gemini
algorithm
______________________
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
//Function declarations
int readData(ifstream &dataIn,string array[]);
bool isDuplicate(string word,string array[],int cnt);
void printArray(string array[],int cnt);
void selectionSort(string names[],int count);
string maxString(string names[],int count);
string minString(string names[],int count);
void converToUpperCase(string names[],int
count);
int main() {
ifstream dataIn;
//Declaring variables
const int size=1000;
string array[size]={""};
int cnt=0;
dataIn.open("words.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
cnt=readData(dataIn,array);
cout<<"Displaying the array
:"<<endl;
printArray(array,cnt);
selectionSort(array,cnt);
cout<<"Displaying the array after sorting
:"<<endl;
printArray(array,cnt);
string max=maxString(array,cnt);
cout<<"Maximum String
:"<<max<<endl;
string min=minString(array,cnt);
cout<<"Minimum String
:"<<min<<endl;
converToUpperCase(array,cnt);
cout<<"Displaying the array after convertingh to
upper-case :"<<endl;
printArray(array,cnt);
}
return 0;
}
int readData(ifstream &dataIn,string array[])
{
int cnt=0;
string word;
while(dataIn>>word)
{
if(!isDuplicate(word,array,cnt))
{
array[cnt]=word;
cnt++;
}
}
return cnt;
}
bool isDuplicate(string word,string array[],int cnt)
{
for(int i=0;i<cnt;i++)
{
if(array[i].compare(word)==0)
return true;
}
return false;
}
void printArray(string array[],int cnt)
{
for(int i=0;i<cnt;i++)
{
cout<<array[i]<<endl;
}
}
void selectionSort(string names[],int count)
{
int start;
string temp;
for (int i = count - 1; i > 0; --i)
{
start = 0;
for (int j = 1; j <= i; ++j)
{
if (names[j].compare(names[start]) > 0)
{
start = j;
}
temp = names[start];
names[start] = names[i];
names[i] = temp;
}
}
}
string maxString(string names[],int
count)
{
int maxAscii=0,val;
int indx=0;
for(int i=0;i<names[0].length();i++)
{
maxAscii+=(int)names[0].at(i);
}
for(int i=0;i<count;i++)
{
val=0;
for(int
j=0;j<names[i].length();j++)
{
val+=(int)names[i].at(j);
}
if(maxAscii<val)
{
maxAscii=val;
indx=i;
}
}
return names[indx];
}
string minString(string names[],int count)
{
int minAscii=0,val;
int indx=0;
for(int i=0;i<names[0].length();i++)
{
minAscii+=(int)names[0].at(i);
}
for(int i=0;i<count;i++)
{
val=0;
for(int
j=0;j<names[i].length();j++)
{
val+=(int)names[i].at(j);
}
if(minAscii>val)
{
minAscii=val;
indx=i;
}
}
return names[indx];
}
void converToUpperCase(string names[],int count)
{
string upper="";
for(int i=0;i<count;i++)
{
upper="";
for(int
j=0;j<names[i].length();j++)
{
if(islower(names[i].at(j)))
{
upper+=(names[i].at(j))-32;
}
else
{
upper+=(names[i].at(j));
}
}
names[i]=upper;
}
}
__________________________
Output:
_______________Could you plz rate me well.Thank
You