What is the order in a for loop? [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to solve Problem 5 of project euler and I the answer I keep getting is wrong:
#include <stdio.h>
main()
{
int num;
int x = 0;
for (num = 20; x == 0; num++)
{
if ((num%1) == 0 && (num%2) == 0 && (num%3) == 0 && (num%4) == 0 && (num%5) == 0 && (num%6) == 0 && (num%7) == 0 && (num%8) == 0 && (num%9) == 0 && (num%10) == 0 && (num%11) == 0 && (num%12) == 0 && (num%13) == 0 && (num%14) == 0 && (num%15) == 0 && (num%16) == 0 && (num%17) == 0 && (num%18) == 0 && (num%19) == 0 && (num%20) == 0)
x = 1;
}
printf("%d %d", num, x);
}
My program keeps printing out 232792561 (I am aware that I'm printing x, this is simply for troubleshooting purposes).
The verbatim output I'm getting is: 232792561 1.
I did some research and I found that the correct answer to the problem is 232792560.
I am now beginning to believe that the problem lies in the for loop.
What does the loop do first, the iteration (num++) or the test (x == 0)?

A for loop can be converted to an equivalent while loop:
for (num = 20; x == 0; num++) {
// do stuff
}
is the same as
num = 20;
while (x == 0) {
// do stuff, then
num++;
}
So first the condition is checked, then the loop body is executed, then the increment.
(And yes, as others suggested, if you break; out of the loop when you need, you'll need the correct result, since break; immediately jumps out of the loop, thus the incrementing statement isn't executed for the last time.)

After the loop body has been executed (if at all, because first the initialisation code is run, then the condition checked to see whether the body is entered),
first the update code is run
then the condition is checked.
So after you set x to 1, num is incremented once more.
Instead of setting x to 1 to end the loop, you could simply break;, that would exit the loop without running the update code.

Your loop is needlessly complex and can be simplified using the while loop construct if you prefer. You can also get rid of the unnecessary variable x
int main() {
int num = 20;
while (!((num%1) == 0 && (num%2) == 0 && (num%3) == 0 && (num%4) == 0 && (num%5) == 0 && (num%6) == 0 && (num%7) == 0 && (num%8) == 0 && (num%9) == 0 && (num%10) == 0 && (num%11) == 0 && (num%12) == 0 && (num%13) == 0 && (num%14) == 0 && (num%15) == 0 && (num%16) == 0 && (num%17) == 0 && (num%18) == 0 && (num%19) == 0 && (num%20) == 0))
{
++num;
}
printf("%d\n", num);
return 0;
}
To answer why your for loop was giving incorrect answer:
Although you were setting x = 1 for the correct value of num, you were checking the condition of the for loop ONLY in the next iteration of the loop (i.e. after the num++ statement is executed), and hence your value of num was offset by 1.
As many people suggested, you could use the break statement to terminate the for loop execution so that the value of num is what you want it to be when the control reaches outside the loop.

Step 1: Initialization (num = 20)
Step 2: Test
Step 3: Iteration
Step 4: Test
Step 5: Iteration
and so on.
If I were you, I would choose while loop with condition as yours (I mean the long one inside for loop) with body of incrementing.

Related

Laravel Check All Multiple Requests Boolean Variable's value to True

I made an if condition for checking request boolean in laravel controller method update, it worked but the line seems too long and i want make the code in short line. And this is my code that i wrote:
if( $request->jamlak == 1
&& $request->kontrak == 1
&& $request->jamuk == 1
&& $request->sprin_pc == 1
&& $request->pc == 1
&& $request->izin_bekal == 1
&& $request->sprin_komisi == 1
&& $request->bek == 1
&& $request->komisi == 1
&& $request->bagudang== 1
&& $request->pem_gudang == 1
&& $request->bast == 1
&& $request->lpp == 1
&& $request->pemerataan == 1){
$validatedData['is_complete'] = 1;
}
else {
$validatedData['is_complete'] = 0;
}
Task::where('id', $task->id)
->update($validatedData);
return redirect('/admin/tasks')->with('success', 'New post has been updated!');
So about this code i want to make $validatedData['is_complete'] turns to be 1(true) after it checks all of requests 1 True, Is there any chance to make it way more efficient line?

Can I compare an array with 2 more arrays and see if it will meet the condition

I wanted to ask if its possible to have 1 array with two conditions for it to meet?
lets just say the arrays are
//gi.move2[10] = 1,1,1,1,1,1,1,0,1,1
//gi.move3[10] = 1,1,1,1,1,1,1,0,1,1
//pi.move1 [10] = 2,3,4,5,6,7,0,8,1,8
scanf("%d",&pi.move1[k]); // user will input 8 thus resulting a &pi.move[7] = 8
for (i = 0; i <= 10; i++)
{
if (pi.move1[7] = (gi.move2[7] == '0' || gi.move3[7] == '0'))
{
printf("x");
}
else
{
printf("-");
}
}
I tied the code above, but
I have the structure of gi.move2, gi. move3 and pi.move 1// I have no problems with those.
I am not sure how to show that this statement is true
(pi.move1[7] = (gi.move2[7] == '0' || gi.move3[7] == '0'))
I tried this also didnt work.
if (pi.move1[7] = (gi.move2[7] == 0 || gi.move3[7] == 0))
it gave me xxxx-xxxxx-xx
**
The outcome I'm looking for is
--------X--
and also once 8 has been input it cant be used again.
**
the outcome i get is ----------
Thank you
if (pi.move1[7] = (gi.move2[7] == '0' || gi.move3[7] == '0'))
the single '=' is the first problem i see
try fixing the if logic

C Operator priority at CLA certification exam

Currently, I'm taking C language certification course at cppinstitute.org.
In one of it's quizzes there is a question as below to recognize output.
int i = 1,j= 1;
int w1,w2;
w1 = (i>0) && (j<0) || (i<0) &&(j>0);
w2 = (i<=0) || (j>=0) && (i>=0) || (j<=0);
printf("%d",w1 == w2);
I think the program should print 0 to the screen, but the quiz accepts printing 1 as the answer.
Am I correct?if not ,where I'm wrong?
Thanks in advance!I'm a beginner.
Here, && higher precedence than || operator.
So,
w1 = (i>0) && (j<0) || (i<0) &&(j>0);
= 1 && 0 || 0 && 1;
= 0 || 0
= 0
And
w2 = (i<=0) || (j>=0) && (i>=0) || (j<=0);
= 0 || 1 && 1 || 0
= 0 || 1 || 0
= 1 || 0
= 1
So, w1 == w2 become false. So, correct output is 0.

Not sure why it exits the loop (in C)

I have my code to run until the user inputs "0 0 0" to stop the program
but my program stops after one loop. I tried adding a print in the inner loop to see what the values were and maybe they were all getting set 0.
my example input
5 10 6
5 3 4 2 4
output
p = 4, s = 9, c = 6
p = 3, s = 6, c = 6
p = 2, s = 4, c = 6
p = 1, s = 0, c = 6
Scenario #1: MHR rides coaster #4, using the single rider line.
I can see that p, s, and c are not all 0 so I don't know why it breaks out of the outer loop when it should just go back to asking for the 3 user input values
#include <stdio.h>
#include <stdlib.h>
int main(){
int p,s,c,h,x=1,coaster;
while(p != 0 && s != 0 && c != 0){
//number of parties, single riders, capacity of ride
scanf("%d%d%d",&p,&s,&c);
//allocate memory
int* parties = malloc(sizeof(int)*p);
for(h=0;h<p;h++){
//get size of each party in line
scanf("%d",&parties[h]);
}
//find the faster line for each scenario
int t = 0;
while(p != 0 || s > 0){
coaster = c - parties[t];
s = s - coaster;
p--;
printf("p = %d, s = %d, c = %d\n",p,s,c);
if(p == 0 && s != 0){
printf("Scenario #%d: MHR rides coaster #%d, using the regular line.\n",x,t+1);
break;
}
if(s <= 0 && p != 0){
printf("Scenario #%d: MHR rides coaster #%d, using the single rider line.\n",x,t+1);
break;
}
if(s <= 0 && p == 0){
printf("Scenario #%d: MHR rides coaster #%d, using either line.\n",x,t+1);
break;
}
t++;
}
x++;
free(parties);
}
return 0;
}
The loop condition you are using is effectively: p not zero AND c not zero AND s not zero. So when s is zero, the condition is false and the loop exits.
The condition you are looking for is NOT (p is zero AND c is zero AND s is zero):
!(p == 0 && c == 0 && s == 0)
There is another bug in the program, you don't initialise p, c or s before checking their value.
Well, you have:
int p,s,c,h,x=1,coaster;
while(p != 0 && s != 0 && c != 0){
which is not good. You check for the values of p, s and c, but they are uninitialized variables!
if you want to quit if everything becomes zero, Change:
while(p != 0 && s != 0 && c != 0)
to:
while(!(p == 0 && s == 0 && c == 0))

C while loop logic ( y != ( 1 || 0) )

I have the below code to choose sin or cos to be integrated,
while( x !=1 || y !=(1||0) ){
printf("Sin (1) or Cos (0)?\n");
x = scanf("%d",&y);
_flushall();
if(y==1){
printf("Sin set\n");
}
else if(y==0){
printf("Cos set\n");
}
}
However the
y!= (1||0)
never evaluates to true for y == 0 , can someone explain what's wrong here? Thanks.
You need (y != 1 && y != 0) (or similar, it depends on what you really mean to express there). The || operator is being applied to the operands 1 and 0. Put another way, y != (1 || 0) means "Do (1 || 0) then do y != result".
You are attempting to effectively code directly Boolean algebra, and C doesn't accept it in the manner you've provided.
while( x !=1 || y !=(1||0) )
should be
while( (x!=1) || ( (y!=1) || (y!=0) ) )
Never underestimate the value of using excess parentheses in C. The optimizer will likely optimize the code to be more efficient anyways.
The part of code that generates this error evaluates as follows:
LHS (left hand side), RHS (right hand side)
LHS = y
!= (1||0) [definition given]
!= (1) [b/c (1||0) = (1)]
y != (0||1)
is equivalent to
y != 1
since 0||1 is 1. You'll need two comparisons if you want y != 0 or y != 1.

Resources