I am finding it hard to understand Recursion. can anybody tell what's wrong in writing cout<< with the print3 call..
void print3(char a[],int i)
{
if(a[i] == '\0')
{
return;
}
else
{
cout<<print3(a,i+1);
}
}
Related
Today when I am solving Fibonacci arrays, I meet with a very strange thing. Recursion only takes 16ms, but iteration takes 80ms. I have tried to optimize my iteration (such as I use a vector container to fulfill my stack) but iteration is still much slower than recursion. It doesn't make sense because recursion still builds a stack at OS level, which is more time-consuming than iteration.
Here is my iteration code:
class Solution {
public:
int fib(int n) {
std::stack<int, std::vector<int>> st;
st.push(n);
int result = 0;
int temp = 0;
while(!st.empty()) {
temp = st.top(); st.pop();
if(temp == 1) result++;
else if(temp == 0) continue;
else {
st.push(temp - 1);
st.push(temp - 2);
}
}
return result;
}
};
Here is my recursion code
class Solution {
public:
int fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
else return fib(n - 1) + fib(n - 2);
}
};
Well, I have searched for the reason. According to Is recursion ever faster than looping?, recursion is more time-consuming than iteration in an imperative language. But C++ is one of the imperative languages, it is not convincing.
I think I find the reason. You can help me check if there is any incorrect in my analysis?
The reason why recursion is faster than iteration is that if you use an STL container as a stack, it would be allocated in heap space.
When the PC pointer wants to access the stack, cache missing might happen, which is greatly expensive as for a small scale problem.
However, as for the Fibonacci solution, the code length is not very long. So the PC pointer can easily jump to the function's beginning. If you use a static int array, the result is satisfying.
Here is the code:
class Solution {
public:
int fib(int n) {
int arr[1000];
arr[0] = n;
int s = 1;
int result = 0;
int temp;
while (s) {
temp = arr[s-1];
s--;
switch (temp) {
case 1:
result++;
break;
case 0:
continue;
break;
default:
arr[s++] = temp - 1;
arr[s++] = temp - 2;
}
}
return result;
}
};
I am doing an iterative search over a binary tree using the stack. But I am getting a segmentation fault. I have crossed checked it many time but couldn't find anything. Please help.
void inOrder(struct Node *root)
{
stack<Node *> s;
Node *t;
s.push(root);
while (s.empty() == false) {
t = s.top();
while (t->left != NULL) {
s.push(t->left);
t = t->left;
}
while (1) {
t = s.top();
s.pop();
cout << t->data << " ";
if (t->right != NULL) {
s.push(t->right);
break;
}
}
} /* end of while */
}
while(1){
should be:
while (!s.empty()) {
I am trying to use a local variable to syncronise among all the work-items in a work-group. However else part on conditional check always fails. Value of d[0] for other work-items does not equals to zero. Why local variable is not visible in the work-group?
I am using AMD APU A12-9800
__kernel void test(__global int *input_vector,__global atomic_int *mem_flag)
{
local int d[32];
if(get_local_id(0)==0) {
d[0] = 100;
}
barrier(CLK_GLOBAL_MEM_FENCE| CLK_LOCAL_MEM_FENCE);
while(1) {
if(get_local_id(0) == 0) {
d[0] = 0;
break;
}
else {
if(d[0] == 0)
break;
}
}
}
as suggested by #alexg I added the barrier along with else condition removed and it worked. Here is the full code
__kernel void test(__global int *input_vector,__global atomic_int *mem_flag)
{
local int d[32];
if(get_local_id(0)==0) {
d[0] = 100;
}
barrier(CLK_GLOBAL_MEM_FENCE| CLK_LOCAL_MEM_FENCE);
while(1) {
mem_fence(CLK_GLOBAL_MEM_FENCE| CLK_LOCAL_MEM_FENCE);
if(d[0] == 0)
break;
if(get_local_id(0) == 0) {
d[0] = 0;
}
}
}
Is there a possibility to add an else after a while-Loop in Arduino, like there is in Java or C#? Something like this:
while(condition){
doThingA;
} else{
doThingB;
}
C# does not have while...else and I don't think Java has this construct either. Python has it and because the instructions in the else block are not executed only when you break from the loop you can emulate it as follows:
bool flag = TRUE;
while (condition)
{
if (anothercondition)
{
flag = FALSE;
break;
}
}
if (flag)
{
...
}
No maybe, it's not actually because it is based on embedded c, which includes syntaxes similar to c. So to conclude it simply can't. was a pleasure to help you.
ESPserial.print(params + '\n');
time = millis();
while(true) {
int avail = ESPserial.available();
if (avail) break;
if (millis() - time > 500) break;
}
do {
char ch = ESPserial.read();
data += ch;
} while (ESPserial.available());
You can't have a else after a whole loop but u can put a exclamation(!) before the statement like:
while (!condition)
orwhile(value1 != value2)
struct node
{
int data;
node* left;
node* right;
};
int secondlargest(struct node* a)
{
while(a->right != NULL){
secondlargest(a->right);
}
return a->data;
}
I am not able to trace where have I done the mistake and why its not coming out of the while loop.
Your mistake is that you shouldn't use an while but instead an if because it is recursive, but what do you want the function to return? the data of the last member? if so it should be like this:
int secondlargest(struct node* a) {
if(a == NULL) return -1;
secondlargestr(a);
}
int secondlargestr(struct node* a) {
if(a->right!=NULL) return secondlargest(a->right);
return (a->data);
}
If you insist on the recursive version, change the while to if.
int secondlargest(node* a)
{
if(a == null){
// if the first node is already NULL
return -1;
}
if(a->right == NULL){
return a->data;
}else{
return secondlargest(a->right);
}
}
Basics of recursion:
Must have base case
Break down problem size recursively
If you want the iterative way:
int secondlargest(node* a)
{
node* temp = a;
int data = -1;
while(temp != null){
data = temp->data;
temp = temp->right;
}
return data;
}