DWK~~Thx so much
Have reduced the code replaced by yours haha.. I am so stupid ~ ~
Anyway can you give me a little bit more hint how to place the 1's??
thanks thanks
This is a discussion on MAZE Problem within the C++ Programming forums, part of the General Programming Boards category; DWK~~Thx so much Have reduced the code replaced by yours haha.. I am so stupid ~ ~ Anyway can you ...
DWK~~Thx so much
Have reduced the code replaced by yours haha.. I am so stupid ~ ~
Anyway can you give me a little bit more hint how to place the 1's??
thanks thanks
still getting error and i am not sure is this correct as well
can any1 can tell m what's gone wrong????Code:int run; char filename[16]; FILE *filep; char *nodep; int row, col, next_row, next_col, heading, steps, pellets; enum {NORTH, EAST, SOUTH, WEST}; ////create map 32 X 32 with all zeros//// char matrix[MATRIX_SIZE][MATRIX_SIZE]; char matrix_data[MATRIX_SIZE][MATRIX_SIZE] = {{0}}; ////created the map with zeros//// ///////////////////////////now fill 1's into the field///////////////////////// ///////////////////////////////////////////////////////////////////////// ///////////test///////// int main() { int x; int c; srand(()%1024); for(x=0; x<100; x++) { int n = 100 + rand() % 300; printf("%d\n", 1); for (c = 0; c < 300; c++) matrix_data[rand()%MATRIX_SIZE][rand()%MATRIX_SIZE] = 1; } } ///////////////////////// char trace_matrix[MATRIX_SIZE][MATRIX_SIZE]; void output_program(struct prog *progp) { char *codep = progp->code; char token; while (token = *codep++) fputs(token_table[token].name, stdout); putchar('\n'); } void print_matrix(void) { int i, j; char symbs[] = " OX"; for (i=0; i < MATRIX_SIZE; i++) { for (j=0; j < MATRIX_SIZE; j++) putchar(symbs[trace_matrix[i][j]]); putchar('\n'); } }
Hi the code is working now.
Code:////create array map 32 X 32 (1024 steps) with all zeros and trace map//// int matrix[MATRIX_SIZE][MATRIX_SIZE]; int matrix_data[MATRIX_SIZE][MATRIX_SIZE] = {{0}}; int trace_matrix[MATRIX_SIZE][MATRIX_SIZE]; ///////////////////////////////////////////char to int////////////// void output_program(struct prog *progp) { char *codep = progp->code; int token; while (token = *codep++) fputs(token_table[token].name, stdout); putchar('\n'); } int main() { int i; time_t start; seed = time(NULL); time(&start);
Now the program produce a random map. But how can i ensure food do not overlap??
In addition, is it able to create 1 random map and 2 maps to allow user to choose? and how can I implement it?
thanks
Watch carefully; there may be a test afterwards:
Code:for (c = 0; c < 300; c++) { int x = rand()%MATRIX_SIZE; int y = rand()%MATRIX_SIZE; //our x- and y-coordinates matrix_data[x][y]++; // add 1 to our randomly chosen spot if (matrix_data[x][y] > 1) { // oops! too much! matrix_data[x][y] = 1; //reset food c--; //take away from the count of food } }
I cant belive you ability so fast......
It runs perfet!!!!
You know the array I have creted... can you hint me why it kept error pleasE??
Code:int matrix[MATRIX_SIZE][MATRIX_SIZE]; int matrix_data[4][MATRIX_SIZE][MATRIX_SIZE]; { cout <<"Ender Maze wanted:"; cin >> x; cout <<"Maze Choosen is: " << x << endl <<endl; matrix_data[0][MATRIX_SIZE][MATRIX_SIZE] = {{0}}; matrix_data[1][MATRIX_SIZE][MATRIX_SIZE]= {{1}}; // //--------------------------------------------------------------// // //--------------------------------------------------------------// return matrix_data[0]; } int trace_matrix[MATRIX_SIZE][MATRIX_SIZE];
in the main
I think I am completely wrong in the int main()Code:int main() { int n; for (n=0; n<4; n++); //result += matrix_data[n]; printf("%d\n", matrix_data[n]); //std::cout<< matrix_data <<std::enl; //cout << result return 0;
wt statement i need to read the array and put it back?
Thanks for you Great Help X 10000000000
You can only use an array initializer when you declare it, so this line:
(and the one after it) are errors.Code:matrix_data[0][MATRIX_SIZE][MATRIX_SIZE] = {{0}};
To initialize a matrix, you need to run a double for-loop. So, for instance, to initialize matrix_data[0], we would run a for-loop on row, and inside that a for-loop on col, and inside that we would set matrix_data[0][row][col].
Alternatively, you could do one big initializer as so:
It will be big, but if you've got them already you can copy'n'paste.Code:matrix_data[4][MATRIX_SIZE][MATRIX_SIZE] = { {{0}}, //this is matrix_data[0], the random one {{0,1,1,0,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0}, //this is first row of [1] { ...stuff ...}, //the second row of [1] { et cetera }, { and so on}} //end of [1] {{more numbers}, //first row of [2] . . .
These are what I have mdify:
Like you said it's gonna be huge if 3-4 maps existed. So ihave only include 1 random map and 1 pre-defined map.Code:#include <iostream.h> #include <fstream.h> #include <iomanip.h> ////create array map 32 X 32 (1024 steps) with all zeros and trace map//// int matrix[MATRIX_SIZE][MATRIX_SIZE]; int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE]; { cout <<"Ender Maze wanted, 0,1,2,3:\n"; cout <<" 0- Random Maze\n"; cout <<" 1- Set Maze (1)\n"; cout <<" 2- Exit\n"; cin >> x; cout <<"Maze Choosen is: " << x << endl <<endl; matrix_data[0][MATRIX_SIZE][MATRIX_SIZE] = {{0}}; matrix_data[1][MATRIX_SIZE][MATRIX_SIZE] = { {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,1}, {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,1,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,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,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,1,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,1,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,1,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,1,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,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,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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,1,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,1,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,1,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,1,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,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,1,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,1} }; return matrix_data[0]; } int trace_matrix[MATRIX_SIZE][MATRIX_SIZE]; ///////////////////////////////////////////char to int////////////// void output_program(struct prog *progp) { char *codep = progp->code; int token; while (token = *codep++) fputs(token_table[token].name, stdout); putchar('\n'); } void print_matrix(void) { int i, j; char symbs[] = " OX"; for (i=0; i < MATRIX_SIZE; i++) { for (j=0; j < MATRIX_SIZE; j++) putchar(symbs[trace_matrix[i][j]]); putchar('\n'); } }
But where do i use the double- for loop?? and what should i include in the main?
What i am currently in the main isn't very usefulllll
Code:int main() { int n; for (n=0; n<4; n++); //result += matrix_data[n]; printf("%d\n", matrix_data[n]); std::cout<< matrix_data <<std::enl; //cout << result return 0;
---------------------------[thanks for the Help]--------------------------------
Won't compile - it's not possible to initialiize the variable once it's been declared - it has to be on this line:Code:matrix_data[1][MATRIX_SIZE][MATRIX_SIZE] = { {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,1}, {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,1,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,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,0,0,0}, .... };
Edit: and of course you need to initialize both at the same time.Code:int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE];
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Hi Mats I am sorry can you explain further what do i need to change??
you mean i have to change from:
matrix_data[1][MATRIX_SIZE][MATRIX_SIZE] =
{ {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,1},
{0,1,.....
[/code]
to
Code:int matrix[MATRIX_SIZE][MATRIX_SIZE]; int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE]; cout <<"Ender Maze wanted, 0,1,2,3:\n"; cout <<" 0- Random Maze\n"; cout <<" 1- Set Maze (1)\n"; cout <<" 2- Exit\n"; cin >> x; cout <<"Maze Choosen is: " << x << endl <<endl; matrix_data[0][MATRIX_SIZE][MATRIX_SIZE] = {{0}}; matrix_data[1][MATRIX_SIZE][MATRIX_SIZE] = { {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,1}, {0,1,0,0,0,0
or sth else???? Cheers
[code]
tapstop ic
So i should put the pre-define matrix inside the
int main()?
I mean "something else".
You have variable declaration (int matrix_data[...][...][...], and then you attempt to assign this variable using an initializer list somewhere else. That is not valid code.
Simple example:
--Code:// Valid code: int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Not valid code int arr[10]; arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
This is the problem; coz i am try to store differetn 32 X 32 matrix.
therefore in my array have 2 maps.
I can't not just put 1 number.
So how can i resolve this???
or like this?
Code:int matrix_data[2][MATRIX_SIZE][MATRIX_SIZE]; = { {{0}}, {{1,1,1,1,1,0,0,0,0,0,0,},{1,1,1,0,0,0,0,1,1,0}};}
---------------------------[thanks for the Help]--------------------------------