I got an assignment for programming my C++ programming class that wants user input of an L-shape and its size. The user enters l1, l2, l3 and l4. Then the user enters a tile size.
This tile is then supposed to fill out the L-shape, also taking into account free space that isn’t filled up and using partial tiles (not full tiles) to fill the entire L-shape.
I gave it a shot, came close to it but always got the same problem, a few partial tiles missing (we got a few example outputs from the teacher).
I’ve talked to classmates, tried finding the issue with chatgpt even, nothing seems to work.
This is the code i’ve got:
#include <iostream>
#include <cmath>
#include <iomanip>
int main() {
// Main rectangle
int l1, l2, l3, l4;
std::cout << "Enter wall lenght measurements (in cm): ";
std::cin >> l1 >> l2 >> l3 >> l4;
// Tile size
int tile;
std::cout << "Enter the square tiles size (in cm): ";
std::cin >> tile;
// Error handle for when l3 or l4 are larger than l2 or l1, since this would make a different shape. Also if the tile size is smaller than 1 cm
if ((l4 > l1) || (l3 > l2)) {
std::cout << "n“The tiles are not appropriate for the wall to be paved!!n";
exit(0);
}
// Top rectangle
int upper_rec_full_tiles = (l1 / tile) * (l3 / tile);
int upper_rec_partial_tiles = 0;
if (l1 % tile != 0) {
upper_rec_partial_tiles += (l3 / tile);
if (l3 % tile != 0) {
upper_rec_partial_tiles += 1;
}
}
if (l3 % tile != 0) {
upper_rec_partial_tiles += (l1 / tile);
}
// Bottom rectangle
int height_lower_rectangle = l2 - l3;
int lower_rec_full_tiles = (l4 / tile) * (height_lower_rectangle / tile);
int lower_rec_partial_tiles = 0;
if (l4 % tile != 0) {
lower_rec_partial_tiles += height_lower_rectangle / tile;
}
int total_full_tiles = upper_rec_full_tiles + lower_rec_full_tiles;
int total_partial_tiles = upper_rec_partial_tiles + lower_rec_partial_tiles;
std::cout << "Numer of tiles needed = " << total_full_tiles + total_partial_tiles << "nNumber of tiles partially used: " << total_partial_tiles;
nils is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2