Counting and summing elements in an array? Help!

  • Thread starter Alf P. Steinbach
  • Start date
A

Alf P. Steinbach

I am trying to create a program that will ask for a user to enter
a series of letters (codes) and then print out a table that shows
the codes in decending frequency.

To enter a series a letters, read one line using std::getline.


Only letters will be read into the array.

It's not a good idea to use a raw array here. What you need is to
refer to a count with a letter as key (index). That is most easily
accomplished using a std::map.

A std::map is the most general solution.

For letters, as a special case, a raw array can be an optimization,
but in that case index it using the letter code values.


I have created a struct and array like below.

struct record
{

char inputChar;
int countChar;
};

That is ungood. You don't need it for a raw array, and you don't
need it for a std::map.



const int ARRAY_SIZE = 100;
record list[ARRAY_SIZE];

That is ungood plus. You should reserve all uppercase names for
macros. They're used for constants in Java, but the Java convention
stems from early C where constants had to be expressed as macros.


I have a function that reads the input stream into the array.

void getUserInput(record list[], int &numItems)

{

int count = 0;
char userChar;

cout << "Enter a sequence of characters (end with '.'):";

Why not use end-of-line or end-of-file as the terminator?

cin >> userChar;

Ungood, duplicated code. Choose the right kind of loop so as
to not duplicate code.

while(userChar != '.' && (count < ARRAY_SIZE))

Ungood. Careful with that indention.


{
if(isalpha(userChar)){ //check to see if input is a character
userChar = tolower(userChar); //convert all chars to lower case.
list[count].inputChar = userChar;
count++;

Style, ungood. Use prefix ++, not postfix ++.

}
if(count < ARRAY_SIZE){ //check to see if user has exceeded

Ungood. Careful with that indentation.

cin >> userChar;



}else{
cout << "You have filled the input stream!" <<endl ;

Ungood. Don't explain internals of the program to the user.


}
}
numItems = count ;
}


My question is how should I go about summing the input?


std::map<char> letters;
std::string line;

std::readline( cin, line );
// error checking here, omitted.

for( std::size_t i = 0; i < line.length(); ++i )
{
++letters[line];
}
 
S

SunMan

Hello!

I am trying to create a program that will ask for a user to enter a series of letters (codes) and then print out a table that shows the codes in decending frequency. Only letters will be read into the array.

I have created a struct and array like below.

struct record
{

char inputChar;
int countChar;
};


const int ARRAY_SIZE = 100;
record list[ARRAY_SIZE];

I have a function that reads the input stream into the array.

void getUserInput(record list[], int &numItems)

{

int count = 0;
char userChar;

cout << "Enter a sequence of characters (end with '.'):";
cin >> userChar;

while(userChar != '.' && (count < ARRAY_SIZE))
{
if(isalpha(userChar)){ //check to see if input is a character
userChar = tolower(userChar); //convert all chars to lower case.
list[count].inputChar = userChar;
count++;
}
if(count < ARRAY_SIZE){ //check to see if user has exceeded
cin >> userChar;
}else{
cout << "You have filled the input stream!" <<endl ;
}
}
numItems = count ;
}


My question is how should I go about summing the input?

The output would look something like:


Code: Number
a 3
o 2
z 1

etc.

Thanks for any input you folks have!

Mike
 
C

Chris Theis

[SNIP]
My question is how should I go about summing the input?


std::map<char> letters;
std::string line;

std::readline( cin, line );
// error checking here, omitted.

for( std::size_t i = 0; i < line.length(); ++i )
{
++letters[line];
}


Did you put the flaws in the code on purpose?

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top