Question: My question is can you rewrite the draw_hist method into two separate methods so that it doesn't take an auto type as one of it's

My question is can you rewrite the draw_hist method into two separate methods so that it doesn't take an auto type as one of it's parameters and instead takes a string and an integer? We are not allowed to use auto.

Here is a copy of my program: (everything else compiles fine)

#include

#include

#include

#include

using namespace std;

const int NAME_LEN = 21;

const int NUM_RANKS = 11;

const int SIZE = 4429;

struct Name

{

char name[NAME_LEN];

int rank[NUM_RANKS];

};

//draws histogram out

void draw_histogram(auto graph, int asterisk)

{

cout << graph << " ";

for (int a = 0; a < asterisk; ++a)

{

cout << "*";

}

cout << " ";

};

//Prints histogram out

void print_histogram(char * name, Name * list)

{

for (size_t a = 0; a < SIZE; ++a)

{

string name1{ list[a].name };

string name2{ name };

if (name1 == name2)

{

for (size_t b = 0; b < NUM_RANKS; ++b)

{

int rank = list[a].rank[b];

draw_histogram_integer(1900 + b * 10, ((1000 - rank) / 10) + 1);

draw_histogram_string(1900 + b * 10, ((1000 - rank) / 10) + 1);

}

return;

}

}

cerr << " Name " << name << " not found";

};

// compares two names for a decade

void compare_names(char * name_1, char * name_2, int decade, Name * list)

{

bool twice = 0;

for (size_t a = 0; a < SIZE; ++a)

{

//comparision.

string name{ list[a].name };

string name1{ name_1 };

string name2{ name_2 };

if (name == name1 || name == name2)

{

int b = (decade - 1900) / 10;

int rank = list[a].rank[b];

draw_histogram(name, ((1000 - rank) / 10) + 1);

if (!twice)

{

twice++;

}

else

{

return;

}

}

}

};

//gets names from list

void load_names(Name * list)

{

string line;

ifstream in("names.txt");

size_t a = 0;

int rank = 0;

while (getline(in, line))

{

stringstream ss{ line };

ss >> list[a].name;

for (size_t b = 0; b < NUM_RANKS; ++b)

{

ss >> rank;

list[a].rank[b] = (rank == 0) ? 2000 : rank;

}

++a;

}

};

//main function

int main()

{

Name list[SIZE];

bool done = false;

load_names(list);

char p1[5] = { 'A', 'd', 'a', 'm' };

char p2[6] = { 'A', 'b', 'd', 'u', 'l' };

cout << " Adam popularity over decades: ";

print_histogram(p1, list);

cout << " Comparision of adam vs abdul.";

compare_names(p1, p2, 1900, list);

return 0;

}

Here is a copy of the assignment:

Write a menu-driven C++ application to do the activities shown in the associated example output. All of the activities involve reading the provided file, names.txt, which gives babies' names in the U.S. and their popularity rankings for 11 decades beginning in 1900. In this file a popularity of 0 indicates a ranking that was 1000 or more. Each of the 4429 lines in this file starts with a baby's first name and is followed by eleven (11) integers representing the popularity ranks for that name for the 11 decades from 1900 through 2005. There should be four (4) menu items the user may choose: Print histogram for a name: The histogram line should be longer the more popular that name is. So, a rank of 1 gets the most stars (100) and a rank of 999 gets only one star. Names whose ranks are zero (0) should show no stars since the zero implies a popularity of 1000 or more. You should aim to get 100 stars for rank 1 and 1 star for rank 999. This can be done by accessing a name's rank for a specific decade and plugging it into the expression: (1000 - rank) / 10 + 1. If the entered name is not found in the list of names, then an error is printed and the menu is redisplayed for another user selection. See the example output. Compare two names in a decade: The user should be prompted to enter each of the two names and the decade value (1 through 11). Handling of errors (a name is not found or an invalid entry for decade) is shown in the example output. Print top ten names for a decade: List the top 10 names for a given decade in order of decreasing popularity (or increasing rank: first 1, then 2, then 3, etc.). The user should be prompted for the decade in which to search. NOTE: This will display 20 names since for each popularity rank there are two name. See the example output. Quit (write anomalies): This menu choice causes the application to terminate. However, before terminating, the application must print all anomalies found in names.txt to an output file named anomalies.txt. The file's format is defined to have two names for every decade/rank pair. For example, if examining decade 7 (1960-1969) and the rank, 9, this would yield the names, Michelle and Thomas. Any pair of a decade and a rank should correspond to two names as in the previous example. However, there are anomalies in the file such that some decade/rank pairs have only one name and a few of these pairs have no name at all. Writing the anomalies to an output file means that your program will search the array of names looking for these anomalies and print a list of these problems with the file's format to the output file. As it turns out, there are 1,065 anomalies: in most cases, only one name with a particular rank in a decade or in a few cases, no names in a decade with a particular rank. Your code to do this will need to check each name in each decade for each rank in order to find all 1065 anomalies. The format of the two different kinds of anomalies you will write to the file named, anolamies.txt, follows: 23 - One name(Wilma) for 1900-1909, rank 139. 676 - No names for 1940-1949, rank 779. - Note: Write out one anomaly per line and number each anomaly written. In the two examples above, the first one was the 23rd anomaly found and the second was the 676th anomaly found. You may find them in a different order, but you numbers should go from 1 to 1065. - Also note, the first type of anomaly that the one name found is given in the description of that anomaly, NOTE: Examine the example output with the goal of making your output look like the example output. When the user is asked to enter an integer, assume that the user will not enter characters that are not a number. However, if the number entered is out of range, those errors should be validated. Once again, the example output shows the handling of such errors, including names entered by the user that are not found in the list. Ignore the case of user input; for example, "jOHn", "John", "JOHN", and "john" should all be the same name as far as the program is concerned. Design: Use the following struct to store the data contained on one line of the file, names.txt: const int NAME_LEN = 21; const int NUM_RANKS = 11; . . . struct Name { char name[NAME_LEN]; int rank[NUM_RANKS]; }; The function, loadNames, should be called to read the entire names.txt file and store the data from each line into a struct of type Name. The main function should pass an array of Name structs to loadNames so that it can simply read a line's data into the struct at the first element of this array, then read the second line's data into the second element, etc. This should be done for all 4429lines of the names.txt file. Once loadNames has completed and returned to main, the file, names.txt, must never be read again during this execution of the application. Be sure to use named constants for all numbers like 21, 11, 4429, etc. Once the array of Name structs is populated by the loadNames function, then the function, main, can present the menu and get a selected menu choice from the user. Here is an example of the main program loop: Name list[SIZE]; bool done = false; loadNames( list ); // main app loop do { switch( getMenuSelection() ) { case HISTOGRAM: displayHistogram( list ); break; case COMPARE: compareNames( list ); break; case TOPTEN: displayTopTenNames( list ); break; case EXIT: done = true; break; } // end main app loop } while( !done ); This example uses the constants: const char HISTOGRAM = 'a'; const char COMPARE = 'b'; const char TOPTEN = 'c'; const char EXIT = 'd'; You should setup your main function in a similar fashion. Make sure you do NOT use recursion -- this is when a function either invokes itself or invokes another function that invokes the first one. Use loops to cycle back to the menu presentation after executing the user's choice. In the example above, the array of Name structs is created in main, populated by loadNames, and the passed to each major function in the list of menu choices. Once an individual task is complete, control returns to main where the application continues to offer the menu once again only if the user has not selected the terminate option. You should notice that the Name struct uses a C-String to store the name found on a line of the text file. You must use this struct and the c-string field, name, exactly as defined above. Using a c-strings requires library functions like, strcpy, strcmp, etc. (see textbook). If you want to use other strings in the application, you are welcome to use the data type, string, for these -- but not for the Name stuct's name field. The string type is found in many examples in the textbook. Your grade on this project will depend heavily on your implementation of a modular design. This implies that you use smaller functions to accomplish larger tasks. This means writing functions to do tasks that are repeated, such as searching for a name, changing a name's case, organizing a line of a histogram, etc. This also means creating functions to hold meaningful subdivisions of the work. Examples of these are the functions called for each of the menu cases in the switch statement above. As you organize the structure of your solution, remember: NO recursion allowed, every function should have ONLY one return statement, break is only used in a switch statement (NOT to terminate loops), and continue and exit are never used in this course Again, the file, names.txt, must read the file once only. This would be done by the loadNames function called by main before the main program loop ever begins. Use named constants wisely. Some examples were given above. You will want others. Every function should have a prolog comment that makes very clear what the task of the function is. Be sure the main prolog COMPLETELY and CLEARLY summarizes all the functionality of this application. Finally, use inline comments to make your code simple to read and understand. Again, make your execution resemble the example output as closely as possible.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To rewrite the drawhistogram function into two separate methods so that it no longer takes an auto type as a parameter and instead uses a string and an integer we will break down the original drawhist... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!