I am taking an online class and, unfortunately is no online lecture both either written or video to go online with it. Just an assignment and the textbook. I have started an assignment on structs and the assignment is more challenging than what the book is preparing me for. I have also spent about 4 hours watching c++ videos online and still do not feel ready for the assignment.
I have some code in place but I don't think I am using my pointer to a struct member to to increment its count correctly. If I cout it the result looks like an address. Also, from here I am not sure what to do next. Any help is appreciated.
Instructions:
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:
Each time you read a character you will increment the corresponding counter in your array. You should not have to traverse your entire array to do this. Normally we aren't all that concerned with writing some code that is slightly inefficient, but this is highly inefficient. If you search the array to match the character input by the user, you will lose a point or two.
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.
You will not need to read the user's input into an array or a c-string or a string. The only variables you will need are the array of structs described above, one single char variable to store each single character as it is read, and perhaps a couple of loop counters.
I used to have several paragraphs trying to explain how the input will work in this assignment. I've decided to just give you the basics of the input loop instead. You should use this:
cout << "Please enter a sequence of characters terminated with a period ('.'): ";
cin >> ch;
while (ch != '.'){
[insert code here to count ch if it is a letter. Should be just 2 - 3 lines of code max.]
cin >> ch;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
include <iostream>
using namespace std;
struct MyStruct
{
int countLetters = 0;
char letters;
};
int main()
{
char ch;
int size = 0;
MyStruct* array = new MyStruct[size];
cout << "Please enter a sequence of characters terminated with a period ('.'): ";
cin >> ch;
while (ch != '.') {
//[insert code here to count ch if it is a letter.Should be just 2 - 3 lines of code max.]
if (ch != '.') {
array->countLetters++;
cout << array->countLetters;
}
cin >> ch;
}
delete[] array;
system("pause");
return 0;
}
|