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 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Can anyone explain to me what's happening in line number 4, and how to understand these types of loops in future.
I was solving this problem. I have used basic approaches like pow(2,n) and (1<<n), but it overflows. Then I got this solution, but I'm unable to understand that fourth line. I know how to use for() loops in C++, but I'm a bit confused because of starting, i.e. nothing is there i.e. for(; e > 0; e >>= 1).
long long modpow(long long b, int e)
{
long long ans = 1;
for (; e > 0; e >>= 1)
{
if (e & 1)
ans = (ans * b) % mod;
b = (b * b) % mod;
}
return ans;
}
The for loop has 3 components:
for (a; b; c) {
}
a runs at the start. The loop will break when b is no longer true, and after each iteration c is executed.
Related
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 months ago.
Improve this question
result = getResult( 2, 5)
int getResult(int m, int n)
{
int ans;
if (m < n)
if (n <= 10)
ans = m + n;
else
ans = m * n;
else
ans = n / m;
return (ans);
}
I am stuck between 10 and 2,
does the second else apply because the second if is true? or do i still go with the first else?
For m = 2, n = 5, the first two if conditions are valid: m < n and n <= 10. Thus, ans = m + n = 7 and it's not modified later on, so we expect 7 as the answer.
This can be much easier deduced if you properly format your code (I did it for you in this case). Also, if you use {} in if/else, that's way easier and less error-prone.
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 1 year ago.
Improve this question
I dont really understand i have written return in the end but still it gives error
CODE :
int factorial(int num)
{
int N;
if (num > 1)
{
N = (num * factorial(num--));
}
else
return N;
}
int main()
{
cout << factorial(5);
return 0;
}
ERROR : warning: control reaches end of non-void function [-Wreturn-type]
16 | }
Your issue is that you don't return anything. If you look at the flow of the program you can see that for num > 1 you do the factorial stuff and for num <= 1 you just return N. For num > 1 the return statement is never reached. This issue can be fixed by removing the else, BUT that leaves an other issue mentioned, namely that for num <= 1 N is never initialised. If you initialise it to 1 that should solve that, but as people pointed out you don't need N, you can do return num * factorial(num - 1); and simply return 1 for num <= 1. The final problem with your code is that you do num * factorial(num--). factorial(num--) will call factorial(num), when you would need factorial(num-1), because num-- is the post-decrement operator.
Other suggestions in the comments are good to heed as well, like implementing guards from integer overflow and the like.
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 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 6 years ago.
Improve this question
I hope you guys all are having a great day!
I have a quick question about using the while loop for competitive programming (we do not know the size of the input, so we have to read until the end of file or 0 value)
For this particular program, the program end with 2 values of 0 as "0 0", and the code I saw used this:
while (cin >> r >> n, r || n) {
// code
}
My question is about the >>> , r || n <<<< part:
Is the while loop as the same meaning as
while ( (cin >> r >> n) || (r || n) )
can I have some preferences to read more about the multi conditions for the while loop.
Please regard my dump question :( Tks you all for reading this post!
Basically.... comma has the lowest precedence and is left-associative.
Given A , B
A is evaluated
The result of A is ignored
B is evaluated
The result of B is returned as the result.
Further Reading : https://stackoverflow.com/a/19198977/3153883
So in your case, cin loads r and n. The return value from that operation is ignored. r or n happens and is the result of the whole while expression. So, a 0 0 will cause the while loop to terminate.
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 8 years ago.
Improve this question
I am trying to figure out the following problem for an upcoming test. I have searched everywhere, and I understand the basics of recursion. What I don't understand for this particular question is the value of int n and int k respectively. I have the answer to this question as it is a practice question, but I have no idea how the answer was found.
// Precondition: n and k are non-negative integers
int f(int n, int k) {
if (k * n == 0)
return 1
else
return f(n - 1, k - 1) + f(n - 1, k)
}
What value is returned by the call f(4, 2)?
Just look at how it's called.
f(4,2) goes into 2nd block, calls f(3,1)+f(3,2)
f(3,1) calls f(2,0)+f(2,1) = 1+f(1,0)+f(1,1)=1+1+f(0,0)+f(0,1)=1+1+1+1=4
f(3,2) calls f(2,1)+f(2,2)= f(1,0)+f(1,1)+f(1,1)+f(1,2) and so on.
You should be able to work it out from here.
I am not sure what the problem is since
f(4,2)=f(3,1) + f(3,2)
=(f(2,0)+f(2,1) )+ (f(2,1) +f(2,2))
=(1 +(f(1,0)+f(1,1))+((f(1,0)+f(1,1))+(f(1,1)+f(1,2))
=(1 + 1 +(1+1)) +( 1 +(1+1) + (1+1) +1 + 1 ))
=11