Create a C++ program using functions aside from main and sorting ( as described
ID: 3777860 • Letter: C
Question
Create a C++ program using functions aside from main and sorting (as described below) that will
DO NOT USE VECTORS OR ALGORITHM. USE FSTREAM, STRING, AND IOSTREAM AS SHOWN IN THE CODE BELOW.
-interactively prompt the user for and read the name of an input file that contains a maximum of 20 words
-open the input file
-read the words and store them in an array, counting as they are read (see get_data function in lecture notes)
-the words may be in a mixture of upper and lower case letters
-format the words and sort them into alphabetical order
-open an output file, the name of the file must be "help.ex11"
-write the following to the output file
a. an alphabetical list of the words (1 per line)
b. a list of the longest word(s) in the file
c. label the output appropriately
d. close all files
-the program must make use of functions and pass parameters (no global variables)
-there must be a function that when passed a string (a word from the file) will format the word (you may decide on the formatting choice - word formats must be consistent before sorting)
-there must be a function to sort the words (bubblesort recommended)
-function(s) to identify and/or display list of longest words
-additional functions are optional
-the file can only be read ONE time
-an array MUST be used to store the words in the file
NOTES:
Assumptions about input: (you do not have to test for these conditions)
data file will exist, it will not be empty
a word is a consecutive series of non-whitespace characters
words will be separated by blanks and/or linefeeds
words in the file will be made up of letters and/or digits
longest word in the file may not be unique
the last line in the data file will be terminated with a linefeed (' ')
Program must be designed to read and write to filestream variables.
Output file MUST be named help.ex11
Include all header files for library functions used by the program.
Label output.
Sample terminal session:
[keys]$ more data4eleven
CaNtalouPe
cheRRy
pear
pinEapplE
cranBerrY
APPLE
waTErmeLon
gRape
bAnAnA
kiWi
oRange
tanGErine
blueberry
[keys]$ g++ ex11.cpp
[keys]$ ./a.out
What is the name of the input file? data4eleven
[keys]$ more help.ex11
Alphabetized List of Words
apple
banana
blueberry
cantaloupe
cherry
cranberry
grape
kiwi
orange
pear
pineapple
tangerine
watermelon
List of Longest Words
cantaloupe
watermelon
HERE IS WHAT BUBBLESORT IS:
void bubblesort(int list[ ],int count)
// Sort an array of integers into descending order.
// Parameters:
// list: array of integers to be sorted
// count: (integer) number of values in the array
// Value passed back: sorted list
{
int temp; //place holder when values are interchanged
for (int i=0; i < count-1; i++)
for (int j=0; j < count-(i+1); j++)
if (list[j] < list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
HERE IS A START TO THE CODE (I HAVE TO USE BUBBLESORT AND USE VOID FUNCTIONS AND SEPERATE FUNCTIONS, NOT JUST MAIN.)
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std:
int main()
{
return 0;
}
void bubblesort(int list[ ],int count)
// Sort an array of integers into descending order.
// Parameters:
// list: array of integers to be sorted
// count: (integer) number of values in the array
// Value passed back: sorted list
{
int temp; //place holder when values are interchanged
for (int i=0; i < count-1; i++)
for (int j=0; j < count-(i+1); j++)
if (list[j] < list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
//this is required for using built in function transform to convert as string of array to lower case
#include <algorithm>
//define MAX words
#define MAX 100
using namespace std;
int main()
{
//string to hold words
string words[MAX];
//declare function which reads from file
int readFile(char *fname,string s[]);
void writeFile(string s[],int size);
//soring function declaration
void sort(string s[],int size);
char name[20];
int size;
cout<<"What is the name of the input file? (data4eleven)"<<endl;
cin>>name;
strcat(name,".txt");
if ( ( size = readFile(name,words) ) < 0 )
{
cout<<"Error occured while reading file"<<endl;
return -1;
}
//call soring function to sort list of words
sort(words,size);
writeFile(words,size);
}
int readFile(char *fname,string s[])
{
//declare object of type filestream to read file
ifstream in;
in.open(fname);
if(!in)
{
cout<<"Unable to open file.. ";
return -1;
}
int cnt = 0 ;
//read the file till we get EOF
while(!in.eof())
{
in>>s[cnt++];
}
//close file after use
in.close();
return cnt;
}
void writeFile(string s[],int size)
{
ofstream out;
//variable to hold longets size of word
int longestSize;
//function to find longest size of word
int longest(string s[],int size);
out.open("help.ex11");
if(!out)
{
cout<<"Unable to open file for writing ";
return;
}
for( int i = 0 ;i< size; i ++)
{
out<<s[i]<<endl;
}
out<<"List of Longest Words"<<endl;
longestSize = longest(s,size);
for(int i = 0 ;i< size; i ++)
{
if(s[i].size() == longestSize)
out<<s[i]<<endl;
}
//close file after use
out.close();
}
//function to find longets word size
int longest(string s[],int size)
{
int longest=s[0].size();
for(int i = 0 ;i< size; i ++)
{
if( s[i].size() > longest)
longest = s[i].size();
}
return longest;
}
//using bubble sort
void sort(string s[],int size)
{
string temp; //place holder when values are interchanged
//convert all words to lower case
for (int i = 0; i < size ; i++)
std::transform(s[i].begin(), s[i].end(), s[i].begin(), ::tolower);
for (int i=0; i < size-1; i++)
for (int j=0; j < size-(i+1); j++)
if (s[j] > s[j+1])
{
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
-------------------------------------------------------------------------------
got output in help.ex11 as below
apple
banana
blueberry
cantaloupe
cherry
cranberry
grape
kiwi
orange
pear
pineapple
tangerine
watermelon
List of Longest Words
cantaloupe
watermelon