Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program works for reading a text of all upper case letters and it provides

ID: 3889595 • Letter: T

Question

This program works for reading a text of all upper case letters and it provides the  order of descending frequency. I need help modifying this exact program to do the  order of descending frequency of a text file that contains symbols,numbers,spaces,upper and lower letter.

example of the text file is like

Z4XPsXZ|%%+%WZ<X%+|%Ry%|Z|%a|POR%ZLiZ<X%ZX%c<Z<XL*|caW|

#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
void sortFrequencies(char* chars, int* freq)
{
    for(int i = 0; i < 25; i++)
        for(int j = 0; j < 25-i; j++)
            if(*(freq+j) < *(freq+j+1))
            {
               char tempC = *(chars + j);
               *(chars + j) = *(chars + j + 1);
               *(chars + j + 1) = tempC;
               int tempI = *(freq + j);
               *(freq + j) = *(freq + j + 1);
               *(freq + j + 1) = tempI;
            }
}
void Frequencies(string stmt, int *freq)
{
for(int i = 0; i < stmt.length(); i++)
{
if(!isalpha(stmt.at(i)))
continue;
else
(*(freq+(toupper(stmt.at(i)) - 'A')))++;
}
}

int main()
{
string statement, fileName;
int i = 0;
cout<<"Enter the name of the file: ";
cin>>fileName;
ifstream fin;
fin.open(fileName);
int *frequencies = (int*) malloc(sizeof(int) * 26);
//return frequencies;
for(int i = 0; i < 26; i++)
*(frequencies+i) = 0;
while(!fin.eof())
{
getline(fin, statement);
//cout<<statement<<endl;
Frequencies(statement, frequencies);
}
char *characters = (char*) malloc(sizeof(int) * 26);
for(int i = 0; i < 26; i++)
   *(characters+i) = 'A' + i;
for(int i = 0; i < 26; i++)
if(*(frequencies+i) != 0)
cout<<(char)('A'+i)<<" "<<*(frequencies+i)<<endl;
sortFrequencies(characters, frequencies);
cout << "After sorting the frequencies in descending order: " << endl;
for(int i = 0; i < 26; i++)
    if(*(frequencies+i) != 0)
        cout << characters[i] << " " << *(frequencies+i) << endl;
}

output of this program

E,22060
T,9708
A,8796
O,8310
N,8084
I,7625
H,7439
S,7265
R,6708
D,5549
L,4712
U,3476

Explanation / Answer

The code is fixed

#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
void sortFrequencies(char* chars, int* freq)
{
for(int i = 0; i < 25; i++)
for(int j = 0; j < 25-i; j++)
if(*(freq+j) < *(freq+j+1))
{
char tempC = *(chars + j);
*(chars + j) = *(chars + j + 1);
*(chars + j + 1) = tempC;
int tempI = *(freq + j);
*(freq + j) = *(freq + j + 1);
*(freq + j + 1) = tempI;
}
}
void Frequencies(string stmt, int *freq)
{
for(int i = 0; i < stmt.length(); i++)
{
if(!isalpha(stmt.at(i)))
continue;
else
(*(freq+(toupper(stmt.at(i)) - 'A')))++;
}
}
int main()
{
string statement, fileName;
int i = 0;
cout<<"Enter the name of the file: ";
cin>>fileName;
ifstream fin;
fin.open(fileName);
int *frequencies = (int*) malloc(sizeof(int) * 26);
//return frequencies;
for(int i = 0; i < 26; i++)
*(frequencies+i) = 0;
while(!fin.eof())
{
getline(fin, statement);
//cout<<statement<<endl;
Frequencies(statement, frequencies);
}
char *characters = (char*) malloc(sizeof(int) * 26);
for(int i = 0; i < 26; i++)
*(characters+i) = 'A' + i;
for(int i = 0; i < 26; i++)
if(*(frequencies+i) != 0)
cout<<(char)('A'+i)<<" "<<*(frequencies+i)<<endl;
sortFrequencies(characters, frequencies);
cout << "After sorting the frequencies in descending order: " << endl;
for(int i = 0; i < 26; i++)
if(*(frequencies+i) != 0)
cout << characters[i] << " " << *(frequencies+i) << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote