| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Letter count program
Okay, I'm in a huuuge rush to get these programs done by friday.
COPY PASTA: You are to write a letter counting program to try to discover what letters of the alphabet are most commonly used. Your program will assume that all letters are CAPITAL letters (If they are lower case, you must convert them!), and will read in a specified number of (up to) 80-character lines. At the end, your program should print each letter followed by a BAR GRAPH showing how many times it was used. For example, your output might look like: A ********** (10) B *** (3) C ** (2) Okie dokie, anyways I have basically the whole program written but after I enter a phrase I get an error saying "Stack around letterfreqs" is corrupted. It won't work. Can someone please: -Help me find out why this is happening -Give tips to ensure correct output Heres the code: Code:
//Letter count - Nov.7 - Ryan Doran
// - This program asks the user to input (up to) 80 character lines
// After the lines are inputted, a bar graph displaying the letter
// amount of times used in sarts and a number is shown.
#include <iostream>
#include <conio.h>
using namespace std;
//Prototypes
void initfreqs(int letterfreqs[26]); //Initializes frequencies of the numbers to 0.
void processlines(int letterfreqs[26], char templ8[26] ); //Processes the information in the lines
//(How many of each letter etc.)
void printout(int letterfreqs[26], char templ8[26]); //Prints in a bar graph
int main(void)
{
int letterfreqs[26]; //No blanks or spaces
char templ8[26] = {'A','B','C','D','E','F','G','H','I','J','K','L',' M','N','O','P','Q','R','S','T','U','V','W','X','Y' ,'Z'};
//26 Character template
initfreqs(letterfreqs ); //Initialize, setting all to 0.
processlines(letterfreqs, templ8 ); //This asks how many lines there will be,
//and then updates the letter frequencies for that many lines
printout(letterfreqs, templ8); //{Produces the Bar Graphs!}
return 0;
}//Main
void initfreqs(int letterfreqs[26])
//letterfreqs - the frequency in which a letter appears in a phrase
{
int count; //Loop counter
//Set all elements in letterfreqs to 0
for (count = 0; count <26; count++)
letterfreqs[count]=0;
}//initfreqs
void processlines(int letterfreqs[26], char templ8[26] )
//letterfreqs - the frequency in which a letter appears in a phrase
//templ8 - Used to count when a letter pops up
{
int pos; //Loop counter
int howmany; //How many lines want to be entered
int alpha; //Used to count how many of one letter are in each line.
int count; //Loop counter
char newline[81]; //81 characters of input from the user
cout<<"How many lines of (up to) 81 characters? "<<endl;
cin>>howmany;
for(count = 1; count <= howmany; count++)
{
//Read in 1 line (see davidLETTERman)
cout<<"Enter phrase (up 80 characters): ";
pos=0;
do{newline[pos] = toupper(_getche());
pos++;
}while ((newline[pos-1]!='\r')&&(pos < 80));
{//Find which letter it alphabet
alpha = 0;
while(newline[pos] != templ8[alpha])
alpha++;
//If found - update the frequency
letterfreqs[alpha]++;
}//while
}//for
}//Processlines
//Printout
//like lotto 6/49
void printout(int letterfreqs[26], char templ8[26])
{
int pos; //Loop counter
int stars; //Loop counter
for(pos=0; pos<26; pos++)
{cout<<templ8[pos];
for (stars=1; stars <= letterfreqs[pos]; stars++)
cout<<"*";
cout<<endl;
} // for
}//Printout
Thanks for any help! |
|
#2
|
|||
|
|||
|
For a start you definitely don't need the temp18 array, you can work out the letters easier in code.
|
|
#3
|
|||
|
|||
|
Was very close to working, i got it going and tidied it up a bit.
Code:
//Letter count - Nov.7 - Ryan Doran
// - This program asks the user to input (up to) 80 character lines
// After the lines are inputted, a bar graph displaying the letter
// amount of times used in sarts and a number is shown.
#include <iostream>
#include <conio.h>
using namespace std;
//Prototypes
void initfreqs (int *letterfreqs); //Initializes frequencies of the numbers to 0.
void processlines (int *letterfreqs); //Processes the information in the lines
void printout (int *letterfreqs); //Prints in a bar graph
int main(void)
{
int letterfreqs[26];//No blanks or spaces
initfreqs(letterfreqs ); //Initialize, setting all to 0.
processlines(letterfreqs); //This asks how many lines there will be,
//and then updates the letter frequencies for that many lines
cout << endl;
printout(letterfreqs); //{Produces the Bar Graphs!}
cout << "Hit Space To Exit\n";
while ( getch() != 32 );
return 0;
}//Main
void initfreqs(int *letterfreqs) //letterfreqs - the frequency in which a letter appears in a phrase
{
for (int count = 0; count <26; count++) //Set all elements in letterfreqs to 0
letterfreqs[count] = 0;
}//initfreqs
void processlines(int *letterfreqs) //letterfreqs - the frequency in which a letter appears in a phrase
{
int howmany; //How many lines want to be entered
cout << "How many lines of (up to) 81 characters? "<<endl;
cin >> howmany;
for(int count = 1; count <= howmany; count++)
{
char newline[81]; //81 characters of input from the user
int pos = 0;
cout<<"Enter phrase (up 80 characters): ";
do
{
//Read in 1 line (see davidLETTERman)
newline[pos] = toupper(_getche());
// this bit was outside the do/while loop so not run properly
int alpha = newline[pos] - 65;
if(alpha >=0 && alpha <= 25)
++letterfreqs[alpha];
++pos;
}
while ( (newline[pos-1]!='\r') && (pos < 80) );
}//for
}//Processlines
//Printout - like lotto 6/49
void printout(int *letterfreqs)
{
for(int pos=0; pos<26; pos++)
{
cout<< (char)(pos + 65);
for (int stars=1; stars <= letterfreqs[pos]; stars++)
cout<<"*";
cout<<endl;
} // for
}//Printout
|
|
#4
|
|||
|
|||
|
Thanks a bunch! (Although what did you do when you changed my parameters? lol.)
|
|
#5
|
|||
|
|||
|
pointer (the *) and arrays ( the []) are actually both passed to functions as a pointer (to the first element in the case of an array) in c / c++.
I prefer using the * myself but it could a matter of taste. I usually err on the side of less typing. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Letter count program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|