public MyTime nextSecond()
{
if(getSecond()>=0||getSecond()<=58)
return new MyTime(getHour(),getMinute(),getSecond()+1);
else if(getSecond()==59)
return new MyTime(getHour(),getMinute(),0);
else
throw new IllegalArgumentException("Invalid Second!");
}
public MyTime nextMinute()
{
if(getMinute()>=0||getMinute()<=58)
return new MyTime(getHour(),getMinute()+1,0);
else if(getMinute()==59)
return new MyTime(getHour()+1,0,0);
else
throw new IllegalArgumentException("Invalid Minute!");
}
public MyTime nextHour()
{
if(getHour()>=0||getHour()<=22)
return new MyTime(getHour()+1,0,0);
else if(getHour()==23)
return new MyTime(0,0,0);
else
throw new IllegalArgumentException("Invalid Hour!");
}
}
I am a new programmer and this is my code, it doesn't have any errors but if statements are not executing!
Does anyone know why it isn't working?
In if condition if the first statement is correct and the extra conditional is combined with OR then it return true although the second condition is false
it shouldn't be OR statement between conditions it should be AND
You are using logical OR where you should be using logical AND.
For example this:
if (getSecond() >= 0 || getSecond() <= 58)
should be
if (getSecond() >= 0 && getSecond() <= 58)
In your version if the value returned by getSecond() is 59 it will never reach the else if because the first if statement will evaluate getSecond() > 0 to true and because it is logical OR, it will not evaluate the second logical expression in that condition (getSecond() <= 58).
The same goes for minutes and hours.
Related
the printOptimalAlignment function is misbehaving. goto and return will not exit when the function reaches location (1,1)... where it should end, no crash and it stops at seemingly an arbitrary location of (6,6)... because for some reason it increments at the end of the function even though there is no increment-er for the values int yL, int xL, (but I don't follow why it calls itself if it gets to the end of the function without any "hits" on the if statements.
Full code:
https://repl.it/#fulloutfool/Edit-Distance
void printOptimalAlignment(int** arr, string y, string x,int yL, int xL){
int I_weight=1, D_weight=1, R_weight=1;
bool printinfo_allot = 1,printinfo = 1;
if(printinfo_allot){
cout<<"Location: "<<"("<<xL<<","<<yL<<")"<<"-------------------------------\n";
cout<<"Same check Letters: "<<x[xL-2]<<","
<<y[yL-2]<<"("<<(x[xL-2] == y[yL-2])<<")"<<"\n";
cout<<"LL: "<<"("<<xL-1<<","<<yL<<")"
<<":"<<arr[yL][xL-1]
<<":"<<(arr[yL][xL-1]+I_weight)
<<":"<<(arr[yL][xL])
<<":"<<(((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1)
<<":"<<(yL>=1 && xL>=1)<<"\n";
cout<<"xL state:"<<((&x[xL]))<<":"<<(x[xL-1])<<"\n";
cout<<"yL state:"<<((&y[yL]))<<":"<<(y[yL-1])<<"\n";
string tx = &x[xL];
cout<<x.length()<<","<<(tx.length()+1)<<"\n";
}
string tx = &x[xL]; // slopy hotfix
if(x.length()==(tx.length()+1)){
cout<<"return functionality not working?-=-=-=-=-=-=-=-=\n";
cout<<"-> Prep last, current distance = "<<arr[yL][xL] <<"\n";
return;
//printOptimalAlignment(arr,y,x,yL-1,xL-1);
//cant use this goto... but where does it go?
//goto because_Im_a_terrible_person;
throw "how?... breaking rules... make it stop";
}
if(yL>=1 && xL>=1 && (x[xL-2] == y[yL-2]) == 1){
if(printinfo){
cout<<"-> Same (same char), current distance = "<<arr[yL][xL] <<"\n";
}
printOptimalAlignment(arr,y,x,yL-1,xL-1);
}
if(yL>=1 && xL>=1 && (arr[yL-1][xL-1] == arr[yL][xL])){
if(printinfo){
cout<<"-> Swap (same int), current distance = "<<arr[yL][xL] <<"\n";
if(arr[yL-1][xL-1]==0)cout<<"---this is last---\n";
}
printOptimalAlignment(arr,y,x,yL-1,xL-1);
}
if(yL>0 && xL>0 && (arr[yL-1][xL]+D_weight == arr[yL][xL])){
if(printinfo){
cout<<"-> Delete, current distance = "<<arr[yL][xL]<<"\n";
}
printOptimalAlignment(arr,y,x,yL-1,xL);
}
//really weird ((yL>1 && xL>1) && (((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1))
//not true if it is?
bool seperate = (((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1);
if(yL>=1 && xL>=1){
if((((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1) && (true)){
if(printinfo){
cout<<"-> Insert, current distance = "<<arr[yL][xL]<<"\n";
cout<<"Next Location1: "<<"("<<xL-1<<","<<yL<<")"<<"\n";
}
printOptimalAlignment(arr,y,x,yL,xL-1);
return;
//how does it get here... also return gets ignored... prob another stack issue
cout<<"insert function broke?????? # (1,1) ???????????????\n";
//return;
}
}
return;
cout<<"END... Hopefully.. if you see this Something went wrong\n";
because_Im_a_terrible_person:
cout<<"QUIT\n";
}
I suspect your problem is that your function calls itself and you don't appear to be taking into account what should happen next after that call to itself finishes. So you get to your finish condition where you say the return doesn't work, but it does... it just returns to where you left off in the previous call to printOptimalAlignment, which still might do something before returning to its caller, and so on. You have three different sites where you recursively call printOptimalAlignment that aren't immediately followed by a return statement, and at any of these it might be that the code will continue and trigger another of your conditional blocks.
I a newbie C++ programmer trying to test aruments/parameters passed to a program.
Multiple arguments can be passed to the program, however I want to test that if certain arguments are passed then other arguments become invalid.
e.g. PGM accepts arg(1) arg(2) arg(3) arg(4) arg(5) etc...
if arg(1) and arg(2) are supplied then arg(3), arg(4) and arg(5) etc... are invalid and the program should terminate with an error message if they are also supplied along with arg(1) and arg(2).
I've thought that using boolean IF tests would be a good way to check if certain values are true/false.
I searched on stackoverflow but not found an answer that encompasses exactly what i'm trying to do. If someone can point me in the right direction or suggest a far more efficient way of doing this I would be very grateful.
My code currently looks like this:
bool opt1 = false;
bool opt2 = false;
bool opt3 = false;
bool opt4 = false;
bool opt5 = false;
for(int i=1; i<argc; i++) {
char *str = argv[i];
if (strcmp (str, "-opt1:")==0) {opt1 = true;}
else if (strcmp (str, "-opt2:")==0) {opt2 = true;}
else if (strcmp (str, "-opt3:")==0) {opt3 = true;}
else if (strcmp (str, "-opt4:")==0) {opt4 = true;}
else if (strcmp (str, "-opt5:")==0) {opt5 = true;}
}
if((opt1) && (opt2) && (~(opt3)) && (~(opt4)) && (~(opt5)) {
** DO SOMETHING **
} else {
** DISPLAY ERROR MESSAGE AND USAGE TEXT **
}
A good solution would be using operands ! and &&
! denotes "not" (or in such case "not true") while && combines two different logical comparisons (in such case, "logic test 1" and "logic test 2")
Here's an example to do it:
if((opt1 && opt2)&&(!(opt3||opt4||opt5))){
/*
Do something if opt1 and opt2 are true and others are false
*/
}
This is practically the same as #Fareanor's solution above (first solution)
A possible fix could be (if I have well understood your problem):
if(opt1 && opt2) // opt3, opt4 and opt5 are invalid
{
if(!(opt3 || opt4 || opt5))
{
// Do something
}
else
{
// Display error message because at least opt3 or opt4 or opt5 is provided and not requested
}
}
else // opt3, opt4 and opt5 are valid
{
// Do something
}
But I think it could be better to just ignore the obsolete parameters instead of display an error while you can still run your process with only opt1 and opt2. Which could lead us to the simpler code:
if(opt1 && opt2)
{
// Do something without using opt3, opt4 and opt5
}
else
{
// Do something taking into account opt3, opt4 and opt5
}
I hope it is what you was looking for.
Here is my code:
bool BinarySearchTree::CheckIfTreeIsBinary(){
bool isBinary=true;
isBinary=CheckIfTreeIsBinaryPrivate(root); // So if my tree is binary, this function does not return anything
// and isBinary should remain true, but it is false.
return isBinary;
}
bool BinarySearchTree::CheckIfTreeIsBinaryPrivate(nodePtr Ptr){
if(Ptr->left!=NULL){
CheckIfTreeIsBinaryPrivate(Ptr->left);
}
if(Ptr->left!=NULL){
if(Ptr->data<Ptr->left->data)
return false; // possibility 1 to return false
}
if(Ptr->right!=NULL){
if(Ptr->data>Ptr->right->data)
return false; // possibility 2 to return false
}
if(Ptr->right!=NULL){
CheckIfTreeIsBinaryPrivate(Ptr->right);
}
}
In my function CheckIfTreeIsBinary, I have set boolean isBinary to true for default value. After that, isBinary is assigned to function CheckIfTreeIsBinaryPrivate, which will not return anything if the tree is binary.
The problem is that function CheckIfTreeIsBinaryPrivate doesn't return anything if the tree is binary, but in the end, isBinary is false.
The problem is that CheckIfTreeIsBinaryPrivate does not have an explicit return value on all program control paths.
That means that the behaviour of your program is undefined.
Your compiler will warn you of this, and it's your job to heed those warnings.
Your recursive logic is incorrect. All paths in a function should return a value and you should always check the return value of recursive calls to CheckIfTreeIsBinaryPrivate. There's no concept of 'a value remaining the same'. Here's what I think you are trying to achieve, but it's quite complicated.
bool BinarySearchTree::CheckIfTreeIsBinaryPrivate(nodePtr Ptr) {
return
// check the left sub tree is ok
(Ptr->left == NULL || // NULL is ok OR
(Ptr->data >= Ptr->left->data && // data >= left->data && left is ok
CheckIfTreeIsBinaryPrivate(Ptr->left))) &&
// and check the right sub tree is ok
(Ptr->right == NULL || // NULL is ok OR
(Ptr->data <= Ptr->right->data && // data <= right->data && right is ok
CheckIfTreeIsBinaryPrivate(Ptr->right)));
}
I think I see where your misunderstanding is; you're expecting isBinary to be updated only if CheckIfTreeIsBinaryPrivate returns a value.
That's not how it works - a function that has a non-void return type must always return something.
If the function doesn't explicitly return anything - for instance, by reaching the end of the function - the program has undefined behaviour.
There is no way for you to determine whether a function returned anything or not, and you must return something on every path through the function.
You can do this with one big expression,
bool BinarySearchTree::isBST(nodePtr Ptr){
return Ptr == nullptr
|| ( (Ptr->left == nullptr || (Ptr->left->data < Ptr->data && isBST(Ptr->left)))
&& (Ptr->right == nullptr || (Ptr->right->data > Ptr->data && isBST(Ptr->right))));
}
or piece by piece:
bool BinarySearchTree::isBST(nodePtr Ptr){
// An empty tree is a BST.
if (Ptr == nullptr)
return true;
// If the left subtree is not a BST, neither is the entire tree.
else if (Ptr->left != nullptr && (Ptr->left->data > Ptr->data || !isBST(Ptr->left)))
return false;
// If the right subtree is not a BST, neither is the entire tree.
else if (Ptr->right != nullptr && (Ptr->right->data < Ptr->data || !isBST(Ptr->right)))
return false;
// All tests passed.
else
return true;
}
Add one more base condition in CheckIfTreeIsBinaryPrivate() to set true,
because once you assign isBinary to CheckIfTreeIsBinaryPrivate() it will set false by default, and you need a return value to get true.
I have this little function that is giving me trouble, only part of the function will follow my conditions at one time.
bool trend()
{
//is there a trend?
close1 = iClose(NULL,0,1); //vars
close2 = iClose(NULL,0,2);
close3 = iClose(NULL,0,3);
open1 = iOpen(NULL,0,1);
open2 = iOpen(NULL,0,2);
open3 = iOpen(NULL,0,3);
if(close3 > open3 && close2 > open2 && close1 > open1)
{
return(true); //uptrend
}
else if(close3 < open3 && close2 < open2 && close1 < open1)
{
return(false); //downtrend
}
else return(EMPTY_VALUE);
}
This is how the function gets called, under int start()
trending = trend();
if (trending == true) Order = SIGNAL_BUY; // Rule to ENTER a Long trade
if (trending == false) Order = SIGNAL_SELL; // Rule to ENTER a Short trade
As written above, my sell signal will work following the conditions, but the buy signals don't follow the conditions, and I can't figure out how they are triggering.
If I remove the "else return(EMPTY_VALUE);" then the buy orders start following the condition but the sell orders no longer follow the conditions. The broken sell order seems to behave like the broken buy order was.
Any ideas why my function is behaving like this? Thanks!
Your function is declared as bool trend(), which means that it can return either true or false. In the line else return(EMPTY_VALUE), the constant EMPTY_VALUE (which has the value 0x7FFFFFFF according to the MQL documentation) is implicitly converted to true. This means that your function will return true (which emits your buy signal) if there is no uptrend and no downtrend.
If you leave out the last line else return(EMPTY_VALUE) you have a missing return statement. This leads to undefined behaviour if you try to access the return value of the function, which you do in the line trending = trend().
To sum it up: Your problem is that the trend function can only return one of two values, true or false. But what you need is a function that returns one of three values uptrend, downtrend, no_trend. You could declare an enum with those three values and change the return type accordingly:
enum Trend {
UPTREND,
DOWNTREND,
NONE
}
Trend trend() {
// check if there is a trend
// [...]
if (close3 > open3 && close2 > open2 && close1 > open1) {
return UPTREND;
}
else if (close3 < open3 && close2 < open2 && close1 < open1) {
return DOWNTREND;
}
else {
return NONE;
}
}
and then later
Trend trending = trend();
if (trending == UPTREND) Order = SIGNAL_BUY;
if (trending == DOWNTREND) Order = SIGNAL_SELL;
Assume I have code like:
if(condition1 || condition2 || condition 3 || condition4)
{
// this inner part will be executed if one of the conditions is true.
// Now I want to know by which condition this part is executed.
}
I'm sure there are better ways to do this, here's one:
int i = 0;
auto check = [&i](bool b)->bool
{
if (!b) ++i;
return b;
};
if (check(false) || // 0
check(false) || // 1
check(true) || // 2
check(false)) // 3
{
std::cout << i; // prints 2
}
|| is short circuit evaluation, so you can have code like this :
if(condition1 || condition2 || condition 3 || condition4)
{
if (condition1 )
{
//it must be condition1 which make the overall result true
}
else if (condition2)
{
//it must be condition2 which make the overall result true
}
else if (condition3)
{
//it must be condition3 which make the overall result true
}
else
{
//it must be condition4 which make the overall result true
}
// this inner part will executed if one of the condition true. Now I want to know by which condition this part is executed.
}
else
{
}
If the conditions are independent of each other, you need to check them separately, or, if they belong to one variable, you can use a switch statement
bool c1;
bool c2
if ( c1 || c2 )
{
// these need to be checked separately
}
int i; // i should be checked for multiple conditions. Here switch is most appropriate
switch (i)
{
case 0: // stuff
break;
case 1: // other stuff
break;
default: // default stuff if none of the conditions above is true
}
Without a switch you can use only or and if statements:
if(condition1 || condition2 || condition 3 || condition4) {
// this inner part will executed if one of the condition true.
//Now I want to know by which condition this part is executed.
if ( condition1 || condition2 ) {
if ( condition1 )
printf("Loop caused by 1");
else
printf("Loop caused by 2");
else
if ( condition3)
printf("Loop caused by 3");
else
printf("Loop caused by 4");
}
I'm not sure that this is the most efficient thing you've ever seen, but it will identify which of the four conditions caused entry into the if ... block.
If you need to know for programmatic reasons, i.e. run different code depending on which condition is true, you could do something like this
if (condition1)
{
...
}
else if (condition2)
{
...
}
else if (condition3)
{
...
}
else if (condition4)
{
...
}
else
{
...
}
If you only want to know for debugging reasons, just do a printout.
What about the comma operator?
By using that logical operators follow the short circuit evaluation method, the following works fine:
int w = 0; /* w <= 0 will mean "no one is true" */
if ( (w++, cond1) || (w++, cond2) || ... || (w++, condN) )
printf("The first condition that was true has number: %d.\n", w);