I am currently working on a dungeon crawl game where the user can move throughout a maze on the screen. I decided to use a 2d array for the maze. One problem, I have a function to print the maze although its not working. I want it to print all four of the rows (there are supposed to be 4 0's per row) but it only prints 4 0's in a single line.
int maze[4][4] = {(0,0,0,0),
(0,0,0,0),
(0,0,0,0),
(0,0,0,0)};
for (int i = 0; i < 4; i++)
{
cout <<maze[i][i];
}
You need two loops, one nested inside the other.
One to print the rows.
One to print each column in the current row.
You need nested loop for displaying 2D array.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout<
Try this.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout <<maze[i][j];
}
cout << "\n";
}
Related
I am currently using C++ on a program called CodeZinger for one of my classes. I was asked to make a program that will output an array with input that the program gives me.
See screenshot below.
The issue is that the program outputs an extra space at the end of my array, which is making the program say that I have not gotten the question right.
#include <iostream>
using namespace std;
int main()
{
int rows = 1;
int cols = 1;
long int arr[100][100];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
My output (see screenshot above) is showing that my code is right and it was going well, except for that extra space at the end. Is there any way to remove that extra space without adding an if statement into my code somewhere?
Have the inner loop iterate until j < cols - 1 and then write one more output line after it ends, without a space (e.g.: std::cout << arr[i][cols-1];) –
UnholySheep
I want to make a 2 dimensional array of 13 rows and 6 columns all initialized with character X. to do so I'm doing this.
char myseats[13][5];
for (int i; i < 13; i++)
{
myseats[i] = { 'X' };
for (int j; j < 5; j++)
{
myseats[j] = {'X'};
}
}
}
however this gives me a error and the array wont initialize with X, same is the case with string. How can I achieve my purpose can anyone please help me?
If you want to access fields of two dimensional array, you should use myseats[i][j] (first you specify row, then the column). Also, your for loops are written incorrectly. When you initialize any variable in the body of for loop, you should assign its value (otherwise i and j can have any value that can be saved in int). Your code should look like that:
char myseats[13][5];
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 5; j++)
{
myseats[i][j] = {'X'};
}
}
Try this:
char myseats[13][5];
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 5; j++)
{
myseats[i][j] = 'X';
}
}
First, the i and j where not initialized in the loop. You need to define at what index you want them to start.
Second, the correct way to access a two dimensional array is with the syntax array[row_index][column_index] = 'your_desired_value_here'.
I have a 2D array that I want to assign the values of another array. I'm making a game of life simulator and have everything else working but this. My code is this:
for(int i = 0; i < ROWS; i++) {
for (int j = 0; i < COLS; j++) {
current[i][j] = next[i][j];
}
}
current and next are both bool's. I keep getting the error code EXC_BAD_ACCESS in X-Code. I'm unsure what I'm doing wrong
To re-iterate my comment, the innerloop compares i with COL rather than j. So
for(int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
current[i][j] = next[i][j];
}
}
should solve the issue
I have a function called zero_row. This function inserts the value of zero into all a row that is specified. The function takes to variables. a (the array) and row (the row in the array). Here is my function
void zero_row (int a [4][5], int row){
for (int i = 0; i < 4; i++) {
a[i][j] = 0;
}
}
I know how to set values of the entire array to zero. As i have a function to do this as well.
void zero_all (int a [4][5]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
my_arr[i][j] = 0;
}
}
}
I cant seem to figure out how to do this using the variable row. I want to use this variable so I can later change the row in my main function, like so: zero_row(a, 3);. Can anyone help?
Thanks!
In your case the first index corresponds to a row and the second index corresponds to a column.
Use
void zero_row (int a [4][5], int row){
for (int j = 0; j < 5; j++) { // 5 instead of 4
a[row][j] = 0;
}
}
The question is to write a function that returns a bounding rectangle for a set of points in a two dimensional plane. SIZE is two. I know the points will come in this format {double, double} and I know how to create the bounding rectangle. I can't seem to grab the points though. I tried iterating like this.
Rectangle2D getRectangle(const double points[][SIZE], int s) {
for (int i = 0; i < s; i++) {
for (int j = 0; j < SIZE; j++) {
cout << points[s][SIZE] << endl;
}
}
// will put these points in after i figure out the iteration.
Rectangle2D rekt(x, y, width, height);
return rekt;
}
You are accessing the same element element every time, because s and SIZE remain constant. You have to access it like this points[i][j] .
And I'm not sure, but i think you can't pass SIZE within the array argument, you should pass it as an additional parameter.
Good luck ;)
Here you go.
for (int i = 0; i < s; i++) {
for (int j = 0; j < SIZE; j++) {
cout << points[i][j] << endl; //observe i,j
}
}
In above case you are iterating row-wise. If you want to iterate column-wise then following will work.
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < s; i++) {
cout << points[i][j] << endl; //observe i,j
}
}