Filling array from file won't work - c++

I have made a 'Map' array and I am attempting to populate it from a 'map' file. On creation I assign the value '0' to each element of the array but the 'Map' file contains the following:
MAP:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I load the map using 'loadMap()'
loadMap():
void room::loadMap()
{
int x=0;
int y=0;
string line;
ifstream mapFile(NAME + "_MAP.txt");
while(!mapFile.eof())
{
for(int i=0; i<cellsY; i++)
{
getline(mapFile,line,'\n');
for(int j=0; j<cellsX; j++)
{
getline(mapFile,line,' ');
map[(cellsX*j) + cellsY] = atoi(line.c_str());
};
};
}
y = 10;
x = 15;
for(int i=0; i<y; i++)
{
cout << endl;
for(int j=0; j<x; j++)
{
cout << map[(x*j) + y];
};
};
}
In this example the elements are still assigned to '0', but I am trying to mimic the Map files layout.

For starters, you never test that any of the input works, nor
that the open succeeded. Then, in the outer loop, you read
a line from the file, and throw it away, before reading further
in the inner loop. And your calcule of the index is wrong.
What you're probably looking for is something like:
std::ifstream mapFile(...);
if ( !mapFile.is_open() ) {
// Error handling...
}
for ( int i = 0; mapFile && i != cellsY; ++ i ) {
std::string line;
if ( std::getline( mapFile, line ) ) {
std::istringstream text( line );
for ( int j = 0; j != cellsX && text >> map[cells X * i + j]; ++ j ) {
}
if ( j != cellsX ) {
// Error: missing elements in line
}
text >> std::ws;
if ( text && text.get() != EOF ) {
// Error: garbage at end of line
}
}
}
For the error handling, the simplest is just to output an
appropriate error message and continue, noting the error so you
can return some sort of error code at the end.

I think this is what you are looking for.
void room::loadMap()
{
int x=0;
int y=0;
ifstream mapFile(NAME + "_MAP.txt");
for(int i=0; i<cellsY; i++)
{
for(int j=0; j<cellsX; j++)
{
int v;
mapFile >> v;
if ( mapFile.eof() )
{
break;
}
map[(cellsX*j) + cellsY] = v;
}
}
y = 10;
x = 15;
for(int i=0; i<y; i++)
{
cout << endl;
for(int j=0; j<x; j++)
{
cout << map[(x*j) + y];
};
};
}

Related

Loading a 2D array from an input file into a Function

I am having trouble loading a 10x10 array from an input file and storing it into an array. I have written this so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void LoadImage(const string imagefile, int image[MAXROWS][MAXCOLS]) //Function to load in image
{
ifstream inputs;
int i,j;
inputs.open(imagefile.c_str());
getline(inputs, imagefile[i][j]);
inputs.ignore(10000,'\n');
if (inputs.is_open())
{
for( i=0; i < MAXROWS; i++ )
{
for ( j=0; i < MAXCOLS; j++ )
{
inputs >> image[i][j];
}
}
}
inputs.close();
}
The void LoadImage function and was given to me with those specific parameters to use or the main function will not execute.
An example of an input file:
#Sample Image--1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Where I have to get rid of the header of the input file before building the array.
If I compile what I have now I get the "error: invalid types ‘const char[int]’ for array subscript
getline(inputs, imagefile[i][j]);"
I understand why I am getting the error, but I do not know how to fix it.
I appreciate any help I can get!
The code is generally OK, just following the comments and fixing a few small mistakes should work.
Note specially the terminating condition for the following loop:
for ( j=0; i < MAXCOLS; j++ )
Should be instead:
for ( j=0; j < MAXCOLS; j++ )
This is the reason why you're getting an infinite loop.
Here's the complete code:
#include <iostream>
#include <fstream>
#include <string>
#define MAXROWS 10
#define MAXCOLS 10
using namespace std;
void LoadImage(const string imagefile, int image[MAXROWS][MAXCOLS]) //Function to load in image
{
ifstream inputs;
int i,j;
inputs.open(imagefile.c_str());
if (inputs.is_open())
{
for( i=0; i < MAXROWS; i++)
{
for ( j=0; j < MAXCOLS; j ++)
{
std::string str;
inputs >> image[i][j];
}
}
}
inputs.close();
}
void PrintImage(int image[MAXROWS][MAXCOLS])
{
int i,j;
for(i = 0; i < MAXROWS; i++)
{
for (j = 0; j < MAXCOLS; j ++)
{
cout << image[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int image[MAXROWS][MAXCOLS] = {0,};
LoadImage ("img.mtx", image);
PrintImage (image);
}
And testing:
$ cat img.mtx
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
$ g++ main.cpp && ./a.out
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
The line
getline(inputs, imagefile[i][j]);
does not make sense.
If you want to ignore the first line of the input file, then you should simply use inputs.ignore, as you are already doing afterwards. So you can simply delete the line which calls getline.
Another problem is that the line
for ( j=0; i < MAXCOLS; j++ )
should probably be
for ( j=0; j < MAXCOLS; j++ )
i.e. you wrote i instead of j.

How to make quadtree algorithm

i'm making an algorithm called Quadtree, so i've tried this code that i made but the output is incorrect
input
8 8
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 1 1 1 1 1
0 0 1 1 1 1 1 1
0 0 1 1 1 1 0 0
0 0 1 1 1 0 0 0
output
0
the code i've tried
#include <iostream>
#include <string>
using namespace std;
int m[128][128];
int nJawaban;
string jawaban[128*128];
bool homogen(int r, int c, int k) {
int val = m[r][c];
for (int i = r; i < r+k; i++) {
for (int j = c; j < c+k; j++) {
if (m[i][j] != val) {
return false;
}
}
}
return true;
}
// i think it's here
void quadtree(int r, int c, int k, string s){
if (m[r][c] == m[r-1][c-1]) {
if (m[r][c] == 1) {
jawaban[nJawaban] = "1" + s;
nJawaban++;
}
} else {
quadtree(r, c, k/2, s+"0");
quadtree(r, c+(k/2), k/2, s+"1");
quadtree(r, c, r+k-1, s+"0");
quadtree(r, c, r+k-1, s+"1");
}
}
int main(){
int r, c;
scanf("%d %d", &r, &c);
int maxRc = max(r, c);
int pow2 = 1;
while (pow2 < maxRc){
pow2 *= 2;
}
for (int i = 0; i < pow2; i++) {
for (int j = 0; j < pow2; j++) {
m[i][j] = 0;
}
}
for (int i = 0; i < r; i++){
for (int j = 0; j < c; j++){
scanf("%d", &m[i][j]);
}
}
nJawaban = 0;
quadtree(0, 0, pow2, "");
printf("%d\n", nJawaban);
for (int i = 0; i < nJawaban; i++) {
printf("%s\n", jawaban[i].c_str());
}
return 0;
}
input:
8 8
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 1 1 1 1 1
0 0 1 1 1 1 1 1
0 0 1 1 1 1 0 0
0 0 1 1 1 0 0 0
expected output:
11
112
113
1211
1212
1213
123
130
131
1320
1321
1322
note: some of the var name is written in indonesian language and i'm trying to make this code without (using namespace std) and also fix the code
i've watched youtube video and reading some stackoverflow answer
https://www.youtube.com/watch?v=xFcQaig5Z2A
Efficient (and well explained) implementation of a Quadtree for 2D collision detection
nevertheless i can't correct my mistake but i can point out where the mistake is. My question here is how to correct this mistake or even provide better algorithm
edit: it's still didn't work

What is a better way to store both shortest path and its length in a Maze?

I am using Lee's algorithm to find the shortest path inside a maze.
The maze consists of 0s and 1s. A cell with value 1 represents an empty space one can go to, a cell with value 0 is an obstacle one cannot pass.
However, by doing the q.pop() part inside the while(!q.isEmpty()) loop, I am loosing my "nodes"(I have implemented a strucutre pozitie_plan that stores the index of the row, the index of the column and the distance between an arbitrary-chosen point in a matrix and itself). These "nodes" could be part of the path that I want to output in the end. Yet, I think I should not be altering that part.
I am restricted to use the Queue on DLLA (Dynamic Linked List on Array). I have implemented my own. If needed, I will add its implementation, too.
The way I am finding the path is by determining the shortest length using Lee's algorithm and leaving a trace on an auxiliary matrix, the value in an (i, j) cell representing the distance from the source to that cell.
Finally, I am tracing back a path(there could be more than one, I believe) starting from the exit of the maze (provided the exit has been found) to the starting position, by searching for those cells that have the value equal to: the value in the current cell - 1.
I am keeping these pozitie_plan nodes in a queue, then reverting the queue in the end and outputing the final result.
I feel this could be done better. But how ?
Here is my code. If asked for, I could edit the post and change the code I posted to a version that has some couts and more commentaries and / or the implementation for the Queue.
main.cpp
#include <iostream>
#include <vector>
#include "Queue.h"
#include "ShortTest.h"
#include "ExtendedTest.h"
#define Nmax 25
using std::cin;
using std::cout;
using std::vector;
using std::swap;
#include <fstream>
using std::ifstream;
using std::ofstream;
template<typename T>
void citeste_mat(ifstream &input_stream, T mat[][Nmax], int &n, int &m)
{
input_stream >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
input_stream >> mat[i][j];
}
void citeste_poz_start(ifstream &input_stream, pozitie_plan &p)
{
input_stream >> p.i >> p.j;
p.dist = 0;
p.i--; p.j--; // utilizatorul vede in indexare de la 1, dar eu am implementat in indexare de la 0!
}
template<typename V>
void citeste_date(ifstream &input_file, V mat[][Nmax], int &n, int &m, pozitie_plan &p)
{
citeste_mat<V>(input_file, mat, n, m);
citeste_poz_start(input_file, p);
}
template<typename U> // ar fi fost problema daca puneam aici tot "T" in loc de "U" ?
void afis_mat(U mat[][Nmax], int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
cout << mat[i][j] << " ";
cout << '\n';
}
cout << '\n';
}
bool valid_pozitie( int i, int j, int n, int m)
{
return 0 <= i && i < n && 0 <= j && j < m;
}
template<typename T>
bool obstacol(T mat[][Nmax], pozitie_plan p)
{
return mat[p.i][p.j] == 0;
}
template<typename T>
bool valid_nevizitat(T aux[][Nmax], pozitie_plan p)
{
return aux[p.i][p.j] == false;
}
template<typename T, typename U>
bool valid(T mat[][Nmax], U aux[][Nmax], pozitie_plan p, int n, int m)
{
return valid_pozitie(p.i, p.j, n, m) && !obstacol<T>(mat, p) && valid_nevizitat<U>(aux, p);
}
// e un pic "overhead" sa am toate functiile astea de valid, stiu; dar parca imi place sa am totul cat se poate de fragmentat
bool pe_contur(pozitie_plan p, int n, int m)
{
// return p.i == 0 || p.j == 0 || p.i == n - 1 || p.j == m - 1;
return p.i * p.j == 0 || p.i == n-1 || p.j == m-1;
}
template<typename T>
bool solution(T mat[][Nmax], pozitie_plan p , int n, int m)
{
return !obstacol(mat, p) && pe_contur(p, n, m);
}
template<typename T>
pozitie_plan gaseste_cale(T mat[][Nmax], int n, int m, pozitie_plan ps)
{
// voi pastra integritatea matricii ( mat nu va fi modificata )
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, 1, 0, -1};
bool visited[Nmax][Nmax] = { false };
visited[ps.i][ps.j] = true; // marchez pozitia de start ca vizitata
Queue q;
q.push(ps); // bag in coada pozitia de start
pozitie_plan current_pos;
int row, column, distance;
while (!q.isEmpty())
// cat timp exista in coada pozitii pentru care trebuie verificati vecinii (daca vecinii sunt iesire sau nu)
{
current_pos = q.top();
if (solution<int>(mat, current_pos, n, m))
{
return current_pos; // voi returna prima solutie gasita, care garantat va fi cea mai scurta (BFS)
}
q.pop(); // stocand head ul in `current_pos` pot spune ca am tratat ce aveam in head, asa cu fac pop
for (int k = 0; k < 4; k++)
{
row = current_pos.i + dx[k];
column = current_pos.j + dy[k];
distance = current_pos.dist + 1;
pozitie_plan vecin{ row, column, distance };
if (valid<int,bool>(mat, visited, vecin, n, m))
{
mat[row][column] = distance;
visited[row][column] = true;
q.push(vecin);
}
}
}
return NULL_POZITIE_PLAN;
}
void reconstruct_path(int mat[][Nmax], int n, int m, pozitie_plan end, pozitie_plan begin, Queue& q)
{
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, 1, 0, -1 };
q.push(end);
pozitie_plan current_pos = end;
int row, column, distance;
int len = current_pos.dist;
while(len != 1)
{
for (int k = 0; k < 4; k++)
{
row = current_pos.i + dx[k];
column = current_pos.j + dy[k];
distance = mat[row][column];
if (valid_pozitie(row, column, n, m)
&& distance == len - 1)
{
pozitie_plan new_pos = pozitie_plan{ row, column, distance };
q.push(new_pos);
current_pos = new_pos;
break;
}
}
len--;
}
q.push(begin);
}
void reverse_queue(Queue& q, const int len)
{
pozitie_plan *aux = new pozitie_plan[len];
for (int i = 0; i < len; i++)
aux[i] = q.pop();
for (int i = 0; i < len / 2; i++)
swap(aux[i], aux[len - 1 - i]);
for (int i = 0; i <len ; i++)
q.push(aux[i]);
}
int main()
{
int mat[Nmax][Nmax] = { 0 };
int n, m;
pozitie_plan pozitie_start;
ifstream input_file;
input_file.open("input.txt", ios::in);
citeste_date<int>(input_file, mat, n, m, pozitie_start);
input_file.close();
afis_mat<int>(mat, n, m);
// pana aici citesc datele de intrare
pozitie_plan end = gaseste_cale<int>(mat, n, m, pozitie_start); // presupun ca utilizatorul numara liniile & coloanele de la 1
if (end == NULL_POZITIE_PLAN)
{
cout << "NO SOLUTION FOUND!\n";
}
else
{
// Queue cale = reconstruct_path(mat, n, m, end);
Queue cale;
reconstruct_path(mat, n, m, end, pozitie_start, cale);
reverse_queue(cale, end.dist + 1);
cout << "The shortest path (length = " << end.dist << " not including the starting position) to be followed in the given matrix (above) is:\n";
cale.toString();
}
return 0;
}
Example
input.txt
18 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1
0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
17 3
console output
The shortest path...// everything is ok, the path is ok

Does row and column of 2D arrays start at 0?

Does int myarray[7][7] not create a box with 8x8 locations of 0-7 rows and columns in C++?
When I run:
int board[7][7] = {0};
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
I get output:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 146858616 1 0 0 146858832 1 1978920048
So the 8 columns seem to work, but not the 8 rows.
If I change it to int board[8][7] = {0}; it works on mac CodeRunner IDE, but on linux Codeblocks I get:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 1503452472
Not sure what's going on here.
Two dimensional arrays are not different to the one dimensional ones in this regard: Just as
int a[7];
can be indexed from 0 to 6,
int a2[7][7];
can be indexed from 0 to 6 in both dimensions, index 7 is out of bounds. In particular: a2 has 7 columns and rows, not 8.
int board[7][7]; will only allocate 7x7, not 8x8. When it's allocated, you specify how many, but indexes start at 0 and run to the size - 1.
So based on your source, I would say you really want int board[8][8].
int board[7][7] = {0}; creates a 7x7 array. You are going out of bounds in your loop. Change it to int board[8][8] = {0};
int board[8][7] = {0};
When you do as above, you created only 8 rows and 7 columns.
So your loop condition should be as follows:
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 7; j++)
{
If you try as follows system will print garbage values from 8th columns
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Araay starts from zero means that it will have n-1 elements not n+1 elements
try
int a[8][8] = {}
i = 0
j = 0
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
a[i][j] = 0;
}
}

Showing 2D array with one element at the time

I have a 2 dimensional array filled with 0s and 1s. I have to display that array in way that:
- 0s are always shown
- 1s are shown one at the time.
It suppose to look like a maze where 0 is a wall and 1 is a current position.
How can I do that in c++?
EDIT:
I came up with a solution but maybe there is simpler one. What if I'd create copy of my _array and copy 0s and blank spaces instead of 1s to it. Then in loop I'd assign one of _array "1" to second array then display whole array and then make swap 1 back with blank space?
EDIT2:
int _tmain(int argc, _TCHAR* argv[])
{
file();
int k=0,l=0;
for(int i=0;i<num_rows;i++)
{
for(int j=0;j<num_chars;j++)
{
if(_array[i][j] == 1)
{
k=i;
l=j;
break;
}
}
}
while(1)
{
for(int i=0;i<num_rows;i++)
{
for(int j=0;j<num_chars;j++)
{
if(_array[i][j] == 0) printf("%d",_array[i][j]);
else if(_array[i][j]==1)
{
if(k==i && l==j)
{
printf("1");
}
else printf(" ");
}
l++;
if(l>num_chars) break;
}
k++;
l=0;
printf("\n");
}
k=0;
system("cls");
}
return 0;
}
I wrote something like that but still i don't know how to clear screen in right moment. Function file() reads from file to 2D array.
Assuming you want something like that
000000
0 0
0000 0
0 1 0
0 0000
000000
You could print a 0 whenever it occurs and a blank space if not. To handle the current position you could use two additional variables like posX, posY. Now everytime you find a 1 in your array you check if (j == posX && i = posY) and print 1 if so...
As you just need to visualize the maze at different possible positions I'd propose a simple display function. DisplayMaze(int x, int y) is printing the maze in the required format to the screen. If _array[y][x] == 1 there is also printed a single 1...
void DisplayMaze(int x, int y)
{
for (int row = 0; row < num_rows; row++)
{
for (int col = 0; col < num_chars; col++)
{
if (_array[row][col] == 0)
std::cout << "0 ";
else if (row == y && col == x)
std::cout << "1 ";
else
std::cout << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
In order to display all possible positions you have to iterate over all of them and check if the current position is marked with 1 in the array (otherwise displaying would't make sense)
for (int y = 0; y < num_rows; y++)
{
for (int x = 0; x < num_chars; x++)
{
if (_array[y][x] == 1)
{
DisplayMaze(x, y);
}
}
}
The output should look like:
0 0 0 0 0 0
0 1 0
0 0 0 0 0
0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 1 0
0 0 0 0 0
0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 1 0
0 0 0 0 0
0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 1 0
0 0 0 0 0
0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0
0 0 0 0 1 0
0 0
0 0 0 0 0
0 0 0 0 0 0
...
However, i'd recommend a more C++ like approach as a maze could be implemented as a class. This class could bring it's own display-method and would encapsulate the internal data. It could basically look like:
class Maze
{
public:
// generate empty maze with given size
Maze(int width, int height);
// destructor
~Maze();
// print maze if the given position is marked with 1
void printPosition(int x, int y) const;
// takes a cstring as input to initialize the maze from
Maze& operator<<(const char* input);
// returns true if the given position is marked with 1
bool isValidPosition(int x, int y) const;
private:
// this is the actual representation of the maze
std::vector<std::vector<int> > grid_;
};
it would be used as followes:
Maze myMaze(num_chars, num_rows);
myMaze << "000000"
"011110"
"000010"
"011110"
"010000"
"000000";
for (int y = 0; y < num_rows; y++)
{
for (int x = 0; x < num_chars; x++)
{
if (myMaze.isValidPosition(x,y))
{
myMaze.printPosition(x,y);
}
}
}
hire you go*[solved]*
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
char map[x][y];
memset(map, 'a', sizeof(map));
int y_pos = 0;
for (int x_pos = 0; x_pos < x * y; x_pos++){
if (x_pos == x){
x_pos = 0;
y_pos = y_pos + 1;
cout<<endl;
}
if (y_pos == y){
system("pause");
return 0;
}
cout<<map[x_pos][y_pos];
}