Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi there, my code is all a mess right now and I don't know what Im doing wrong the assignment: Note: Do not use classes

Hi there, my code is all a mess right now and I don't know what Im doing wrong the assignment:

Note: Do not use classes or any variables of type string to complete this assignment

Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must be ignored. Any characters entered after the period ('.') should be left in the input stream unprocessed. No limit may be placed on the length of the input.

Use an array with a struct type as its base type so that each array element can hold both a letter and an integer. (In other words, use an array whose elements are structs with 2 fields.) The integer in each of these structs will be a count of the number of times the letter occurs in the user's input.

Let me say this another way in case this makes more sense. You will need to declare a struct with two fields, an int and a char. (This should be declared above main, so that it is global; that is, so it can be used from anywhere in your program.) Then you need to declare an array (not global!) whose elements are those structs. The int in each struct will represent the number of times that the char in the same struct has occurred in the input. This means the count of each int should start at 0 and each time you see a letter that matches the char field, you increment the int field.

Don't forget that limiting the length of the input is prohibited. If you understand the above paragraph you'll understand why it is not necessary to limit the length of the input.

The table should not be case sensitive -- for example, lower case 'a' and upper case 'A' should be counted as the same letter. Here is a sample run:

Enter a sequence of characters (end with '.'): do be Do bo. xyz Letter: Number of Occurrences o 3 d 2 b 2 e 1 

Note: Your program must sort the array by descending number of occurrences. You may use any sort algorithm, but I would recommend using the selection sort from lesson 9.6. Be sure that you don't just sort the output. The array itself needs to be sorted. Don't use C++'s sort algorithm.

Submit your source code and some output to show that your code works.

Hints:

You will want to be familiar with various C++ functions for dealing with characters such as isupper, islower, isalpha, toupper, and tolower. You should assume for this assignment that the ASCII character set is being used. (This will simplify your code somewhat.)

Note that your struct should be declared above main(), and also above your prototypes, so that your parameter lists can include variables that use the struct as their base type. It should be declared below any global consts.

Students seem to get hung up on the requirement that the user can enter multiple lines of input and that you should leave characters after the period in the input stream. The intention of these requirements (believe it or not) is to steer you toward a better and simpler solution, so please don't get too hung up on them! Another way of stating the requirement is this: just keep reading characters one at a time, ignoring newline characters (that is, treating them just like any other non-alphabetic character), until you get to a period.

More hints about how this will work: The user enters a bunch of characters then hits the enter button. When the user hits the enter button, those characters go into the input stream. THEN cin.get(ch) reads the first character from the input stream, then it is counted, then the second time through the loop the second character is read from the input stream and counted, then the third time through the loop the third character is read from the input stream and counted, and so on until the loop condition becomes false or all of the characters that the user entered have been read.

At that point, if all of the characters have been read, then the cin.get(ch) causes the program to stop and wait for more characters to be entered.

If the loop has exited, then any remaining characters that the user entered will stay in the input stream, so that next time you do a cin.get(ch) or cin >> ch, those characters will be read.

One last way of saying this: If the user enters several characters, but does not enter '.', and then hits the enter key, the loop should process all the characters entered, and then just wait for more characters, until it finally sees a '.'

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

=+ For what reasons can and do unions go on strike?

Answered: 1 week ago

Question

=+ Is secondary industrial action common and/or legal?

Answered: 1 week ago

Question

=+What sanctions are available to employers

Answered: 1 week ago