Question: c++ I have given you the .cpp file, you must make a project called Lab1, and add the existing file to the project. You wont
c++
I have given you the .cpp file, you must make a project called Lab1, and add the existing file to the project.
You wont have to change anything in the main() function
You will have to fix the functions so that they work with the array of structs o
The sort function should sort the array in Descending order+
#include
#include
#include
#include
#include
using namespace std;
struct charTally
{
int tally;
char ch;
};
void getFileName(ifstream& );
void countChars(ifstream& , int[]);
void printCount(ofstream& , int[]);
void countWords(ifstream&, ofstream&);
void selectionSort(int[], int);
int main()
{
ifstream input;
ofstream output;
charTally charCount[58];
getFileName(input);
countWords(input, output);
countChars(input, charCount);
selectionSort(charCount, 58);
printCount(output, charCount);
return 0;
}
void getFileName(ifstream& in)
{
string fileName;
cout << "Please enter the name of the input file: ";
getline(cin, fileName);
in.open(fileName);
if (!in)
{
cout << "Could not open the input file, exiting ";
cout << "Press any key";
getchar();
exit(0);
}
}
void countChars(ifstream& in, int count[] )
{
char character;
int place;
for(int i = 0;i < 58; i++)
count[i] = 0;
in.clear(); //turns the stream back on
in.seekg(0); //go back to the beginning of the file
in >> character;
while (in)//while(!in.fail())
{
place = static_cast<int>(toupper(character)) - 33;
if (place >= 0 && place < 58)
count[place]++;
in >> character;
}
}
void printCount(ofstream& out, int count[])
{
double total = 0;
//out.open("mackayProj3Results.txt");
out << setprecision(5) << fixed;
out << "Character Count Percentage ";
for (int i = 0; i < 58; i++)
total += count[i];
for (int i = 0; i < 58; i++)
out << setw(6) << static_cast<char>(i + 33) << setw(11) << count[i]
<< setw(16) << count[i] / total * 100<< endl;
}
void countWords(ifstream& in, ofstream& out)
{
out.open("mackayProj3Results.txt");
int totalWords = 0;
string dummy;
//in >> dummy;
while (in >> dummy)
{
totalWords++;
//in >> dummy;
}
out << "The total number of words in the file is: " << totalWords << endl;
}
void selectionSort(int list[], int length)
{
int index;
int smallestIndex;
int location;
int temp;
for (index = 0; index < length - 1; index++)
{
//Step a
smallestIndex = index;
for (location = index + 1; location < length; location++)
if (list[location] > list[smallestIndex])
smallestIndex = location;
//Step b
temp = list[smallestIndex];
list[smallestIndex] = list[index];
list[index] = temp;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
