Note: Do not use classes or any variables of type string to complete this assign
ID: 3539731 • Letter: N
Question
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:
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.
Submit your source code and some output to show that your code works.
Hints: 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.
You will definitely want to pay close attention to section 9.2 from the text while tackling this assignment. In my solution I use cin.get(ch), isalpha(ch), and tolower(ch).
When determining which array element to access you may want to do some arithmetic that involves characters. For example, saying array[ch - 'a'] will access array[0] if ch is 'a', array[1] if ch is 'b', and so on. Let me know if you need further explanation on this concept. Although it is not completely safe, 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.
this is what i got so far. please help!!
#include <iostream>
using namespace std;const int ARRAY_SIZE = 1000;void readChars(string list[], int &numItems);
void sort(string list[], int numItems);
void printChars(const string list[], int &numItems);int main()
{
string list[ARRAY_SIZE];
int numItems; readChars(list, numItems);
// sort(list, numItems);
printChars(list, numItems, sort(list[ARRAY_SIZE], numItems);, int numItems);
}
void readChars(string list[], int &numItems)
{
string Char;
char count = 0;
cout << "Enter a sequence of characters (end with '.'): ";
cin >> Char;
int subCounter = 0; list[subCounter] = Char.substr(subCounter, 1);
char period;
while (list[subCounter] != ".")
{
list[subCounter] = Char.substr(subCounter, 1);
subCounter++;
// cout << subCounter;
// cout << list[2];
}
numItems = subCounter;
cout << numItems;
}void sort(string list[], int numItems)
{
/*
for (int count = 0; count < numItems - 1; count++){
swap(list[indexOfSmallest(list, count, numItems)],
list[count]);
}
*/
}void printChars(const string list[], int& numItems)
{ for (int count = 0; count < numItems; count++){
cout << list[count] << " ";
}}
Explanation / Answer
#include<iostream>
#include<cstdio>
using namespace std;
//DECLARE ARRAY OF SIZE
const int arrayRAY_SIZE = 1000;
// create struct to hold letter and its frequency.
struct new_type
{
char letter;
int frequecny;
};
// selection sort to sort frequencices.
void selectionSort(struct new_type array[])
{
int i,j;
struct new_type temp;
for (i=0; i<26; i++)
{
for (j=i+1; j<=26; j++)
{
// if array[i].frequecny greater than array[j].frequecny
// then swap both array objects. not just their frequencies.
if ( array[i].frequecny > array[j].frequecny )
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
int main()
{
//create array of size 26
// because maximum characters we have is 26
struct new_type arrayray[26];
// declare a char
char ch;
// initialize frequency to ZERO.
// and letter to a, b,..........z.
for(int i=0; i<26; i++)
{
arrayray[i].frequecny=0;
arrayray[i].letter=(97+i);
}
cout << "Enter a sequence of charrayacters (end with '.'): ";
while(true)
{
// read input.
cin.get(ch);
// if read character is alphabetic then update its frequency.
if(isalpha(ch))
{
char ch1 = tolower(ch);
arrayray[ch1-'a'].frequecny++;
}
// if '.' is encountered break the while loop.
if(ch=='.') break;
}
// apply selection sory algorithm on array.
selectionSort(arrayray);
cout << " Letter: Number of Occurrences"<< endl;
// now print leter and its frequency.
for(int i=25; i>=0; i--)
{
if(arrayray[i].frequecny==0)
{
break;
}
else
cout << arrayray[i].letter<<" "<<arrayray[i].frequecny<< endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.