Create a C program using functions aside from main and sorti
Create a C++ program using functions aside from main and sorting (as described below) that will
-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;
}
}
Solution
#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
void bublesort(vector<string>& names,int c)
{
string temp;
for(int i=0; i<=c - 2; i++) {
for(int j=0; j<=c-2-i; j++) {
if(names[j].compare(names[j+1]) >0){
temp = names[j];
names[j] = names[j+1];
names[j+1] = temp;
}
}
}
}
int main() {
vector<string> names;
cout<<\"What is the name of the input file?\ \";
char filename[100];
gets(filename);
ifstream in(filename);
if(!in.is_open())
cout << \"Unable to open file\ \";
string word;
int c=0;
int max=0;
int min=99999;
string minstr,maxstr;
while(getline(in, word))
{
std::transform(word.begin(), word.end(), word.begin(),::tolower);
names.push_back(word);
c++;
}
bublesort(names,c);
int indexmax,indexmin;
vector<string> maxstring;
cout<<\"Alphabetized List of Words\ \";
for (size_t i = 0; i < names.size(); i++)
{
cout << names[i] << \'\ \';
if(names[i].length()>max)
{
max=names[i].length();
}
}
cout<<\"Longest string are\ \";
for (size_t i = 0; i < names.size(); i++)
{
if(names[i].length()==max)
{
cout<<names[i]<<endl;
}
}
}
================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
What is the name of the input file?
input.txt
Alphabetized List of Words
apple
banana
blueberry
cantaloupe
cherry
cranberry
grape
kiwi
orange
pear
pineapple
tangerine
watermelon
Longest string are
cantaloupe
watermelon




