|
|
I create CSingleLock in the thread
Obviously not a good idea the pass the object to a different thread
|
|
|
|
|
I'm very new to C and I've been working so hard on this and I have no idea what is wrong with it, I think it is in the Move method, but I wasn't sure so I included the whole program, if anyone can figure out why whats wrong please let me know! Thanks!
My assignment is to get through a maze that can be up to 50 by 50, the sample maze I've been using looks like this: The 1s are walls and the 0s are where you can step.
1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 0 0 0 0 1
1 1 0 0 0 1 0 1 0 1
1 0 0 1 1 1 0 1 0 1
1 0 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 0 1 1
1 1 0 1 1 0 1 0 0 0 S
1 0 0 0 0 0 0 1 0 1
1 0 1 0 0 0 0 0 0 1
1 0 0 1 0 0 1 1 1 1
1 0 1 0 0 1 0 0 1 1
1 1 1 1 0 1 1 1 1 1
E
It prints X's on the places it has visited and it will go left, but it won't go up, down, or right. it starts at S and ends at E, but it will only go over the first 3 0s, replace them with X's, remove the last two X's and terminate. Please help!
#include <stdio.h>
#define M 50
#define N 50
int rows, columns, exitRow, exitCol, enterRow, enterCol;
void printArray(char a[M][N], int rows, int columns);
int moveLeft(int currCol, int currRow, char a[M][N]);
int moveRight(int currCol, int currRow, char a[M][N]);
int moveUp(int currRow, int currCol, char a[M][N]);
int moveDown(int currRow, int currCol, char a[M][N]);
int Move(int currCol, int currRow, char array[M][N], int first);
void removeX(int currRow, int currCol, char array[M][N]);
int fail=0;
main()
{
char comma;
char foundEnter='n';
char foundExit='n';
int first=1;
//takes in the size of the maze
printf("Please type in the size of the field as rows, columns\n");
scanf("%d", &rows);
scanf("%c", &comma);
scanf("%d", &columns);
/*Asks user for input file and adds the field from the file to an array*/
char array[M][N]={9};
FILE *fptr;
char c;
char file_name[20];
int i,j;
printf("Type in the name of the file containing the Field\n");
scanf("%s",file_name);
fptr=fopen(file_name,"r");
for (i=0; i<rows; i++)
for (j=0; j<columns; j++){
c=fgetc(fptr);
while ( !((c == '1')||(c =='0')) ) c=fgetc(fptr);
array[i][j]=c;
}
fclose(fptr);
//Copies maze into the failed array
char failArray[M][N]={9};
for (i=0; i<rows; i++)
for (j=0; j<columns; j++){
failArray[i][j]=array[i][j];
}
//finds the entrance and exit of the maze
for(j=0; j<columns; j++){
if(array[0][j] != '1' && array[0][j]=='0'){
if(foundEnter=='n'){
enterCol= j;
enterRow=0;
foundEnter='y';
}
else{ exitCol= j;
exitRow=0;
foundExit='y';
}
}
}
for(i=0; i<rows; i++){
if(array[i][columns-1] != '1' && array[i][columns-1]=='0'){
if(foundEnter=='n'){
enterCol= columns-1;
enterRow=i;
foundEnter='y';
}
else { exitCol= columns-1;
exitRow=i;
foundExit='y';
}
}
}
for(j=columns-1; j>=0; j--){
if(array[rows-1][j] != '1' && array[rows-1][j]=='0'){
if(foundEnter=='n'){
enterCol= j;
enterRow=rows-1;
foundEnter='y';
}
else { exitCol= j;
exitRow=rows-1;
foundExit='y';
}
}
}
for(i=rows-1; i>=0; i--){
if(array[i][0] !='1' && array[i][0]=='0'){
if(foundEnter=='n'){
enterCol= 0;
enterRow=i;
foundEnter='y';
}
else { exitCol= 0;
exitRow=i;
foundExit='y';
}
}
}
//prints the array
printArray(array, rows, columns);
int currRow=enterRow; //sets the current Row to the enter Row
int currCol=enterCol; //sets the current Column to the enter Column
int end;
end=Move(currCol, currRow, array, first);
//if theres no path found it prints the fail array
if(end){
printArray(failArray, rows, columns);
printf("Path not found.\n");
}
else{
//if the path is found it prints the path to the exit
printArray(array, rows, columns);
printf("Path found.\n");
}
return 0;
}
//prints the array
void printArray(char a[M][N], int rows, int columns){
int i, j;
for (i=0; i<rows; i++)
for (j=0; j<columns; j++) {
if (j == 0) printf("\n");
printf("%c ",a[i][j]);
}
printf("\n");
}
//checks if move Left is possible
int moveLeft(int currCol, int currRow, char a[M][N]){
if(currCol-1>=-1)
if(a[currRow][currCol-1]=='0')
return 1;
return 0;
}
//checks if move Right is possible
int moveRight(int currCol, int currRow, char a[M][N]){
if(currCol+1<currCol)
if(a[currRow][currCol+1]=='0')
return 1;
return 0;
}
//checks if move Up is possible
int moveUp(int currRow, int currCol, char a[M][N]){
if(currRow-1>-1)
if(a[currRow-1][currCol]=='0')
return 1;
return 0;
}
//checks if move Down is possible
int moveDown(int currRow, int currCol, char a[M][N]){
if(currRow+1<currRow)
if(a[currRow+1][currCol]=='0')
return 1;
return 0;
}
//moves through the maze
int Move(int currCol, int currRow, char array[M][N], int first){
int fail;
int c;
array[currRow][currCol]='X';
failArray[currRow][currCol]='X';
//checks the exit condition as long as its not the first time through
if(!first){
if(currRow==exitRow && currCol==exitCol)
return 0;
if(currRow==enterRow && currCol==enterCol)
return 1;
}
//this is to track the array (temporary until code fixed)
first=0;
printArray(array, rows, columns);
printf("At Point row %d and col %d \n",currRow, currCol);
printf("fail=%d \n", fail);
getchar();
//goes left until it can't
if(moveLeft(currCol, currRow, array)){
c=--currCol;
printf("Moved left \n");
printf("Moving to col %d \n",c);
fail = !Move(c, currRow, array, first);
}
//if left fails goes up until it can't
if(moveUp(currCol, currRow, array) && fail){
c=--currRow;
printf("Moved up \n");
fail = !Move(currCol, c, array, first);
}
//if up fails goes down until it can't
if(moveDown(currCol, currRow, array) && fail){
c=++currRow;
printf("Moved down \n");
fail = !Move(currCol, c, array, first);
}
//if down fails goes right until it can't
if(moveRight(currCol, currRow, array) && fail){
c=++currCol;
printf("Moved right \n");
fail = !Move(c, currRow, array, first);
}
//removes the X so we can backtrack
removeX(currRow, currCol, array);
return 1;
//absolute failure
printf("Failed all \n");
return 0;
}
//removes the X and replaces it with a 0.
void removeX(int currRow, int currCol, char array[M][N]){
array[currRow][currCol]='0';
}
|
|
|
|
|
You're very unlikely to get an answer to such posts as it is very long and people here may not have the time to look at it.
You should read Chris' message How to get an answer to your question[^]
Finally, I think you should use a debugger and single step through your code.
And it would help if your code is properly indented.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
forensicgeek wrote: I wasn't sure so I included the whole program...
First mistake is there's never a reason to include so much code.
forensicgeek wrote: ...if anyone can figure out why whats wrong please let me know!
Second mistake is asking others to wade through all of this code. It's your assignment, after all.
Third mistake is that you did not read (or did but chose to ignore) this, especially point #7.
Rather than just run the program and hope it spits out the correct answer at the end, consider stepping through it using the ever-helpful debugger. You may not find the exact problem with it, but you can very easily narrow it down to just a small handful of lines rather than a whole program.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
You can get through any maze by putting your right hand on the wall as you enter, then keeping your right hand on the wall as you walk through the maze.
I.e., whenever you have the opportunity to turn right, do it, still keeping your right hand on the wall.
When you reach a dead end, you loop around, still keeping your right hand on the wall at all times.
This will get through any maze. Another name for this search pattern is a Depth-First Search (http://en.wikipedia.org/wiki/Depth-first_search[^]).
|
|
|
|
|
Is there any way to get the time as represented on another computer on a LAN.
Regards,
Bram van Kampen
|
|
|
|
|
Use NetRemoteTOD() .
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Thanks,
Works a Treat. Blessed are those that remember all those Windows API Names.
Regards,
Bram van Kampen
|
|
|
|
|
Hi,
I'm going to write small app ( in C++) for creating music, which would work under Win Xp, and I need some means for playing samples and streaming raw audio data.
I've looked up for some audio apis but I have no idea which one would be the best to perform this task. I've found some info about core audio APIs and I think that they would be right choose for my app. Unfortunately, as I know, they are just available for Vista and probably Win 7. That's why I would be grateful if you could give me some alternatives for this API (It also can be some cross-platform API).
Thanks in advance for help.
modified on Friday, September 18, 2009 3:06 PM
|
|
|
|
|
|
Thanks for that, but I forgot to mantion that I want to write in C++ rather than C. So I would need some c++ API.
Heh, I've just realised that ASIO is an API. So, thank you one more time for help.
|
|
|
|
|
Hi,
I have a process that need to check a string formating. How the best way to check it ?
My client receive a e-mail with a field to fill a date "DD/MM/AAAA". when i receive the answer i need to check if the "/" and values for DD, MM and AAAA
or
My client receive a e-mail with a field to fill a value "9999" with range 10 to 1500.
I whould like a way to check it directy for true or false.
Someone can help me ?
|
|
|
|
|
for the date thing, i would just do it with simple CString search functions.
off the top of my head....
if (inString.GetLength()==10)
{
if (inString.GetAt(2) == '/' && inString.GetAt(5) == '/')
{
csDD = inString.Mid(0, 2);
csMM = inString.Mid(3, 2);
csYYY = inString.Mid(6, 4);
}
}
that's really brittle and won't accept things like "9/28/1927" or "5/5/05". but if you want to strictly enforce two-digit D and M and four digit Y, then that's all you need.
|
|
|
|
|
Use CStringT::Tokenize[^].
In this case the function must return 3 values in a loop.
The first value will be DD and the next MM and the next AAAA.
You could do the validation on these values.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
SNArruda wrote: My client receive a e-mail with a field to fill a date "DD/MM/AAAA". when i receive the answer i need to check if the "/" and values for DD, MM and AAAA
For dates, how about something like:
COleDateTime date;
date.ParseDateTime("17/9/2009");
if (date.GetStatus() != COleDateTime::valid)
...
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Can anyone suggest a good study book for C/C++/VC++2008.
The book should be intermidiate/advanced.
My background is that i have read Programming in C by Byron S. Gottfried.
|
|
|
|
|
Here[^] is my list of recommended books.
|
|
|
|
|
Ivor Horton has a good book on Visual C++ 2008.
He teaches C++ in the first half of the book and Windows Programming in the second half. He's still uses MFC for most of the book (which is a good thing IMO) but he has adapted and incorporated WinForms and some managed code for those who want it.
I've been very impressed with his teaching style and his choice of topics to cover.
|
|
|
|
|
#include<stdio.h>
#define abs(x) ((x<0)?(-(x)):(x))
#define epsilon .000000000001
#define size 5
double multiply(a,m,product)
double a[size][size],product [size][size];
int *m;
{
int test=0;
int count,i,j,k,l;
double olda[size][size],diff;
for(i=0;i<*m;i++)
for(j=0;j(* m;j++)
olda[i][j]=a[i][j];
for(count=1;count<=1000;count++)
{
for(i=0;i<*m;i++)
for(j=0;j<*m;j++)
product[i][j]=0;
for(i=0;i<*m;i++)
for(j=0;j<*m;j++)
for(k=0;k<*m;k++)
product[i][j]=product[i][j]+a[i][k]*olda[k][j];
for(i=0;i<*m;i++)
for(j=0;j<*m;j++)
{
diff=abs(product[i][j]-olda[i][j]);
if(diff>epsilon)
{
test=1;
for(k=0;k<*m;k++)
for(l=0;l<*m;l++)
olda[k][l]=product[k][l];
}
break;
if(test<1)
{
printf("\nThe numberof iterations is:");
printf("\n");
printf("\n %d",count);
break;
}
test=0;
}
return(0);
}
main()
{
static double a[size][size]={{0.1,1.0,0.0,0.0,0.0},
{0.0,0.0,0.3,0.7,0.0},
{0.0,0.0,0.0,0.2,0.8},
{0.0,0.0,0.1,0.0,0.9},
{1.0,0.0,0.0,0.0,0.0}};
double product[size][size],pi[size],diff;
int test;
int m=size;
int i j;
multiply(a,&m,product);
printf("\n");
printf((" \nThe product is:");
printf("\n");
for(i=0;i<m;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
printf(" %15.12f", product[i][j]);
}
printf("\n");
printf("n");
return(0);
}
I tried to compile this and I got errors thank you for your input. I apologise for not posting this correctly yesterday.
|
|
|
|
|
belleissexy333 wrote: I tried to compile this and I got errors thank you for your input
It would really help if you post the exact error messages. They are there for a reason you know
Anyway, at first sight the block of for loops seems very strange...
|
|
|
|
|
The compiler tells you at what line the error occurs and what is the error. Could you please provide us this useful info?
Anyway, the following lines looks wrong:
belleissexy333 wrote: for(i=0;i<m;i<m;i++)
belleissexy333 wrote: for(j=0;j(* m;j++)
BTW: why the m parameter of the multiply function is a pointer (you always access just its value)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
I just got this error yesterday with VS 2005. I thought it had been solved with SP1, but it hadn't. My solution was to access the class view from the header. Don't know why it worked.
|
|
|
|
|
I using Window CE API WriteFile() to write some barcode data to a file. Data is scanned and written to file ok. Problem is everything is on one line. How can I get the data in the file to be
line based. In other words, everytime I write to the file it writes a new line.
Thanks
|
|
|
|
|