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 5 years ago.
Improve this question
which is the most effective way to calculate does double is equal to another double, does it less with 1 or greater with one of it.
EDIT:
Example 18 > 18, 17 and 19 - true; 5,5 > 5,5, 4,5, and 6,5 - true.
If 18 is is in range between 17 and 19 - return true. Else return false.
If 5,5 is equal to 5,5 or it is in range of 4,5 to 6,5 - return true. Else return false.
Code started:
bool inRange(double one, double two)
{
return (one== one);
}
#include <cmath>
bool inRange(double dNumber, double dMiddle)
{
return std::fabs(dNumber - dMiddle) <= 1;
}
Related
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 last year.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
So the program must compute all the numbers that can be written as a sum of a power of 2, a power of 3 and a power of 5 below 5.000.000.
For example 42 = 16 + 1 + 25 = 2^4 + 3^0 + 5^2. Any idea how can I do this?
you can get all powers of 2 and all powers of 3 and all powers of 5 under 5.000.000. first Then you can try all combinations
vector<int> solve(){
const int M = 5000000;
vector<int> p_2={1},p_3={1},p_5={1};
while(p_2.back()*2<M)p_2.push_back(p_2.back()*2);
while(p_3.back()*3<M)p_3.push_back(p_3.back()*3);
while(p_5.back()*5<M)p_5.push_back(p_5.back()*5);
set<int> st;//to remove duplicates
for(auto power_of_2 :p_2){
for(auto power_of_3:p_3){
for(auto power_of_5:p_5){
If(power_of_2+power_of_3+power_of_5<M)
st.insert(power_of_2+power_of_3+power_of_5);
}
}
}
return vector<int>(st.begin(),st.end());
}
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 5 years ago.
Improve this question
Here is the code, I know what does it do , but I don't understand, what does the if condition do?
if(n&1)
{
for(i=n/2,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
else
{
for(i=n/2-1,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
return 0;
}
Its a bitwise operator. There's a search term for you!
The & operator provides a mask that "cancels out" bits in the first depending if they're set in the second parameter - so assume N is the number 17, that expressed in binary is 00010001, the number 1 in binary is 00000001, so masking the two together will "blank" the first set of bits, leaving you with N as 00000001.
Basically that particular if statement drops all except the last bit, which is either 0 or 1, so it is a condition detecting if N is odd or even.
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 6 years ago.
Improve this question
#include<iostream>
int fun(int &x,int y=10)
{
if(x%y==0)
return (++x);
else
return (y--);
}
int main()
{
int p=10,q=13;
q=fun(p,q);
cout<<p<<q<<endl;
q=fun(q);
cout<<p<<q<<endl;
q=fun(p);
cout<<p<<q<<endl;
return 0;
}
The output as shown is as follows:
1013
1010
1111
But when I try to solve it using pen and paper I am stuck. Please show me how to arrive at the correct output so as to keep my concepts clear.
p=10 q=13
1st call
p= 10 , q = 13
p is passed by reference
returned will be y-- since it is post decrement it will return 13
2nd call
q = 13
passed by ref
x = 13 y =10
return will be y-- which is =10 not 9 since it is post
now p = 10 and q =10
3rd call
x = 10 and y =10 by default
return ++x and x = 11 ,so q=11,now since its is call by ref x =11 so p=11 and
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
There is any function to sum relative numbers?
Example
I Have 5 and -10 So the result should be: 15
or
-5 (+) 15 -> 20
-1 (+) 1 -> 2
There is any function in C++ to sum numbers like that?
Do you intend absolute values? You can use the abs function.
abs(-5) + abs(15) gives 20 as a result.
I don't know about such function, however you can simply create one: just add absolute values:
#include <iostream>
int sumAbs( int a, int b) {
return std::abs( a) + std::abs( b);
}
int main() {
int a = -5;
int b = 10;
std::cout << sumAbs( a, b); // 15
return 0;
}
Do you mean the difference? abs(15 - (-5)) also gives 20.
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'm looking for a way to round a number to nearest number that can be divided by 4 without remainder
num = std::round(num / 4.0) * 4.0;
Here is some pseudo code. Probably not the most efficient way, but...
if num mod 4 == 0 then you are good
if num mod 4 == 1 then subtract 1
if num mod 4 == 2 then you decide (subtract/add 2)
if num mod 4 == 3 then add 1
Use the following MACRO:
#define ALIGN4(len) (((len) + 3) & ~3) // round up to 4 items