Write a program whose main function is merely a collection ofvariable declaratio
ID: 3618272 • Letter: W
Question
Write a program whose main function is merely a collection ofvariable declarations and function calss. This program reads a textand outputs the letters, together with their counts, as explainedbelow in the function printResults. (There can be no globalvariables! All information must be passed in and out of thefunctions. Use structure to store the information.)Your programmust consist of at least the following functions: 1. openFile:Opens the input and output files. You must pass the file streams asparameters (by reference). If the files does not exist, teh programshould print an appropriate message to exit. The program must askthe user fo rthe input&output files. 2. count: Countsevery occurence of capital letters A-Z and small letteres a-z inthe txt file opened in teh function openFile. This information mustgo into an array of structures. The array must be passed as aparameter, and the file identifier must also be passed as aparameter. 3. printResults:Prints the number of capital letters and small letters, as well asthe percentage of capital letters for every letter A-Z and thepercentage of the small letters for every letter a-z. Thepercentages should look like this: "25%". This information mustcome from an array of structures, and this array must be passedas a parameter. Write a program whose main function is merely a collection ofvariable declarations and function calss. This program reads a textand outputs the letters, together with their counts, as explainedbelow in the function printResults. (There can be no globalvariables! All information must be passed in and out of thefunctions. Use structure to store the information.)Your programmust consist of at least the following functions: 1. openFile:Opens the input and output files. You must pass the file streams asparameters (by reference). If the files does not exist, teh programshould print an appropriate message to exit. The program must askthe user fo rthe input&output files. 2. count: Countsevery occurence of capital letters A-Z and small letteres a-z inthe txt file opened in teh function openFile. This information mustgo into an array of structures. The array must be passed as aparameter, and the file identifier must also be passed as aparameter. 3. printResults:Prints the number of capital letters and small letters, as well asthe percentage of capital letters for every letter A-Z and thepercentage of the small letters for every letter a-z. Thepercentages should look like this: "25%". This information mustcome from an array of structures, and this array must be passedas a parameter.Explanation / Answer
please rate - thanks #include<iostream>#include <fstream>
using namespace std;
struct letters{
int upper;
int lower;
} let[26];
int openFile(ifstream&,ofstream&);
void count(ifstream&,letters[]);
void printResults(ofstream&, letters[]);
int main()
{ifstream in;
ofstream out;
int i;
for(i=0;i<26;i++)
{let[i].upper=0;
let[i].lower=0;
}
i=openFile(in,out);
if(i==0)
{count(in,let);
printResults(out,let);
}
in.close();
out.close();
return 0;
}
void printResults(ofstream& out, letters let[])
{int i,tot;
out<<"letter upper lower % upper % lower ";
for(i=0;i<26;i++)
{out<<(char)('a'+i)<<" "<<let[i].upper<<" "<<let[i].lower;
tot=let[i].upper+let[i].lower;
if(tot==0)
out<<" "<<0<<" "<<0<<endl;
else
out<<" "<<(double)let[i].upper/tot*100.<<" "<<(double)let[i].lower/tot*100.<<endl;
}
}
void count(ifstream& in,letters let[])
{char l;
in.get(l);
while(in)
{if(l>='a'&&l<='z')
let[l-'a'].lower++;
else if(l>='A'&&l<='Z')
let[l-'A'].upper++;
in.get(l);
}
}
int openFile(ifstream& in,ofstream& out)
{char filename[30];
cout<<"what is the name of the input file? ";
cin>>filename;
in.open(filename); //open file
if(in.fail()) //is it ok?
{ cout<<"input file didnot open please check it ";
return 1;
}
cout<<"what is the name of the output file? ";
cin>>filename;
out.open(filename);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.