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.
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 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 4 years ago.
Improve this question
I very new at this and have an assignment in which I would like for a loop to exit if the user inputs(trans) 'e' but also end if a calculation balance(bal) is less than a constant I have set. Basically as my question states one is a character and the other an integer, will that work? I'm not trying to get people to do my homework for me, so I'm not posting all of my code or assignment, hope it makes sense.
This is the line of code I have
do {
ask user input(&trans)
e or calculation
{
while (trans != 'e'| bal < -OVR);
Just use regular unconditional loop and multiple exit conditions:
while( true ) {
char trans;
std::cin >> trans;
if( !std::cin or trans == 'e' )
break;
calculation;
if( bal > -0VR )
break;
}
So first of all you would not do unnecessary calculations, but what is more important you would make your code more readable and easier to understand - you make loop exit decision where it should be instead of pushing it into the 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 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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
int rVals[];
string rNum;
for (i=0; i < rNum.length(); ++i) {
if((rVals[i] < rVals[i+1]) && (rNum[i] =='C' || rNum[i]=='X' || rNum[i]=='I')){
continue; //checks to see if preceeding value is < the next value
} else {
valid = false;
cout << "you can't subtract by M, D, L, or V\n" << endl;
break;
}
}
rVals[] is a dynamic array and is set correctly. No matter what the input is the if statement seems to evaluate to false. what is wrong with the if statement?
Take a look at this: rVals[i] < rVals[i+1]. If rVals length is 10 for instance and i is 9 rVals[i+1] will "point" to the 11th element of the array (since the indexing of an array is starting from 0 and between 0 and 9 you heave 10 elements - the size of our array).