patterncppMinor
Finding cost of tile to cover W x H floor
Viewed 0 times
tilefindingcostcoverfloor
Problem
I am new to C++ and programming in general. After reading through my first book, I decided to take on some programming exercises and test what I learned. The exercise is phrased as follows:
Find Cost of Tile to Cover W x H Floor - Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user.
This is the code I wrote for it:
It does what I need it to, I just want to know whether it can be improved upon. This also brings me to question of: How do I self-review my own code? I don't want to have to constantly be asking a review of my code and rather self-assess it myself.
Find Cost of Tile to Cover W x H Floor - Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user.
This is the code I wrote for it:
#include
using namespace std;
int main() {
double StartSquare, cost, width, height; // Starting square meters and cost. Width and height of floor.
cout > cost;
cout > StartSquare;
cout > width;
cout > height;
cout << "You need to cover a total of " << width * height << " square metres.\n";
cout << "The total cost will be: " << (width * height) * cost << endl;
return 0;
}It does what I need it to, I just want to know whether it can be improved upon. This also brings me to question of: How do I self-review my own code? I don't want to have to constantly be asking a review of my code and rather self-assess it myself.
Solution
One of the most powerful techniques you will develop is called 'separation of concerns'. What this means when it boils down is that your program can be split into discrete logical units. Ideally one module (or in the case of c++, class) will do exactly one job without depending on any other.
This has a number of benefits in that the parts can be re-used without needing any knowledge of which parts of the program are using them.
here is your program again, split into essentially three sections, or concerns. Hopefully you will see that although there are more lines of code, the logic in the main() function is easier to follow. Also you can hopefully see that the "input_data" class is an entirely autonomous entity, which could be re-used to gather data for other parts of a bigger program:
I also fixed the small logic error in your calculation, which was easier to see when the calculations were separated from the input/output code.
This has a number of benefits in that the parts can be re-used without needing any knowledge of which parts of the program are using them.
here is your program again, split into essentially three sections, or concerns. Hopefully you will see that although there are more lines of code, the logic in the main() function is easier to follow. Also you can hopefully see that the "input_data" class is an entirely autonomous entity, which could be re-used to gather data for other parts of a bigger program:
I also fixed the small logic error in your calculation, which was easier to see when the calculations were separated from the input/output code.
#include
using namespace std;
double area(double width, double height)
{
return width * height;
}
double cost_to_cover(double area, double area_per_pack, double cost_per_pack)
{
double packs_needed = area / area_per_pack;
double cost = packs_needed * cost_per_pack;
return cost;
}
struct input_data {
double area_per_pack, cost_per_pack, width, height; // Starting square meters and cost. Width and height of floor.
void gather() {
cout > cost_per_pack;
cout > area_per_pack;
cout > width;
cout > height;
}
};
int main() {
// concern 1 - gather data
input_data input;
input.gather();
// concern 2 - perform calculations
double room_area = area(input.width, input.height);
double cover_cost = cost_to_cover(room_area, input.area_per_pack, input.cost_per_pack);
/// concern 3 - output the result
cout << "You need to cover a total of " << room_area << " square metres.\n";
cout << "The total cost will be: " << cover_cost << endl;
return 0;
}Code Snippets
#include <iostream>
using namespace std;
double area(double width, double height)
{
return width * height;
}
double cost_to_cover(double area, double area_per_pack, double cost_per_pack)
{
double packs_needed = area / area_per_pack;
double cost = packs_needed * cost_per_pack;
return cost;
}
struct input_data {
double area_per_pack, cost_per_pack, width, height; // Starting square meters and cost. Width and height of floor.
void gather() {
cout << "Enter value of 1 pack of tiles: ";
cin >> cost_per_pack;
cout << "Now enter the square metres covered by the pack: ";
cin >> area_per_pack;
cout << "What is the width of your room? ";
cin >> width;
cout << "Now enter the height:\n";
cin >> height;
}
};
int main() {
// concern 1 - gather data
input_data input;
input.gather();
// concern 2 - perform calculations
double room_area = area(input.width, input.height);
double cover_cost = cost_to_cover(room_area, input.area_per_pack, input.cost_per_pack);
/// concern 3 - output the result
cout << "You need to cover a total of " << room_area << " square metres.\n";
cout << "The total cost will be: " << cover_cost << endl;
return 0;
}Context
StackExchange Code Review Q#96976, answer score: 6
Revisions (0)
No revisions yet.