Question:
Need to make a C++ program that has a function that does a character count after reading a txt file...?
Alex
2009-11-11 11:10:06 UTC
Write a program to create an alphabet histogram from a data input file.

1) The histogram will keep track of every occurrence of a letter from the alphabet. The histogram should ignore a letter’s case. All non-alpha characters should be included in a count for other characters.

1) you must write the following functions:

initialize – which sets your array of counters to 0
readData which opens the file and populates the arrays’ counters with their counts based on the file data. Be sure to close the file before the function exits!
printResults which prints the histogram results to the output file. Be sure to close the output file before the function exits!

That's the assignment I need to do. The answer doesn't have to be this exact program, just something I can use as a reference for code blocks I could use to write this program.
Four answers:
Ciaron
2009-11-11 11:41:44 UTC
What you need to do is have the function use a loop to read in a character at a time and increment the loop use an array to store the occurances

Here's a small program that should start you off unless you want fries with that.

#include

#include

#include

#include

int main() {

int sum=0 ;

char x;

ifstream inFile;

int alphabet[26];

inFile.open("test1.txt");

if (!inFile) {

cout << "Unable to open file";

}



while (inFile >> x) {

sum++;

switch (x) {

case 'a': alphabet[0]++;

break;

case 'b': alphabet[1]++;

break;

case 'c': alphabet[2]++;

break;

case 'd': alphabet[3]++;

break;

case 'e': alphabet[4]++;

break;

case 'f': alphabet[5]++;

break;

case 'g': alphabet[6]++;

break;

case 'h': alphabet[7]++;

break;

case 'i': alphabet[8]++;

break;

case 'j': alphabet[9]++;

break;

case 'k': alphabet[10]++;

break;

case 'l': alphabet[11]++;

break;

case 'm': alphabet[12]++;

break;

case 'n': alphabet[13]++;

break;

case 'o': alphabet[14]++;

break;

case 'p': alphabet[15]++;

break;

case 'q': alphabet[16]++;

break;

case 'r': alphabet[17]++;

break;

case 's': alphabet[18]++;

break;

case 't': alphabet[19]++;

break;

case 'u': alphabet[20]++;

break;

case 'v': alphabet[21]++;

break;

case 'w': alphabet[22]++;

break;

case 'x': alphabet[23]++;

break;

case 'y': alphabet[24]++;

break;

case 'z': alphabet[25]++;

break;



default: break;

}

cout<
}

cout<<" There are "<


cout<<" There were occurances of the letters "<
cout<<" A occured "<
cout<<" B occured "<
cout<<" C occured "<
cout<<" D occured "<
cout<<" E occured "<
cout<<" F occured "<
cout<<" G occured "<
cout<<" H occured "<
cout<<" I occured "<
cout<<" J occured "<
cout<<" K occured "<
cout<<" L occured "<
cout<<" M occured "<
cout<<" N occured "<
cout<<" O occured "<
cout<<" P occured "<
cout<<" Q occured "<
cout<<" R occured "<
cout<<" S occured "<
cout<<" T occured "<
cout<<" U occured "<
cout<<" V occured "<
cout<<" W occured "<
cout<<" X occured "<
cout<<" Y occured "<
cout<<" Z occured "<






inFile.close();

getch();

return 0;

}
Nick T
2009-11-11 19:20:19 UTC
Ok This is obviously your homework. The idea is that YOU practise to reinforce a concept or idea studied in class. At least attempt to do it and then post code if and when you go wrong. Expecting us to just do it for you means you will never learn anything.



The question is well written and gives you a decent strategy for how to approach the problem.



The question doesnt specify wether you must use Global variables or arguments to pass data but it is a minor difference in implementation which can be ignored for pseudo code.



In reality I wouldnt use the initialise code given below, but for language independant pseudo code it is the easiest description.



A histogram should by definition include some form of graphical output, wether it is asterisks or blobs or the character itself. How you scale it is then the next problem, By percentage may be the best approach.

PSEUDO CODE





FUNCTION Initialise

..LOOP FOR EACH element of the data array

....data[element] = 0

..END LOOP

END



FUNCTION readData

prompt Enter Filename

get filename

OPEN file called filename

Clear Character count

IF ( File Exists ) THEN

..LOOP WHILE NOT End Of File

....Read character from File

....Increment CharacterCount

....IF ( Character is Alpha ) THEN

......Increment Data Array counter for this character

....ELSE

......Increment Other Character counter

....END IF

..END LOOP

..Close File

ELSE

..output Unable to open specified file!

END IF

result = CharacterCount

END



FUNCTION printResults

prompt Enter Output Filename

get filename

OPEN file called filename for writing

IF ( File Exists ) THEN

..output to file Total Characters read (CharacterCount)

..LOOP FOR EACH element of the data array

....output to file Character ID Character count data[element]

..END LOOP

..output to file Other Character counter

..Close File

ELSE

..output Unable to open specified file!

END IF

END



MAIN ENTRY POINT

..Call Initialise

..Call readData

..Call PrintResults

END
MichaelInScarborough
2009-11-12 00:27:38 UTC
/*

If there are errors in the code, you have to argument them. If you can't argument them, you have not understood the code. If you have not understood the code and present it, you could be considered fraudulent.

*/



#include

#include

#include



void initialize(int *iArray, int iSize)

{

memset(iArray, 0, iSize * sizeof(int));

}



void readData(int *iArray, int iSize, const char *szFile)

{

FILE *fInput;

int iC;

fInput = fopen(szFile, "rt");

if (!feof(fInput)){

while(!feof(fInput)){

iC = getc(fInput);

if (iC >= 'A' && iC <= 'Z') {

iC += 32; /* make lower case */

}

if (iC >='a' && iC <= 'z'){

++iArray[iC-'a'];

++iArray[iSize-1];

}

else {

++iArray[iSize-2];

}

}

fclose(fInput);

}

}

void printResults(int *iArray, int iSize)

{

double dblTotal = 0.0;

double dblPercentage = 0.0;

int iPercentage = 0;

int i, j, iStars = 0;

fprintf(stdout, "Statistics\n==========\n");

for (i = 0; i < 'Z' - 'A' + 1; i++){

dblPercentage = (double)iArray[i]*100.0/(double)iArray[iSize-1];

fprintf(stdout, "%c = %6d (%5.2lf%%) ", 'A' + i, iArray[i], dblPercentage);

iPercentage = 0;

dblTotal += dblPercentage;

iStars = (int)(dblPercentage + 0.5);

for (j = 0; j < iStars; j++){

if ((j + 1) % 10 == 0){

fprintf(stdout, "%d", (j + 1)/10);

}

else {

fprintf(stdout, "*");

}

}

fprintf(stdout,"\n");

}

//fprintf(stdout, "Alpha: %7d\n", iArray[iSize-2]);

//fprintf(stdout, "Other: %7d\n", iArray[iSize-1]);

//fprintf(stdout, "Total: %7d\n", iArray[iSize-2] + iArray[iSize-1]);

//fprintf(stdout, "Prct.: %7.2lf\n", dblTotal);

}



int main(char *argv[], int argc)

{

int iArray[28];

const char szFile[] = argv[1];

readData(iArray, 28, szFile);

printResults(iArray, 28);

return 0;

}
Rooker
2009-11-11 19:32:20 UTC
here are some references for code blocks:



http://www.daniweb.com/forums/thread118509.html



http://lernc.blogspot.com/2009/03/program-to-read-file-and-count-blank.html



http://www.cplusplus.com/forum/general/4021/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...