Can someone help me get started on my assignment?

Would anyone like to help me get started on my C++ assignment involving structures / arrays? (Not allowed to use strings)

I'm not asking you guys to like..DO my assignment because that'd be pointless for me, but could you guys help me figure out how to approach this? Here are the instructions from my class:


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 occurrences, listing each letter that occurred along with the number of times it occurred. 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 components.

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, using a selection sort. Be sure that you don't just sort the output. The array itself needs to be sorted.
right?



Reminder : I am writing in C++. Thanks guys! =]
Last edited on
No limit may be placed on the length of the input.

That's good to know. You know that you're dealing with some sort of dynamic memory.

You will need to declare a struct with two fields, an int and a char.


Okay. You have to write a struct which should look something like this:

1
2
3
4
struct Letter {
	char letter;
	int count;
}


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


If your instructor doesn't think that a constructor is to class-like, you can write a constructor for the Letter struct which initializes count to zero.

All non-alphabetic characters must be ignored. The table should not be case sensitive -- for example, lower case 'a' and upper case 'A' should be counted as the same letter.


Okay. So you know you will at some point have to declare a Letter array with 26 elements like so:

const unsigned short NUM_LETTERS = 26;
Letter letters[NUM_LETTERS];
Last edited on
Topic archived. No new replies allowed.