Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I tried writing a program that outputs "YES" whether every x value or y value is all the same. Otherwise it gives the output "NO". The logic is, if all the x values maximum value is the same as the all the x values minimum value, than this value has never changed, hence all the x values are the same. Same for the y values.
However, the output sometimes give the correct result, sometimes not (for the same input). Moreover, the outputs are not regular. (For example, 2 correct, 3 wrong, 5 correct, 1 wrong etc.)
Here is my code:
#include <iostream>
#include <climits>
using namespace std;
int main(){
int n;
int minX,minY=INT_MAX;
int maxX,maxY=INT_MIN;
cin>>n;
while(n--){ //for the next n line
int x,y;
cin>>x>>y;
maxX=max(maxX,x);
//cout<<maxX<<" "; //comments I write to find out what the heck is happening
minX=min(minX,x); // This value changes irregularly, which I suspect is the problem.
//cout<<minX<<" ";
maxY=max(maxY,y);
//cout<<maxY<<" ";
minY=min(minY,y);
//cout<<minY<<endl;
}
if(maxX==minX||maxY==minY){ //If the x values or the y values are all the same, true
cout<<"YES";
}
else{
cout<<"NO";
}
return 0;
}
Input:
5
0 1
0 2
0 3
0 4
0 5
Output when it Works (with the couts I commented):
0 0 1 1
0 0 2 1
0 0 3 1
0 0 4 1
0 0 5 1
YES
One of the output when it doesn't work(with the couts I commented)
0 -1319458864 1 1 // Not all the wrong outputs are the same, each wrong output is different than the other wrong output.
0 -1319458864 2 1
0 -1319458864 3 1
0 -1319458864 4 1
0 -1319458864 5 1
NO
In these lines
int minX,minY=INT_MAX;
int maxX,maxY=INT_MIN;
^
minX and maxX is never initialized. This is an UB as defined by the standard. Whatever you read is not predictable - it's usually what'l left on that block of memory by another process.
Do note that = has a higher priority than comma, so the expression is evaluated as
int (minX),(minY=INT_MAX);
Actually the comma has the lowest priority among all operators in C++. Change them to these should fix
int minX=INT_MAX,minY=INT_MAX;
int maxX=INT_MIN,maxY=INT_MIN;
^~~~~~~
Related
This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Good debugger tutorial for beginners [closed]
(1 answer)
Closed 2 years ago.
Hello i was having some funny with my code lately and i met strange output of double recursion. I was trying to understand it but im not so lucky to know the answer yet. Maybe u ll help me with tis problem . Why this program has so weird output? output : 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1. I figure out that firstly it just do first somefunc call so printing 4321 , next somefunc has output like 1234. firs it decrement 1 so its 0 next decrement 2 by 1 so its 1 and then decrement then 3 decrement by 1 its 2 and 1 , decrement 4 by 1 untill 0 so its 321 its logically output will be 4321 2 1 3 2 1 but i dont understand the rest.
#include <iostream>
using namespace std;
void somefunc(int c)
{
if(c>=1)
{
cout <<c<<" ";
somefunc(c-1);
somefunc(c-1);
}
}
int main()
{
somefunc(4);
return 0;
}
I had referred to many articles and questions that answered how to solve a maze effectively but here I want to confirm what's going wrong in my code. Consider the maze:
2 1 0 0 3
0 1 0 1 1
0 1 0 0 1
0 1 1 0 0
0 0 0 0 0
where the 1's represent the walls and 0's represent the path.(source is 2 and destination is 3).
I have to output whether there is a path or not.
int y=0;
while(y==0)
{
robo1(n,m,maze);//this function adds 2 to any '0'/'3' in (i,j+1),(i+1,j),(i-1,j),(i,j-1) (if exists),where (i,j) is 2
robo2(n,m,k2,maze);//this function adds 3 to any '0'/'2' in (i,j+1),(i+1,j),(i-1,j),(i,j-1) (if exists), where (i,j) is 3
if(find5(n,m,maze)==1)//this function returns 1 if there is '5' in the maze
y++;
if(find0(n,m,maze)==0)//this function returns 0 if there are no '0' in the maze
break;
}
if(find0(n,m,maze)==0 && y==0)
printf("-1\n");//no path
else
printf("1\n");//there is a path
My idea is that if after any number of loops a five is found in the maze, then it would mean there is a path.
But while implementing this function in code I get wrong answers and sometimes run-time errors.
Is there any flaw in the above logic?
The general idea should almost work, but of course everything is in the details.
One case in which your approach will not work even if implemented correctly is however this:
2 1 0 0 0
1 1 0 1 1
0 0 0 1 3
i.e. if both 2 and 3 are "closed" by walls but there are 0s in the room. Your loop will never end because despite having 0s around neither of the two robo function will change anything.
A simple solution is returning 0/1 from robos if they actually changed at least a value in the matrix and quitting when this doesn't happen.
Note that this is not a very efficient way of solving a maze (your code will keep checking the same cells over and over many times).
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 7 years ago.
Improve this question
I am creating a Minesweeper game. However, while testing the generating function, It malfunctions almost always (if not always), and I do not understand why.
Here is my code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct board {
int width=9, mines=10;
char board[9][9];
/* char board[][]
* -1 = Mine
* 0 = No mines near
* 0+ = x amount of mines are near
*/
};
struct point {
int x,y;
};
board newBoard(){
board board1;
point randPoint;
for(int i=0;i<board1.width;i++){
for(int j=0;j<board1.width;j++) board1.board[i][j]=0; // Initialize array
}
for(int i=0;i<board1.mines;i++){
randPoint.x=rand()%board1.width, randPoint.y=rand()%board1.width; // Where will the mine go?
if(board1.board[randPoint.x][randPoint.y]!=-1){ // If not already a mine
board1.board[randPoint.x][randPoint.y]=-1; //make a mine
} else i--; //else don't count this
}
for(int i=0;i<board1.width;i++){
for(int j=0;j<board1.width;j++){
if(board1.board[i][j]==-1) { // If mine exists
// The if checks preceding the ++'s are to prevent out of bounds erors
if (j-1>=0) board1.board[i][j-1]++;
if (j+1<board1.width) board1.board[i][j+1]++;
if (i-1>=0) board1.board[i-1][j]++;
if (i+1<board1.width) board1.board[i+1][j]++;
if ((i-1>=0) && (j-1>=0)) board1.board[i-1][j-1]++;
if ((i-1>=0) && (j+1<board1.width))board1.board[i-1][j+1]++;
if ((i+1<board1.width) && (j-1>=0))board1.board[i+1][j-1]++;
if ((i+1<board1.width) && (j+1<board1.width))board1.board[i+1][j+1]++;
}
}
}
return board1;
}
int main() {
board boardGame=newBoard();
printf("- ");
for(int i=0;i<boardGame.width;i++) printf("%i ",i+1);
printf("\n\n");
for(int i=0;i<boardGame.width;i++){
printf("%i. ",i+1);
for(int j=0;j<boardGame.width;j++) if (boardGame.board[i][j]==-1) {
printf(" X");
} else {
printf(" %i", boardGame.board[i][j]);
}
printf("\n");
}
return 0;
}
This produces:
- 1 2 3 4 5 6 7 8 9
1. 0 0 0 0 1 X 1 0 0
2. 1 1 0 0 2 2 2 1 1
3. X 2 1 1 1 X 1 1 X
4. 1 2 X 0 1 1 0 1 1
5. 0 1 1 1 0 0 0 0 0
6. 0 0 0 0 1 1 1 0 0
7. 0 0 1 1 2 X 1 0 0
8. 1 1 2 X 2 1 1 0 0
9. 1 X 2 1 1 0 0 0 0
As you most likely already know, in the game of minesweeper, there is mines (in this case will they will be marked as X), and all nearby grid points are the number of mines near it (if you are still unfamiliar with it this page may of use). As you can see, the numbers at 4,7 and 4,4 are incorrect.
I do not know why this is this way. Could someone aid my understanding in this, and tell my how to to fix this?
Also, I just noticed that this produces the same output every time it is run. Why?
Ioums is correct, you are not checking to see if a cell is a mine before incrementing it. However, with the way that your code is currently set up, this will mean adding a check that the cell does not equal -1 in every single if statement. You should consider creating a function to safely increment a cell if it is within bounds and not a mine, like so:
void safeIncrement(int x, int y, board& b)
{
if(x >= 0 && y >= 0 && x < b.width && y < b.width && b.board[x][y] != -1)
{
b.board[x][y]++;
}
}
This means that you can replace your if statements with:
safeIncrement(i-1,j,board1);
safeIncrement(i-1,j-1,board1);
safeIncrement(i-1,j+1,board1);
safeIncrement(i,j-1,board1);
safeIncrement(i,j+1,board1);
safeIncrement(i+1,j,board1);
safeIncrement(i+1,j-1,board1);
safeIncrement(i+1,j+1,board1);
Which is much more readable in my opinion. Additionally, since the function doesn't increment the cell if it is a mine, you could also replace the if statements with the following code!
for(int a=-1; a<=1; a++)
{
for(int b=-1; b<=1; b++)
{
safeIncrement(i+a,j+b, board1);
}
}
The problem happens when 2 mines are close together: when you're adding to the mine count, you don't check if that square has a mine.
Suppose you get a mine on (0, 0) and another on (0, 1). When you're adding to the mine count around (0, 0), you accidentally also add to the mine in (0, 1), changing it from -1 to 0. It also makes the second mine being processed disappear.
I suggest using another number to signal a mine, like -999, and check if the number is negative when looking for them. It's easier than adding another condition for all if clauses you already have.
I was solving a problem on codechef:
https://www.codechef.com/NITWMA01/problems/QPALIN.It required to input m number of input cases after getting the value of m from the user. I always used to run a loop of:
while(m--)
{//input test cases}, but in this problem I don't know why the loop is running less than m times when I have to get input cases m times. I tried running the code with the sample input(with m having 6) but main() returned 0 after getting just 4 inputs(and printint respective outputs wherever necessary).
My code is as follows:
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main()
{
int n,m,op,x,l,r,i,j,xorpair=0;
char k,s[100000];
scanf("%d",&n);
scanf("%s",s);
scanf("%d",&m);
for(j=0;j<m;j++)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&x);
scanf("%c",&k);
s[x-1]=k;
}
else
{
xorpair=0;
scanf("%d%d",&l,&r);
for(i=l-1;i<r;i++)
{
xorpair^=s[i]-'0';
}
if(xorpair==0)
{
printf("YES\n");
}
else printf("NO\n");
}
}
return 0;
}
PS: I have replaced cin with scanf. Also I believe I was not able to properly convey what problem actually I am facing. So here's the test case that explains it more clearly:
(What it should be like)
Sample input:
7
abbacca
6
2 1 4
1 1 z
2 1 4
1 4 z
2 1 4
2 5 7
Sample output:
YES
NO
YES
YES
Following is the problem that I am facing:
When I run the program for the above input, this is what appears on the output screen
7
abbacca
6
2 1 4
YES
1 1 z
2 1 4
NO
1 4 z
Process returned 0 (0x0)
I am not able to enter inputs 6 times and after 4th input it returns 0.
scanf("%c" does not do what you seem to expect. It reads just the ' ' before the char you want. Then the next tries at reading op and x fail leaving op still equal to 1, so you do two of operation 1 for every once that you intended to.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to recreate the game 2048 in C++.
I'm working on a spawn function right now. It takes in an array of the current values of the 16 spots, randomly scans for an empty one, and puts either a 2 or a 4 in that spot.
I am starting out with a test array, b. I want to pass this array to a function that will alter one of its values, which I know I need to do by passing a pointer, but none of the changes are staying after I leave the function.
Can anyone see what is wrong here? How do I properly pass the array in so the changes will stay after the spawn function?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void showBoard(int board[]);
void spawn(int* board);
int main() {
srand(time(NULL));
int b[16] = {2, 2, 2, 2};
int* bp = b;
showBoard(b);
spawn(bp);
showBoard(b);
}
// print out the 16 current tiles to the console
void showBoard(int board[]) {
for(int i=0; i<=15; ++i){
if(i%4==0)
cout<<'\n';
cout<<board[i]<<" ";
}
cout<<'\n';
}
void spawn(int* board) {
int x; // index
// randomly choose an index to spawn a 2 or 4:
do x=rand()%16; while(board[x]!=0);
// when found empty place (with value 0), spawn a new tile.
/* there should be a 90% chance of spawning a 2
* and a 10% chance of spawning a 4. Generate a
* random number between 0 and 9, and if it is
* 9, make the new spawn tile a 4.
*/
if (rand()%10 == 9) {
board[x] == 4;
cout << "added 4 \n";
}
else {
board[x] == 2;
cout << "added 2 \n";
}
}
The output:
2 2 2 2
0 0 0 0
0 0 0 0
0 0 0 0
added 2
2 2 2 2
0 0 0 0
0 0 0 0
0 0 0 0
So my cout confirms that I got to the if block where I would set board[x] to 2, but when I do showBoard afterwards, there is no update to the array. Any help?
board[x] == 2;
board[x] == 4;
Need to be:
board[x] = 2;
board[x] = 4;
Also, it may make the code simpler if you make this a 2D 4x4 array. Picking a random square would then become:
int x = rand() % 16;
int board_spot = board[x%4][x/4];