- #include <iostream>
-
#include <fstream>
-
#include <string>
-
#include <cctype>
-
using namespace std;
-
-
//function prototypes
-
void initialize(int [], int);
-
int readData(char [], int, int [], int);
-
void printResults(char [], int, int [], int, int);
-
-
//constant global variables used for arrays
-
const int LIMIT = 27;
-
const int ALPHA = 26;
-
int main()
-
{
-
char alphabet[ALPHA];
-
int count[LIMIT];
-
int total;//holds the total number of characters in the file
-
-
//sets each letter in the alphabet to a subscript in the alphabet array
-
alphabet[0] = 'A';
-
alphabet[1] = 'B';
-
alphabet[2] = 'C';
-
alphabet[3] = 'D';
-
alphabet[4] = 'E';
-
alphabet[5] = 'F';
-
alphabet[6] = 'G';
-
alphabet[7] = 'H';
-
alphabet[8] = 'I';
-
alphabet[9] = 'J';
-
alphabet[10] = 'K';
-
alphabet[11] = 'L';
-
alphabet[12] = 'M';
-
alphabet[13] = 'N';
-
alphabet[14] = 'O';
-
alphabet[15] = 'P';
-
alphabet[16] = 'Q';
-
alphabet[17] = 'R';
-
alphabet[18] = 'S';
-
alphabet[19] = 'T';
-
alphabet[20] = 'U';
-
alphabet[21] = 'V';
-
alphabet[22] = 'W';
-
alphabet[23] = 'X';
-
alphabet[24] = 'Y';
-
alphabet[25] = 'Z';
-
-
initialize(count, LIMIT);//call funtion
-
total = readData(alphabet, ALPHA, count, LIMIT); //call function
-
printResults(alphabet, ALPHA, count, LIMIT, total); // call function
-
//system("pause");
-
return 0;
-
}
-
-
//this function initializes the counts in the array to 0. It accpes the count
-
//array and the number of elements in the array, which is n.
-
void initialize(int count1[], int n)
-
{
-
for(int i=0; i<n; i++)
-
count1[i] = 0;
-
}
-
-
//this function opens the input file and reads a character at a time using the
-
//get() function. It reads from the file until there is no more information to
-
//be read, in which case the while loop ends. this function accepts the alphabet
-
//array, the number of elements in that array, which is c, the count array, and
-
//the number of elements in that array, which is m.
-
int readData(char brr[], int c, int count2[], int m)
-
{
-
char x;
-
int counter = 0;
-
-
ifstream infile;
-
infile.open("jkprogram.txt");//opens the input file
-
infile.get(x);//reads a character from the file
-
x = toupper(x);
-
while(infile) //reads from the file until there is nothing else to be read
-
{
-
for(int z=0; z<c; z++)//goes through every letter in the array
-
{
-
//if the read in character matches a letter in the alphabet
-
if(x == brr[z])
-
count2[z]++; //increments the count if there is a match
-
}
-
//tests if the same read in character is not a letter or white space
-
if(isalpha(x) == false && isspace(x) == false)
-
//if not a letter or white space, increments the count for "other",
-
//which is the last subscript in the count array. Notice the
-
//subscript in the count array is 1 more than the alphabet array.
-
count2[m-1]++;
-
if(isspace(x) == false)//tests if read in character is a white space
-
counter++; //keep track of total number of characters
-
infile.get(x);//read in next character
-
x = toupper(x);//changes the read in character to uppercase, which in
-
//this case ignores the case of the letter
-
}
-
infile.close(); //closes the file
-
return counter;//returns the total number of characters in the file
-
}
-
-
//this function displays the letters and their counts. It accepts the alphabet
-
//array, which is crr, the number of elemetns in that array, which is d, the
-
//count array, which is count3, the number of elements in that array, which is
-
//p, and the total number of characters in the file, which is counttotal.
-
void printResults(char crr[], int d, int count3[], int p, int counttotal)
-
{
-
ofstream outfile;
-
outfile.open("histogram.txt"); //opens the output file
-
float percent;
-
-
-
outfile.setf(ios::fixed, ios::floatfield);
-
outfile.precision(2);//sets the number of decimal places to be shown
-
outfile.width(15);//sets the width of where to display the information
-
outfile << "Letter";
-
outfile.width(15);
-
outfile << "Occurrences";
-
outfile.width(15);
-
outfile << "Percentage" << endl;
-
outfile << "--------------------------------------------------------------";
-
outfile << endl;
-
for(int b=0; b<d; b++)//goes through the elements in the array
-
{
-
outfile.width(15);
-
outfile << crr[b];//displays each letter in the alphabet array
-
outfile.width(15);
-
outfile << count3[b];//displays the count of each letter
-
outfile.width(15);
-
-
percent = static_cast<float>((count3[b]*100))/counttotal;
-
outfile << percent << "%" << endl;
-
}
-
-
outfile.width(15);
-
outfile << "Other";
-
outfile.width(15);
-
outfile << count3[p-1]; //displays the last count in the count array
-
outfile.width(15);
-
-
percent = static_cast<float>((count3[p-1]*100))/counttotal;
-
outfile << percent << "%" << endl;
-
-
outfile.close();//closes the output file
-
}
1 2978 Banfa 9,065
Recognized Expert Moderator Expert
Works for me, it is probably not finding your input file after attempting to open a file you should check to see if it has opened and output an error message if it has not.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Hung Jung Lu |
last post by:
Hi,
I have been looking into AOP (Aspect-Oriented Programming) for
sometime, now. I frankly don't like the syntax of any of the
approaches I have seen so far. I am kind playing around with some
ideas, and maybe write up an article later.
AOP is not just buzzword. It's not just callback, it's not just Ruby's
MixIn, it's not just Python's...
|
by: Hung Jung Lu |
last post by:
Hi,
I know people have talked about it before, but I am still really
confused from reading the old messages.
When I talk about code blocks, I am not talking about Lisp/Ruby/Perl,
so I am not making comparisons.
If you have a file, in Python you could do:
|
by: John Rivers |
last post by:
hi, here is how to do it and restore sanity to aspx html rendering:
(please only reply with sensible architectural discussion - juan)
put this at the end of an aspx file (or use an include at the end
if you want to reuse it on many aspx pages)
(notice closing brace)
(start)
<%}
|
by: xiaotom |
last post by:
I want my software to be independant of operation system and databases.
That's why I want to use odbc, and don't want to use MFC.
Here I have some questions to ask:
1. On unix (like sun solaris), does there exists any kind of midware
like odbc?
2. I search on the internet, and find most of the examples are based on
PHP. Is there any c++...
|
by: aldrin |
last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0
compiler with great difficulty.There is no problem with running this
under KDevelop in linux. Any help would be greatly appreciated.
Enter an interesting string.
Too many cooks spoil the broth
Error opening C:\myfile.txt for writing. Program termnated.
This...
| |
by: zika.ondra |
last post by:
Please could someone write me in which program should I start
programin? I tried to write the source in notepad and than save it as
*.exe file but it didn't work. Help me please, Thanks.
|
by: Nena |
last post by:
Hi there,
I'm trying to include the Numerical Recipes for C++
for the Dev-C++ Editor Version.
But so far I've failed.
Therefore I've copied all head-files (for example nr.h)
into the folder where my program is. When compiling my
program two windows are poping up.
The first one is saying "Total errors 0" and "Size of
|
by: linarin |
last post by:
#include <iostream>
using namespace std;
typedef bool (*CallableFunction)(int argc,char* argv);
void DefineMyFunction(const char* name,CallableFunction func){
//here do the define action.
}
template<typename R,typename A,R (*pFunc)(A)>
|
by: rockz |
last post by:
Hi, i know this might sound stupid but i keep getting this error each time when i compile! and have no idea what the comp is taking bout! please help!
"system cannot find specified file"
it also says something about there not seeming to be a GNU make file in path or in Dev C++ bin path.
how do i get this GNU make file etc.
|
by: xpun |
last post by:
hey all
So I decided to be a little ambitious over the summer and create the game of clue in c. I figured it would give my self a GOOD refresher of the language and further understand functions arrays objects etc..
Any way, I encountered a strange logic error and kinda put it to the side until now.
This code is supposed to role the die,...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |