
April 10th, 2017, 10:19 AM
#1
pattern in code
hello i'm a newbie, i am learning c++ and working with my teacher. Recently i got stuck with a problem. i am working on a code for shortest path.
i have data for obstacle and i need to input them in my code. this is the code:
Code:
int main()
{
int i,j,x,y,x1,y1;
for(i=0; i < 12; ++i)
for(j=0; j < 12; ++j)
RES[i][j]=0; // way
RES[1][5]=3; // start
RES[10][5]=5; // goal
RES[3][4]=RES[3][5]=RES[3][6]=2; // block
x=1;
y=5;
for(i=0; i < 12; ++i)
for(j=0; j < 12; ++j)
R[i][j]=254; // walkable
R[1][5]=253; // start
R[10][5]=0; // finish
R[3][4]=R[3][5]=R[3][6]=255; // pathless, bunker
for(j=0; j < 12; ++j)
R[0][j] =255;
for(j=0; j < 12; ++j)
R[11][j]=255;
for(i=0; i < 12; ++i)
R[i][0] =255;
for(i=0; i < 12; ++i)
R[i][11]=255;
ofstream RRR;
RRR.open("OutWalk.txt", ios::out  ios::app);
if(!RRR)
RRR.open("OutWalk.txt", ios::out);
for(N=0; N < Nmax; ++N)
{
cout<<" N = " << N << endl;
for(i=1; i < 11; ++i)
for(j=1; j < 11; ++j)
{
if(R[i][j] == N)
{
if(R[i+1][j] == 253)
{
x=i+1; // start
y=j;
goto PATH;
}
if(R[i+1][j] == 254)
R[i+1][j]=N+1;
if(R[i1][j] == 253)
{
x=i1; // start
y=j;
goto PATH;
}
if(R[i1][j] == 254)
R[i1][j]=N+1;
if(R[i][j+1] == 253)
{
x=i; // start
y=j+1;
goto PATH;
}
if(R[i][j+1] == 254)
R[i][j+1]=N+1;
if(R[i][j1] == 253)
{
x=i;
y=j1;
goto PATH;
}
if(R[i][j1] == 254)
R[i][j1]=N+1; // start
}
} // R[12][12]
} // N
PATH:
if(R[x+1][y] > R[x1][y])
{
x1=x1;
y1=y;
}
else
{
x1=x+1;
y1=y;
}
if(R[x1][y1] > R[x][y+1])
{
x1=x;
y1=y+1;
}
if(R[x1][y1] > R[x][y1])
{
x1=x;
y1=y1;
}
RES[x1][y1]=1;
if(R[x1][y1] == 0)
{
RES[x1][y1]=5;
goto END;
}
x=x1;
y=y1;
goto PATH;
END:
cout << " N = " << N << endl;
RRR << endl;
RRR << endl;
RRR << " Number of circuit = " << N << " Goal: x = "<< x1 << " y = "<< y1 << endl;
for(i=10; i > 0; i)
{
RRR << endl;
for(j=1; j < 11; ++j)
RRR << R[i][j] << " \t ";
}
RRR << endl;
for(i=10; i > 0; i)
{
RRR << endl;
for(j=1; j < 11; ++j)
RRR << RES[i][j] << " ";
}
return 0;
}
and i have a data.txt file containg values
0 9
1 7
2 5
3 2
4 0
with them i have to make pattern like this
0 0 0 0 0 0 0 0 0
0 2 2 2 2 2 2 2 0
0 2 5 5 5 5 5 2 0
0 2 5 7 7 7 5 2 0
0 2 5 7 9 7 5 2 0
0 2 5 7 7 7 5 2 0
0 2 5 5 5 5 5 2 0
0 2 2 2 2 2 2 2 0
0 0 0 0 0 0 0 0 0
can anyone help me with it?
Last edited by 2kaud; April 10th, 2017 at 11:01 AM.
Reason: Added code tags

April 10th, 2017, 11:20 AM
#2
Re: pattern in code
When posting code, please use code tags so that the program is readable. Go Advanced, select the formatted code and click '#'.
i need to input them in my code
Inputting numeric data from a file is actually quite easy. Consider
Code:
...
ifstream ifs("data.txt");
if (!ifs.is_open()) {
cout << "Cannot open input file data.txt" << endl;
return 1;
}
int num1, num2;
while (ifs >> num1 >> num2) {
//process num1 and num2 as required
}
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++17 Compiler: Microsoft VS2017 (15.4.1)

April 10th, 2017, 11:21 AM
#3
Re: pattern in code
Originally Posted by user420420
with them i have to make pattern like this
The pairs of numbers on data.txt seems to be distances to the left and and an associated number to the right (a height maybe?).
You can view the matrix as an (x,y) coordinate system with 9 at origo (coordinate (0,0) and distance 0). Then at distance 1 you have 7. In coordinates (x,y) that would be at positions: (0,1), (1,1), (0,1), (1,1), (1,0), (1,1), (0,1) and (1,1).
Can you from (x,y) calculate the distance? Yes you can using this formula,
distance = max(abs(x), abs(y))
Now you know how to calculate the distance from origo to every coordinate (x,y) and you also know the number associated with the distances. So say you want to print the matrix. Then you just scan through the coordinates (using two nested loops) starting at the upper left corner (which is coordinate (4,4)). The distance at that position is max(abs(4),abs(4)) which is 4. The number associated with distance 4 is 0 which is then to be printed..
That's one way to do it.
Last edited by wolle; April 10th, 2017 at 01:02 PM.

April 10th, 2017, 04:08 PM
#4
Re: pattern in code
Thank you for your answer. but as i said i'm a newbie, can you please take a look at my code and fix it.
in the code the line
Code:
RES[3][4]=RES[3][5]=RES[3][6]=2; // block
in this line i want to implement the coding I asked from text file in a fixed place in matrix like in the code.
if you still don't get let me give you another example:
Code:
0 0 0 0 5 1 1 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 1 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 2 2 2 1 0 0 0
0 0 0 2 9 2 1 0 0 0
0 0 0 2 2 2 1 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 0 0 3 0 0 0 0 0
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
