Question
DIY (50%) Except from the mandatory sections of DIY, all the other parts can be changed by students if they find it necessary as long
DIY (50%)
Except from the mandatory sections of DIY, all the other parts can be changed by students if they find it necessary as long as the program produces an output that exactly matches the required output.
Write Bar Chartprogram that receives several samples values and creates a bar chart based on the values received.
ABarChartis a collection of validBars that are drawn horizontally on the screen:
Bar Chart Title ----------------------------------------------------------------------- First sample title..|===== Second Sample.......|============================================= Third Sample........|==================== Fourth..............|======= Fifth...............|========================= And Six.............|=============== -----------------------------------------------------------------------
A sample consists of a title and an integer value between 0 and 100.
TheBarChartis initialized by the title of the survey, the number of the samples (ie. Bars to be drawn) and the fill character to draw the bars.
TheTitle of the chartandthe Barsin theBarChartare to be held dynamically.
Each sample isaddedto the chart by thetitleand thevalueof the sample to be represented by aBarin theBarChart.
Note: The length of the bar will be "thevaluedivided by 2", which means, a value of 10 will be displayed by a bar that is 5 characters long or a value of 41 will be displayed by a bar that is 20 characters long and so on...
When displaying theBarChart, If thenumber of added samplesis not the same asthe number of samples, or if any of theBars inBarChartare invalid, then the Chart will be considered invalid and a proper error message will be displayed when attempted to draw the chart prematurely . (see main and execution sample below)
Here is a sample of the main program using theBarChartClass and its execution result:
#include#include "BarChart.h" using namespace sdds; using namespace std; int main() { BarChart bc; bc.init("How much students like this lab.", 5, '='); bc.add("Best Lab Ever", 10); bc.add("Good Lab", 30); bc.add("Doable Lab", 40); cout << "Premature draw call:"<< endl; bc.draw(); // will not draw; it will print an error instead bc.add("Bad Lab", 15); bc.add("Worst Lab Ever", 5); cout << "Proper draw call after all sampels are entered:" << endl; bc.draw(); bc.deallocate(); return 0; }
Execution sample:
Premature draw call: Invalid Chart! Proper draw call after all sampels are entered: How much students like this lab. ----------------------------------------------------------------------- Best Lab Ever.......|===== Good Lab............|=============== Doable Lab..........|==================== Bad Lab.............|======= Worst Lab Ever......|== -----------------------------------------------------------------------
Files to submit:
chartMain.cpp <--- contains the main function Bar.cpp Bar.h BarChart.cpp BarChart.h
Bar Module
Suggested member variables
- a Cstring to hold maximum of 20 characters for title
- a char variable to hold the fill character
- an integer variable to hold the sample value of the Bar
Suggested public member functions
- a function to set the Bar to the empty state
- a function to set the title, fill character and value of the bar and set the fuction to invalid state if the value is not acceptable (less than zero or more than 100)
- a function that returns "if the Bar is valid or not"
Mandatory public member function
Write a member function calleddrawthat does not accept any parameters and can not modify the bar.
This function should draw a bar with as follows.
For example if thetitleof the bar is "Number of cakes", thefillcharacter is asterisk ('*') and the value is51; the Bar should be drawn exactly like this:
Numberof cakes......|*************************<NEWLINE>
- Print the title in 20 characters and fill the spaces with dot character ('.').
- print a pipe character ('|')
- print the fill character tothe value divided 2(ie. 51/2 -> 25)
If the value of the Bar is invalid (less than zero or more than 100) this function takes no action.
barTester program:
#include "Bar.h" using namespace sdds; int main() { Bar b; for (int i = 1; i <= 5; i++) { b.set("the bar", '=', i*20); b.draw(); } b.set("Bad Bar", '=', -200); b.draw(); b.set("Bad Bar", '=', 200); b.draw(); return 0; }
bar tester output
the bar.............|========== the bar.............|==================== the bar.............|============================== the bar.............|======================================== the bar.............|==================================================
BarChart Module
Mandatory member variables
- acharacter pointerto hold the dynamically allocated "title of the chart"
- aBar pointerto hold the dynamically allocated array ofBars.
Suggested member variables
- acharacter variableto hold the fill character
- aninteger variableto hold the size of the dynamically allocated array ofBars
- aninteger variableto keep track of number of added samples to set theBars.
Suggested member function
- a function to return if all theBars are in a valid state or not.
Mandatory member functions
void init(const char* title, int noOfSampels, char fill);
Initializes the Chart as explained before.
void add(const char* bar_title, int value);
Sets the next availableBar, or does nothing if they are all already set.
void draw()const;
If TheBarCharis valid and if all theBars in the Chart are valid it draws the bar exactly as follows:
- prints the title of the Chart and goes to new line
- prints 71 dashes ('-') and goes to new line
- prints all the Bars
- prints 71 dashes ('-') and goes to new line Otherwise it will print:
Invalid Chart!void deallocate();
Deallocates all the dynamically allocated memory.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started