Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys
i'm having truoble with this code.
i dont know how to read it, especially the ' || ' between the two summonus of the function.
thanks.

C++
#include <stdio.h>

int f3(int *a, int n, int m);
int f4(int *a, int n);
void f5(int *a, int n);

int main()
{
	int arr[6] = {3, 12, 6, 11, 2, 10};
	char *txt[] = {"NO","YES","MAYBE","PROBABLY"};

	printf("%s\n",txt[f3(arr,6,5)]);
	return 0;
}
int f3(int *a, int n, int m)
{
	if(m==0) return 1;
	if(n<2) return 0;
	return f3(a+1,n-1,m-a[0]) || f3(a+1,n-1,m);
}
Posted

In addition to the above, perhaps the following reworking helps better understand the issue. I believe 'enum' works the same in C as C++.

C++
#include <stdio.h>

enum Branch { InitialEntry, LeftBranch, RightBranch };
int f3(int *a, int n, int m, Branch b);
 
int main()
{
	int arr[6] = {3, 12, 6, 11, 2, 10};
	char *txt[] = {"NO","YES","MAYBE","PROBABLY"};
 
	printf("%s\n",txt[f3(arr,6,5, InitialEntry)]);
	return 0;
}
int f3(int *a, int n, int m, Branch b)
{
   if(b==InitialEntry) printf("%s %d %d\n", "Entry ",n, m);
   else if(b==LeftBranch) printf("%s %d %d\n", "Left ",n, m);
   else printf("%s %d %d\n", "Right ",n, m);
	if(m==0) return 1;
	if(n<2) return 0;
	return f3(a+1,n-1,m-a[0], LeftBranch) || f3(a+1,n-1,m, RightBranch);
}
 
Share this answer
 
v2
Comments
idobry 13-Jul-13 7:30am    
THX!
The || operator is the logical OR operator as described in http://msdn.microsoft.com/en-us/library/x04xhy0h(v=VS.80).aspx[^]. In the above code the return statement will return the logical sum of the two calls to the f3 function. As to what this function actually does, I have not analysed it so cannot tell you.
 
Share this answer
 
Comments
idobry 13-Jul-13 7:30am    
It's the first tme im seeing it outside from if statement.
now i anderstand thank you.
Richard MacCutchan 15-Jul-13 2:25am    
You should spend time reviewing the operators and expressions section of your C++ guide (or follow the link above), and remember that expressions like this can occur anywhere in your code.
H.Brydon 13-Jul-13 10:58am    
The best answer IMHO. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900