Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
So, there are two dates and I have to check if date1 is further away than date2. What is the best way to do that?
int date1_day = 21, date1_month = 1, date1_year = 1990;
int date2_day = 19, date2_month = 5, date2_year = 1989;
if(???)
{
// date1 is further away
}
I'm struggling with this one for hours.
Try this without using any logical operators:
int date1 = date1_day + date1_month*100 + date1_year*10000;
int date2 = date2_day + date2_month*100 + date2_year*10000;
if(date1 > date2)
printf("date1 is further away than date2\n");
It's not rocket surgery:
if ( date1_year > date2_year ||
(date1_year == date2_year && date1_month > date2_month) ||
(date1_year == date2_year && date1_month == date2_month && date1_day > date2_day))
{
// date1 is further away
}
With a little more thought I'm sure you can come up with a simpler method.
COmpared to the 5 logical operators and 6 compares of Paul, R, here a version with 4 logical operators and 5 compares):
if(date1_year>date2_year ||
(date1_year==date2_year && (date1_month>date2_month
|| (date1_month==date2_month && date1_day>date2_day))))
You can use std::tie from <tuple> to lexicographically compare multiple variables.
if (std::tie(date1_year, date1_month, date1_day) >
std::tie(date2_year, date2_month, date2_day))
{
// date1 is further away
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
cin >> a >> b >> n;
int ans=0;
c=max(a,b);
d=min(a,b);
while(n>c)
if(d+c>n) {
ans++;
break;
}
cout << ans;
}
why if I insert 1,2,2 as input the result will be 0 instead of one
If you had a debugger that you could step through the code with, the mistake would have been easy to find.
When you get to the while loop, a = 1, b = 2, n = 2, c = 2, d = 1 and ans = 0.
Since the condition n > c is false (because !(2 > 2)) the body does not get executed and you get what you started with.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
okay, so when i change the " = " in if( (i = 4) || (i = 5) ) to "==" it returns hello world. But when the "=" is kept at "=" the output is nothing. It does not give me a syntax error cuz ik you need to put "==" inside an if
void f( int i )
{
if( (i = 4) || (i = 5) ) return;
cout << "hello world\n" ;
}
int main()
{
f( 3 );
f( 4 );
f( 5 );
return 0;
}
So when the code is "if( (i = 4) || (i = 5) )" output is nothing (as in the screen is empty).
when the code is "if( (i == 4) || (i == 5) )" output is hello world.
my main question is: Why does == and = make a difference in the output but not give me a syntax error?
As #Carcigenicate said, == and = is different.
== is for comparing and = is for assigning.
An assignment a = b does not only set the value in variable a to b, but also returns the value of b. This way, an assignment like a = b = c is possible, because the value returned to be put in a is the same as has been set to b. You can use this trick in conditionals, as you've done, for example:
int x;
while(x = functionWhichCouldReturnZero()){
// Do something with x
}
When C++ tries to interpret the return value as a boolean (true or false), it interprets 0 as false, and all other values as true.
On the other hand, with (a == b), this is a pure conditional, and returns true if a is equal to b, otherwise it returns false.
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 4 years ago.
Improve this question
In code I come to situation like this:
if (a && b || c && d || e && f || g && h){
// do something
}
Like this:
if len(env.workers) == 0 && env.minQueue.Len() == 0 || len(env.workers) == len(env.daemonList) && env.minQueue.Len() == 0 || len(env.workers) > 0 && len(env.workers) == len(env.daemonList) {
env.shouldStop = true
return nil
}
But it's hard to debug and find errors. Is there any way to use more friendly constructuion to replace such statement.
As #Eugene mentioned it's always good idea to break long expressions like this into multiple smaller expressions.
exp1 = a && b
exp2 = c && d
exp3 = exp1 || exp2
exp4 = e && f
exp5 = g && h
exp6 = exp4 || exp5
exp7 = exp3 || exp6
if(exp7){
//doSomething
}
This may look absurd in beginning but believe me it has long way to go, at any point you can come back to the above code and easily understand what's cooking there. In fact if you like using debuggers then doing this would make your life way easier.
Also in point of performance, all you are doing is making extra 7 boolean variables. It's insignificant when code readability is concerned. And the thumb rule for better code readability is naming your variable right, not exp1,2,....
You use len(env.daemonList), len(env.workers) and env.minQueue.Len() multiple times. Storing them in variables not only shortens up that long condition, but also gives you variables that can be referenced when debugging.
You could write it as:
w_len = len(env.workers)
d_len = len(env.daemonList)
q_len = env.minQueue.Len()
if w_len == 0 && q_len == 0 || w_len == d_len && q_len == 0 || w_len > 0 && w_len == d_len {...
Now, of course the problem here is that while shorter, the names aren't as descriptive. You could give them better names at the cost of verbosity. How much you want to lean in each direction is a matter of taste and context.
This also doesn't "get rid" of the if like the title states, but that's not always a great goal to have. ifs aren't necessarily bad.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got an if condition as a multiples of 5, i need to check if condition until a value <= 10000. My if statement looks like this
// in main function
if(value >=0 && value <16){
function(number,value);
}
else if(value >=5 && value <10){
value-=16;
function(number,value);
}
....
// function
int function(int n, int value){
return (n<<value)|(n>>(16-value))
}
Is there a better way to do this if statement. I am new to programming world and a bit curious to know how to do this.
Thanks in advance
You could use function pointers.
typedef void (*func)();
func fpointers[] = {func1, func2, func3}
int check = value / 5;
fpointers [check] ();
Put the amounts you're subtracting from value into an array, indexed by the multiple of 5 for each range:
int subtract[] = [2, 5, ...];
if (value > 0 && value < 5*(sizeof subtract/sizeof(*subtract))) {
value -= subtract[value/5];
functioncall(value);
}
If you need to evaluate the condition for multiples of 5 , I suggest you to use swich case
int check = value/5;
switch(check)
{
case 0: // 0 <= value < 5
// do things
break;
case 1 : // 5 <= value < 10
// do things ...
break;
.............
default:
break;
}
I got an if condition as a multiples of 5
To check if a number is a multiple of 5, use the modulus operator:
if (number % 5 == 0) ...
i need to check if condition until a value <= 10000
This sounds like you need a loop.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am reading C++ Primer, 5th Edition to learn C++ however I have come across a question that I am kind of stuck at. The question is as follows:
The following expression fails to compute due to operator precedence.
How would you fix it?
string s = "word";
string p1 = s + s[s.size() - 1] == 's' ? "" : "s";
I have tried various solutions but I can't seem to get it. My logic is that the equality operator needs two expressions so I need to create that but my solutions don't work.. Any help is much appreciated!
In general, you don't want to fit your solution in to one line, so lets break this up in to the indivdual parts so we can see why it doesn't work, what we want and how we would do that.
What it is at the moment
string s = "word";
string p1 = s + s[s.size() - 1] == 's' ? "" : "s";
means:
if(s + s[s.size() - 1] == 's')
{
p1 = "";
}
else
{
p1 = "s";
}
What's wrong
It is now clear why this won't work, firstly we are comaring a string (s + s[s.size() -1]) to a character s
Also, looking at the result, I suspect that isn't what you want.
The Fix
Instead we want to append an 's' if the last character is not an s. So in long form:
if(s[s.size() - 1] == 's') // compare the last character
{
p1 = s + ""; // equivalently = s but for clarity in the next step we'll write it like this
}
else
{
p1 = s + "s"; // append the s
}
So now we can condense this back down, adding in brackets to get the desired behaviour
string p1 = s + (s[s.size() - 1] == 's' ? "" : "s");
We append something to s, where the something is determined by the last character of s
I assume you want something like this:
string p1 = s + ((s[s.size() - 1] == 's')? "" : "s");
You want something like:
string s = "word";
string p1 = s + ((s[s.size() - 1] == 's')? "" : "s");