Alter the attached code so that the bar chart can be up to 25 categories, and can be sorted ascending or descending. In this assignment an array of Category structs are used to store both the category name (label) and the category value. The Category struct is provided below along with changes to the prototypes from assignment 3. The get_longest_category_name function has been removed because after sorting, the last or first element in the cats array will be the longest length label. Use this information in create_bar_chart and the asc Boolean value where true is sort ascending and false is sort descending.
Alter the attached code so that the bar chart can be up to 25 categories, and can be sorted ascending or descending. In this assignment an array of Category structs are used to store both the category name (label) and the category value. The Category struct is provided below along with changes to the prototypes from assignment 3. The get_longest_category_name function has been removed because after sorting, the last or first element in the cats array will be the longest length label. Use this information in create_bar_chart and the asc Boolean value where true is sort ascending and false is sort descending.
struct Category
{
std::string label;
double value;
};
- Update the attached code to include the following in global scope:
#define CATEGORIES 25
int num_categories = 5;
- Change the attached code's functions to the following:
//Ask user how many categories, up to a max of 25
int how_many_categories();
void get_category(Category cats[CATEGORIES]);
void get_values(Category cats[CATEGORIES]);
double compute_total(Category cats[CATEGORIES]);
//
// sort the cats array of Category structs
// do not use selection sort or STL's sort
// use: bubblesort, insertion-sort, etc.
void sort(Category cats[CATEGORIES], bool asc=true);
// can get the longest_category_length from the sorted cats array
void create_bar_chart(Category categories[CATEGORIES], double total, bool asc=true);
- Sample main for new code
int main()
{
double total{ 0.0 };
bool asc = true;
while (true) {
std::cout << "Enter 1 or 0 for ascending (1) or descending order (0)?";
std::cin >> asc;
while (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cout << "Enter 1 or 0 for ascending (1) or descending order (0)?";
std::cin >> asc;
}
num_categories = how_many_categories();
get_category(cats);
get_values(cats);
total = compute_total(cats);
//sort categories, if ascending last element for longest, first for descending
sort(cats, num_categories, asc);
std::cout << "\nCategories as a Percentage of the Total (category_amount/" << total << ")\n";
std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
create_bar_chart(cats, total, asc);
std::cout << "\nEnter y to create another bar chart, or any other key to exit : ";
std::string response; std::cin >> response; std::cout << "\n";
if (response != "y")
break;
total = 0.0;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images