i am getting runtime error vecause of commented if statement how to solve? - runtime-error

vector<vector<int>> floodFill(vector<vector<int>>& ig, int sr, int sc, int nc) {
int n= ig.size();
int m=ig[0].size();
// cout<<m<<endl;
queue<pair<int,int>> q;
if(ig[sr][sc]==nc)
return ig;
int t = ig[sr][sc];
q.push({sr,sc});
while(!q.empty()){
pair<int,int>v=q.front();
q.pop();
ig[v.first][v.second]=nc;
if(v.first>0){
if(ig[v.first-1][v.second]==t)
{
q.push({v.first-1,v.second});
}
}
if(v.first<n){
if(ig[v.first+1][v.second]==t)
{
q.push({v.first+1,v.second});
}
}
/*if(v.second>0){
if(ig.at(v.first).at(v.second-1) ==t)
{
q.push({v.first,v.second-1});
}
}
*/
if(v.second<m){
if(ig[v.first][v.second+1]==t)
{
q.push({v.first,v.second+1});
}
}
}
return ig;
}
how to solve this? error is in the third if statement which i have commented out.when i commented the third if statement it was compiled properly and when it was included it shows runtime error .
Can anyone help me with this?

I think the problem is v.second-1 because it's out of bound if v.second equals 0. You need to handle the bound of indices before accessing invalid (negative) indices.

Related

I got runtime error while using dfs Algorithm

I got a runtime error while using dfs for a problem.
Here is the error:
AddressSanitizer: stack-overflow on address 0x7ffce7e6eff8 (pc 0x000000359715 bp 0x7ffce7e6f040 sp 0x7ffce7e6f000 T0
Here is the question link:
(https://leetcode.com/problems/flood-fill/)
And here is my code:
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int color=image[sr][sc];
dfs(image,sr,sc,color,newColor);
return image;
}
void dfs(vector<vector<int>> &image,int sr,int sc,int color,int newcolor)
{
if(sr<0 || sr>=image.size() || sc<0 || sc>=image[0].size() || image[sr][sc]!=color)
{
return;
}
image[sr][sc]=newcolor;
dfs(image,sr,sc+1,color,newcolor);
dfs(image,sr,sc-1,color,newcolor);
dfs(image,sr+1,sc,color,newcolor);
dfs(image,sr-1,sc,color,newcolor);
}
};
It failed on the following test case:
[[0,0,0],[0,1,1]]
1
1
1
I am not able figure it out where I went wrong.
Thanks.
Check that newColor != color, otherwise you have infinite recursion, as you rely on the color change to avoid revisiting previously visited pixels.
This is precisely what happens with your failing test case.

Having problem in conversion from Recursive solution to DP

Given a Binary Tree of size N, find size of the Largest Independent Set(LIS) in it. A subset of all tree nodes is an independent set if there is no edge between any two nodes of the subset. Your task is to complete the function LISS(), which finds the size of the Largest Independent Set.
I came up with this recursive solution.
int rec(struct Node *root,bool t)
{
if(root==NULL)
return 0;
if(t==true)
{
return max(1+rec(root->left,!t)+rec(root->right,!t),rec(root->left,t)+rec(root->right,t));
}
else
{
return max(rec(root->left,!t)+rec(root->right,!t),rec(root->left,t)+rec(root->right,t));
}
}
int LISS(struct Node *root)
{
int x,y;
y=rec(root,true);
return y;
}
To solve this problem via DP, I modified the code as follows, but then it gives wrong answer.
It doesn't even work with Binary tree with distinct elements.
map<int,int> mm;
int rec(struct Node *root,bool t)
{
if(root==NULL)
return 0;
if(mm.find(root->data)!=mm.end())
return mm[root->data];
if(t==true)
{
mm[root->data]=max(1+rec(root->left,!t)+rec(root->right,!t),rec(root->left,t)+rec(root->right,t));
return mm[root->data];
}else
{
mm[root->data]=max(rec(root->left,!t)+rec(root->right,!t),rec(root->left,t)+rec(root->right,t));
return mm[root-s>data];
}
}
int LISS(struct Node *root)
{
//Code here
mm={};
int y=0;
y=rec(root,true);
return max(x,y);
}
What's the mistake?
You have two states in your function but you are memoizing only one state. Let's say for root x,
rec(x,true) = 5 and
rec(x,false) = 10 .
You calculated the rec(x, true) first and saved it in your map "mm" as mm[x] = 5.
So when you are trying to get the value of rec(x, false) it is getting the value of rec(x, true) which is 5.

JavaCC simple example not working

I am trying javacc for the first time with a simple naive example which is not working. My BNF is as follows:
<exp>:= <num>"+"<num>
<num>:= <digit> | <digit><num>
<digit>:= [0-9]
Based on this BNF, I am writing the SimpleAdd.jj as follows:
options
{
}
PARSER_BEGIN(SimpleAdd)
public class SimpleAdd
{
}
PARSER_END(SimpleAdd)
SKIP :
{
" "
| "\r"
| "\t"
| "\n"
}
TOKEN:
{
< NUMBER: (["0"-"9"])+ >
}
int expr():
{
int leftValue ;
int rightValue ;
}
{
leftValue = num()
"+"
rightValue = num()
{ return leftValue+rightValue; }
}
int num():
{
Token t;
}
{
t = <NUMBER> { return Integer.parseInt(t.toString()); }
}
using the above file, I am generating the java source classes. My main class is as follows:
public class Main {
public static void main(String [] args) throws ParseException {
SimpleAdd parser = new SimpleAdd(System.in);
int x = parser.expr();
System.out.println(x);
}
}
When I am entering the expression via System.in, I am getting the following error:
11+11^D
Exception in thread "main" SimpleAddTest.ParseException: Encountered "<EOF>" at line 0, column 0.
Was expecting:
<NUMBER> ...
at SimpleAddTest.SimpleAdd.generateParseException(SimpleAdd.java:200)
at SimpleAddTest.SimpleAdd.jj_consume_token(SimpleAdd.java:138)
at SimpleAddTest.SimpleAdd.num(SimpleAdd.java:16)
at SimpleAddTest.SimpleAdd.expr(SimpleAdd.java:7)
at SimpleAddTest.Main.main(Main.java:9)
Any hint to solve the problem ?
Edit Note that this answer answers an earlier version of the question.
When a BNF production uses a nonterminal that returns a result, you can record that result in a variable.
First declare the variables in the declaration part of the BNF production
int expr():
{
int leftValue ;
int rightValue ;
}
{
Second, in the main body of the production, record the results in the variables.
leftValue = num()
"+"
rightValue = num()
Finally, use the values of those variables to compute the result of this production.
{ return leftValue+rightValue; }
}

"Not an allowed type" in Turbo C++

I'm trying to make a grading system.So what i want to do is take 1/3 of the value of outputgrade and add it with 2/3 of the value of outputgrade2, I tried midterm1=(outputgrade()*1/3)+(outputgrade2*2/3) but I receive an error which is
Not an allowed type
Please somebody help me on what to do.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
double AAO,Quizzes,Project,MajorExam,Midterm;
void inputprelim()
{
clrscr();
gotoxy(3,4);cout<<"Input Prelim Grade";
gotoxy(1,6);cout<<"Attendance/Ass./Oral: ";cin>>AAO;
gotoxy(1,7);cout<<"Project: ";cin>>Project;
gotoxy(1,8);cout<<"Quizzes: ";cin>>Quizzes;
gotoxy(1,9);cout<<"Major Exam: ";cin>>MajorExam;
gotoxy(1,11);cout<<"Prelim Grade: ";
}
int getgrade(double a, double b, double c, double d)
{
double result;
result=(a*0.10)+(b*0.20)+(c*0.30)+(d*0.40);
cout<<setprecision(1)<<result;
return result;
}
void outputgrade()
{
getgrade(AAO,Project,Quizzes,MajorExam);
getch();
}
void inputmidterm()
{
gotoxy(33,4);cout<<"Input Midterm Grade";
gotoxy(29,6);cout<<"Attendance/Ass./Oral: ";cin>>AAO;
gotoxy(29,7);cout<<"Project: ";cin>>Project;
gotoxy(29,8);cout<<"Quizzes: ";cin>>Quizzes;
gotoxy(29,9);cout<<"Major Exam: ";cin>>MajorExam;
gotoxy(29,11);cout<<"Temporary Midterm Grade: ";
gotoxy(29,12);cout<<"Final Midterm Grade: ";
}
void outputgrade2()
{
getgrade(AAO,Project,Quizzes,MajorExam);
getch();
}
void main()
{
inputprelim();
gotoxy(15,11);outputgrade();
inputmidterm();
gotoxy(54,11);outputgrade2();
gotoxy(55,11);
Midterm1=(outputgrade()*1/3)+(outputgrade2()*2/3);
}
Your functions outputgrade() and outputgrade2() are of return type void.
In order to use them in midterm1=(outputgrade()*1/3)+(outputgrade2*2/3) you need to change their return type as int/double/float,etc.
If I have understood the logic of your code correctly then edit the two functions as:
double outputgrade()
{
return getgrade(AAO,Project,Quizzes,MajorExam);
}
double outputgrade2()
{
return getgrade(AAO,Project,Quizzes,MajorExam);
}
What I have done is changed the return type to double and at the same time the two functions are now returning whatever value getgrade returns.
outputgrade() and outputgrade2() need to return a numeric value for use in the calculation.
Right now they don't return anything.

Issue with Recursive Methods ("missing return statement")

so I have a program that is running a bunch of different recursive methods, and I cannot get it to compile/run. The error is in this method, according to my computer:
public static int fibo(int n)
// returns the nth Fibonacci number
{
if (n==0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else if (n>1)
{
return fibo(n-1) + fibo(n-2);
}
}
I have this method called correctly in my main method, so the issue is in this bit of code.
I think I can help you in this. Add return n; after your else if. Outside of the code but before the last curlicue.
The code will work as long as n ≥ 0 btw; another poster here is right in that you may want to add something to catch that error.
Make sure all possible paths have a return statement. In your code, if n < 0, there is no return statement, the compiler recognizes this, and throws the error.
public static int fibo(int n)
// returns the nth Fibonacci number
{
if (n<=0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else // All other cases, i.e. n >= 1
{
return fibo(n-1) + fibo(n-2);
}
}

Resources