Why I am getting SIGBART error in this code of diagonal traversal of Binary tree? [closed] - c++14

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 1 year ago.
Improve this question
I am getting SIGBART error in this code. I am trying to print diagonal traversal of a binary tree.
void digonalUtil(Node* root,int dVal,map<int,vector<int> > &mp){
if(root==NULL) return;
mp[dVal].push_back(root->data);
digonalUtil(root->left,dVal+1,mp);
digonalUtil(root->right,dVal,mp);
}
vector<int> diagonal(Node *root)
{
// your code here
vector<int> ans;
if(root==NULL) return ans;
map<int,vector<int> > mp;
digonalUtil(root,0,mp);
for(auto itr=mp.begin();itr!=mp.end();itr++){
for(auto ptr=itr->second.begin();ptr!=itr->second.end();itr++){
ans.push_back(*ptr);
}
}
return ans;
}
While a bit commenting out statements, I find that the error is in the line
mp[dVal].push_back(root->data);
But I am not getting why is this happening. I had earlier done same operation on map in other questions, there it worked fine.
Please help.

I got the answer, in the second for loop I was incrementing itr rather than ptr that was causing the error, it was just a silly mistake I can say.

Related

How does "or" short-circuiting and error work in Go? [closed]

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 1 year ago.
Improve this question
if true || 0/0 == 0 {
print()
}
If the first parameter is true, then the 0/0 wouldn't be evaluated.
Why does this return a DIVIDE BY ZERO error?
The divide by zero here is a compiler error, not a runtime error. Shortcutting only applies at runtime. If you change it to 0/x where x is set to zero, you won't get the error:
var x = 0
if true || 0/x == 0 {
print()
}
https://play.golang.org/p/7E9MMqUbnQm

Map string (key) to number (or any data) [closed]

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 2 years ago.
Improve this question
In JavaScript I can do something like this:
const periods = {
'mercury': 0.2408467,
'venus': 0.61519726,
'earth': 1.0,
'mars': 1.8808158,
'jupiter': 11.862615,
'saturn': 29.447498,
'uranus': 84.016846,
'neptune': 164.79132,
};
const planetName = prompt();
const period = periods[planetName];
How can I do similar thing in go-lang?
You came very close to answering your own question. :)
Just put those tags on google.
package main
import (
"fmt"
)
func main() {
periods := map[string]float32{
"mercury": 0.2408467,
"venus": 0.61519726,
}
fmt.Println(periods["mercury"])
}

Processing - "unexpected token: void" [closed]

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 am using the below code to simulate "Purple Rain" (from The Coding Train on
YouTube).
Class Drop(){
float x = width/2;
float y = 0;
float yspeed = 1;
void fall(){
y = y + yspeed;
}
void show(){
stroke(138, 43, 226); //purple rain
line(x, y, x, y+10);
}
}
However, I am getting the error unexpected token: void on line 6:
void fall(){
I can't see any syntax errors - could someone suggest why I am getting this error?
You do have a syntax error. The Class keyword should be class with a lower-case c. You should also not have parenthesis after the class name.
Also, see my answer here. The Processing editor doesn't like when you only have a class definition without Processing functions like setup() and draw().
Side note: you might want to try approaching problems like this with a "what syntax error am I not seeing?" mentality instead of saying "I have no syntax errors" - the compiler doesn't lie, so saying you have no syntax mistakes comes off as a little bit presumptuous.

For loop inside if/else inside while. Proper syntax [closed]

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 7 years ago.
Improve this question
Why doesn't this work? I'm getting unexpected keyword_end.
while(!k && DATA[q])
if DATA[q] == 'aa' && DATA[p] == 'aa'
pl = DATA[r]
for i in 0..pl
PACK[i] = DATA[i+4]
end
k+=1 #end while
else
q++
p++
r++
end
end
You are getting error because ++ does not work in Ruby.
Ruby has no pre/post increment/decrement operator. For instance, x++
or x-- will fail to parse. More importantly, ++x or --x will do
nothing! In fact, they behave as multiple unary prefix operators: -x
== ---x == -----x == ...... To increment a number, simply write x += 1.
source (which indeed are the words of Ruby's author himself.)
You should replace them as follows:
q+=1
p+=1
r+=1

How do YOU handle bad parameters in functions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Let's say you have some function that has one int parameter that can be good or bad. Let's say that it's bad when it's less than 5. And if it's bad you should get out of function. I think you already made up that function in your mind. Now tell me which of these functions is what you would've written.
1.
void abc(int a)
{
if (a < 5) return;
//...
}
2.
void abc(int a)
{
if (a >= 5)
{
//...
}
}
This may sound like a really stupid question. But I often have hard time deciding between these two lol.
I prefer the first way:
if a < 5
// return error or throw exception
To me it looks like some kind of "guard". Also you should handle this "bad" variable somehow (return error, throw exception) and it will be harder to follow if this is in some else block somewhere in the function.

Resources