#beginner
13 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
i don't understand how to make my code do, for example, a cube filled with x's and replace the left side one layer with "o"
I’m assuming you can fill the multi-dimensional with “x” but I don’t understand what you mean exactly by the left side one layer with “o”.
For the left side part you mentioned, if your int i variable is iterating through your rows and int j variable is iterating through the columns you can do an if statement inside the two for loops to check if j < size_of_column / 2.
It would be easier to help you if you copied and pasted the code you’ve written down so far.
!format
Here.
#include <iostream>
const int size = 5; // You can adjust the size of the cube
void drawCube() {
char cube[size][size][size]; // 3D array for the cube
// Fill the cube array with 'x' symbols
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
for (int k = 0; k < size; ++k) {
cube[i][j][k] = 'x';
}
}
}
// Draw the cube
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
for (int k = 0; k < size; ++k) {
std::cout << cube[i][j][k] << ' ';
}
std::cout << std::endl;
}
}
}
int main() {
drawCube();
return 0;
}
what should I do if i want to replace the entire left side with "o"'s instead?
// Draw the cube
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
for (int k = 0; k < size; ++k) {
std::cout << cube[i][j][k] << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
}
}
Adding another newline can help you see the cube. Try running before and after to see the difference
if you add an if statement before the std::cout << cube[i][j][k] and use the information I gave you from the previous message then make the std::cout << cube[i][j][k] an else statement you'll be able to solve it
Try it on your own and if you can't do it I'll give you the code
You'll have to change a few things since I gave you code for 2d array
I don't know if you want it anymore since you haven't responded but here's the code
#include <iostream>
#include <iostream>
const int size = 5; // You can adjust the size of the cube
void drawCube() {
char cube[size][size][size]; // 3D array for the cube
// Fill the cube array with 'x' symbols
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
for (int k = 0; k < size; ++k) {
cube[i][j][k] = 'x';
}
}
}
// Draw the cube
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
for (int k = 0; k < size; ++k) {
if (k < 2)
std::cout << 'o';
else
std::cout << cube[i][j][k];
}
std::cout << std::endl;
}
std::cout << std::endl;
}
}
int main() {
drawCube();
return 0;
}
I would recommend using std::array instead of C style arrays because std::array has bounds checking to prevent potential access violations