find empty inventory slots

I made two dimensional array to represent my (game) inventory.
Slot is empty if its value is 0 and 1 if not.
Now i need to find if i can put new item in my inventory that can take up 4x2 slots. I tried this but it doesn't work as i expected:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include int main() { const int inventorySlotRows = 5; const int inventorySlotColumns = 5; int arr[inventorySlotRows][inventorySlotColumns] = { { 1, 1, 1, 1, 1 }, { 0, 1, 1, 0, 1 }, { 1, 0, 1, 0, 0 }, { 1, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0 } }; // needed empty slots for new item int emptySlotsRows = 4; int emptySlotsColumns = 2; bool have = false; for(int i = 0; i < inventorySlotRows; ++i) { for(int j = 0; j < inventorySlotColumns; ++j) { if(0 == arr[i][j] && (i + emptySlotsRows) <= inventorySlotRows && (j + emptySlotsColumns) <= inventorySlotColumns) { int sum = 0; for(int k = 0; k < emptySlotsRows; ++k) { for(int l = 0; l < emptySlotsColumns; ++l) { sum += arr[i+k][j+l]; } } have = (sum == 0) ? true : false; } } } std::cout << std::boolalpha << have << std::endl; return 0; };``````

How would you do this?

Last edited on
There is no space in the inventory for an item that takes 4 rows and 2 columns, but there is space for an item that takes 2 rows and 4 columns. Your algorithm seems to work fine except that it will not stop when `have` becomes true so what it really does at the moment is to print if the item can be placed in the bottom right corner of the inventory.
Yea, i got all wrong with rows and columns, should be 2x4 to find.
Looks like i need a goto there to break out of loops.
Topic archived. No new replies allowed.