I'm trying to solve a C++ problem using Lee's Algorithm (a maze). I have to find a place where two people meet at the exact same time (the shortest), knowing that R and J represent the places where they start and X are the obstacles. This is how rj.in looks :
5 8
XXR XXX
X X X
J X X X
XX
XXX XXXX
And the problem is that when I read it, it always has the same value. What am I doing wrong? This is my code, and this is where the problem appears:
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
#define ex -1
#define red(X) scanf("%d",&X);
const int MAX=150;
int ro [MAX][MAX],ju [MAX][MAX];
inline int inside(int x,int y,int n,int m)
{
return x>=1 and x<=n and y>=1 and y<=m;
}
queue<pair<int,int> > Qr;
queue<pair<int,int> > Qj;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int main()
{ freopen("rj.in","r",stdin);
freopen("rj.out","w",stdout);
int rx_n,ry_n,jx_n,jy_n,n,m,i,j,t;
red(n);
red(m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
red(t);
printf("%d ",t);
if((char)t=='X') {ro[i][j]=-1; ju[i][j]=-1; }
if((char)t=='R') {ro[i][j]=1; Qr.push(make_pair(i,j));}
if((char)t=='J') {ju[i][j]=1; Qj.push(make_pair(i,j));}
}
I'm using the C functions because they are faster.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I write my code in code blocks and I get this runtime error during execution:
Process returned (0xC0000005)
This is my code
I believe it has something to do with the way I call my functions
main:
int main()
{
int LT[101],i, NI;
float x;
for (i=0 ; i<101 ; i++)
LT[i]=i*i;
NI=find_NI(LT , x);
x=recieve_check_x();
cout<<"square root of x is:"<<(NI+(x-LT[NI])/(2*NI));
return 0;
}
rec_chk.cpp:
#include <iostream>
#include "funcs_hd.h"
using namespace std;
float recieve_check_x ()
{
float x;
cout<<"enter the value of x:"<<endl;
cin>>x;
while (x<0 or x>10000)
{
cout<<"\a error! x must be within range of [0,10000]"<<endl;
cout<<"enter another value:"<<endl;
cin>>x;
}
}
find_NI:
#include <iostream>
#include "funcs_hd.h"
#include <cstdlib>
#include <cmath>
using namespace std;
int find_NI (int LT[] , float x)
{
int i, j , NI;
j=i+1;
double i_delta=abs(LT[i]-x);
double j_delta=abs(LT[j]-x);
if (i_delta<j_delta)
NI=LT[i];
else
NI=LT[j];
return NI;
}
funcs_hd:
#ifndef FUNCS_HD_H_INCLUDED
#define FUNCS_HD_H_INCLUDED
float recieve_check_x (void);
int find_NI (int [] , float);
#endif // FUNCS_HD_H_INCLUDED
I get an error code and the program stops running
int i, j , NI;
j=i+1;
^ indeterminate value
You read an indeterminate value in the program. Therefore the behaviour of the program is undefined.
How can I change the values of x and y in this code?
When I try to do it by x=y, y=x; it changes it to the same numbers.
How can I do this?
How can I do this for 3 values like (x y z)?
#include <iostream>
using namespace std;
int main()
{
int x=4;
int y=6;
cout<<x<<y;
return 0;
}
I tried this code before but it isn't working the way I want it.
#include <iostream>
using namespace std;
int main()
{
int x=4;
int y=6;
x=y,y=x;
cout<<x<<y;
return 0;
}
This is because you first set x's value and then copy that value into y. There is a standard library function called std::swap, which should do the job.
You can see an exapmle of it here.
std::swap is defined in the header <algorithm> before C++11 and in <utility> since C++11. So make sure you #include the correct header.
The benefit of using std::swap in C++11 as opposed to having a third temporary variable that you copy the value into, is that std::swap uses std::move and thereby creates no additional copies.
For three numbers you'll have to make your own implementation like this:
#include <iostream>
int main() {
int x{5}, y{3}, z{2};
int temp{std::move(x)};
x = std::move(y);
y = std::move(z);
z = std::move(temp);
std::cout << x << ' ' << y << ' ' << z << '\n';
return 0;
}
Ideone
For 2 variables, you should use std::swap, for more variables, you may use std::tuple:
std::tie(x, y, z) = std::make_tuple(y, z, x);
Demo
There are two options:
Introduce temporary variable:
int temporary = y;
y = x;
x = temporary;
Or use std::swap like this std::swap(x, y); (you might need to import <algorithm> or <utility>)
Now why you are getting this error? Let's analyze what you are doing here step by step:
x = y; Give x value of y. So now x is equal to y
y = x; Give y value of x. But wait, x is now equal to y. So nothing changes.
If you still have trouble understanding what you did wrong I suggest taking a piece of paper and following your own code step by step on paper writing state of program at each step.
And one last thing as advice for the future. Please make you questions clear and properly formatted. For example, it totally don't understand your point with 3 values. Please explain what you mean and probably provide some example and then maybe someone will be able to help you.
try this:
#include <iostream>
using namespace std;
int main()
{
int x=4;
int y=6;
int temp=x;
x=y;
y=temp;
cout<<x<<y;
return 0;
}
Without using a third variable you can do swapping two variables like this,
#include <iostream>
using namespace std;
int main()
{
int x=4;
int y=6;
x = x + y;
y = x - y;
x = x - y;
cout<<"X = "<<x << "\n"
<<"Y = "<<y;
return 0;
}
So the output will be,
X = 6
Y = 4
x=4
y=6
x=y // x=6, y=6
y=x // does nothing
Try using another variable:
#include <iostream>
using namespace std;
int main()
{
int x=4;
int y=6;
int temp=x;
x=y,y=temp;
cout<<x<<y;
return 0;
}
Here is code to swap 2 variables(1 line)
#include <iostream>
using namespace std;
int main()
{
int x = 4;
int y = 6;
cout << x<< y;
swap(x,y);
cout << x << y;
getchar();
return 0;
}
Here is code to swap 3 variables(1 line)
#include <iostream>
#include <tuple>
using namespace std;
int main()
{
int x = 4;
int y = 6;
int z = 8;
cout << x<< y<<z;
tie(x, y, z) = make_tuple(y, z, x);
cout << x << y << z;
getchar();
return 0;
}
I have to create a game of 5 rounds simulating a soccer shootout using a 2x3 array that represents the goal. The computer randomly picks 3 places to block and the user chooses one place to shoot. If the user chooses a coordinate that is not blocked then its a goal. Two functions are needed, one where the computer picks 3 random places to block and the other function is prints out the goal every round. If the user scores 3 times then they win, otherwise they lose.
The output should look like this(B=Blocked, G=Goal, "-" = empty space):
B - B
B - G
Ive been stuck on my code and have gotten an error that I just cant seem to fix within both functions
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
void computerPick(char soccer[]);
void shot(char shooter[]);
int main()
{
int userInputX;
int userInputY;
srand(time(NULL));
char soccer[2][3];
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
soccer[i][j]='-';
}
}
cout<<"Pick a X coordinate to shoot at: "<<endl;
cin>>userInputX;
cout<<"Pick a Y coordinate to shoot at: "<<endl;
cin>>userInputY;
computerPick(soccer);
shot(soccer,userInputY,userInputX);
}
void computerPick(char soccer[])
{
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
}
void shot(char shooter[], int userInputY, int userInputX)
{
int score=0;
if(shooter[userInputX][userInputY]!='B')
cout<<"shot is good"<<endl;
else
cout<<"shot is blocked"<<endl;
}
You have to use correct types for arguments and have to match the prototype declaration and definition of functions.
This code compiles:
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
void computerPick(char soccer[][3]);
void shot(char shooter[][3], int userInputY, int userInputX);
int main()
{
int userInputX;
int userInputY;
srand(time(NULL));
char soccer[2][3];
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
soccer[i][j]='-';
}
}
cout<<"Pick a X coordinate to shoot at: "<<endl;
cin>>userInputX;
cout<<"Pick a Y coordinate to shoot at: "<<endl;
cin>>userInputY;
computerPick(soccer);
shot(soccer,userInputY,userInputX);
}
void computerPick(char soccer[][3])
{
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
}
void shot(char shooter[][3], int userInputY, int userInputX)
{
int score=0;
if(shooter[userInputX][userInputY]!='B')
cout<<"shot is good"<<endl;
else
cout<<"shot is blocked"<<endl;
}
You might want to look at these parts again:
char soccer[2][3];
and
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
Also note that booleans would be clearer in your two dimensional array instead of chars of 'B' or 'G'.
Also when using multidimensional arrays as parameters, you can pass them as
int foo(int (*array)[5][10])
Which means that you are passing a pointer to an array of fixed size 5-10
I understand the difference between int, float and double data types. But I have observed that sometimes when I use 'int' data type in a mathematical operation comprising of only integer values, it gives a result one less than the right answer. However, it's correct when I use float or double.
Take a look at the code below.
#include <iostream>
#include<math.h>
using namespace std;
int getno(int num)
{
int x,i;
float y=0;
for(i=0; i<4; i++)
{
x=num%10;
y=y+(x*pow(10,i));
num=num/10;
cout<<x*pow(10,i)<<endl;
cout<<y<<endl;
}
return y;
}
main()
{
int n;
cin>>n;
cout<<getno(n);
}
if I change the datatype of y to int, it gives a wrong answer by one. i.e 12345 would result in 2344 instead of the required 2345. Why is that happening?
I suppose "pow(10, i)" returns a bit less value than 10^i, let say 999.9999999 for i=3.
And afterwards it's truncated by float->int conversion into 99. As a result, we can loss 1 at every "y=.." operation.
I don't see your issue:
#include <iostream>
#include <math.h>
using namespace std;
int getno(int num)
{
int x,i;
float y=0;
for(i=0; i<4; i++)
{
x=num%10;
y=y+(x*pow(10.0f,(float)i));
num=num/10;
cout<<x*pow(10.0f,i)<<endl;
cout<<y<<endl;
}
return (int)y;
}
int main(void)
{
int n=12345;
// cin>>n;
cout<<getno(n);
return 0;
}
and the output is:
5
5
40
45
300
345
2000
2345
2345
I just cleaned up the warnings about int to float conversion on your call to pow and explicitly convert your return type.
I want to program the g-adic expansion in the c++ language, but whatever I try, the output is still wrong. Let me first explain what the g-adic expansion is. The g-adic expansion is a way to represent numbers. For example, binary numbers, this is the 2-adic expansion of numbers. And hexadecimal is 16-adic expansion. So here is my code:
#include <iostream>
#include <cmath>
#include <complex>
#include <valarray>
using namespace std;
int main()
{
int x;
int g;
cin>>x;
cin>>g;
int k=log(x)/log(g)+1;
int e;
int b=0;
int* myArray=NULL;
myArray=new int[k];
for(int i=0;i<k;i++)
{
myArray[i]=0;
}
while(b!=k)
{
e=x/(g^(k-b-1));
myArray[b]=e;
x=x-e*g^(k-b-1);
b++;
}
b=0;
while(b!=k)
{
cout<<myArray[b]<<endl;
b++;
}
delete [] myArray;
myArray=NULL;
return 0;
}
So for example if I want to convert 105 to binary, x=105 and g=2, k is the length of the new number. In this case that is 7. int e=105/2^(7-1)=1. This is the first number. Then x=105-1*2^(7-1)=41. If you do this by hand, you will find that 105 becomes 1101001. But if I compile this piece of code, it just doesn't work. My question is what is wrong with this code?
The ^ doesn't do exponentiation. It's the exclusive-or operator. To do exponentiation, use the pow function.
e=x/std::pow(double(g),double(k-b-1));
myArray[b]=e;
x=x-e*std::pow(double(g),double(k-b-1));
You can see your program in action, with my changes, on IDE One.
here: run this program
#include <iostream.h>
#include <cmath>
#include<stdlib.h>
#include<stdio.h>
int main()
{
int x;
int g;
cin>>x;
cin>>g;
while(x>g)
{
cout<<x%g<<endl;
x/=g;
}
cout<<x%g<<endl;
return 0;
}
works for 105 and 2 and does not need an array