This program works for reading a text of all upper case letters and it provides
ID: 3889605 • 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 being read is like<<<<<<<<<<<<<<
Z4XPsXZ|%%+%WZ<X%+|%Ry%|Z|%a|POR%ZLiZ<X%ZX%c<Z<XL*|caW|
For example in the current program I have the output text file give me, It's basically the same concept but I need it for a text file shown above^^
E,22060
T,9708
A,8796
O,8310
N,8084
I,7625
H,7439
S,7265
R,6708
D,5549
L,4712
U,3476
#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;
}
Explanation / Answer
#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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.