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
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 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 2 years ago.
Improve this question
I came across this code:
#include<iostream>
using namespace std;
int main()
{
int a=5,
b=a++ - 2;
cout << b;
return 0;
}
The output is 3. Why is it not 4?
-2 or - 2 should not give any error
see there are two types of operator post increment and pre increment
a++ is post increment it means first it will assign the value then it will increase the value by 1
meaning b = 5 - 2;
a will get get increased by 1
a=6 now but in the equation it will be 5
but if you do ++a
then it will first increase the value then assign
meaning b = 6 - 2;
-2 or - 2 wont give any error
check here
Lets break it down step by step.
These type of comma separated expressions happen from left to right. So its the same as this,
int a = 5;
int b = a++ - 2;
In this, a++ increments the value of a by one and then assigns it to it. Then the - 2 happens. Simply what happens under the hood is,
// Here 5 is the value of a.
int b = 5 - 2, a = a + 1;
More info: Incrementing in C++ - When to use x++ or ++x?
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;
}
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
Increment a alpha numerical string "abcd1234" upto "abcd2000"
output:
abcd1234
abcd1235
.
.
.
abcd2000
Simply:
for (int a = 1234; a <= 2000; ++a)
{
std::cout << "abcd" << a;
}
In C,
for (int i=1234; i<= 2000;i++)
printf("abcd%d\n",i);
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.