Jump to content

Coder


bullet-401

Recommended Posts

This should be a lot of fun. NOFX, great idea.

 

RULES: Code should have a number or word answer to it. This is on a trust system too. No using code programs. One that answers the code correct gets to make up another. There is a BB code thing.. use it.

 

NOFX's code

 

while (x <=10, x++);
{
i = i+x;
}
print i;

 

Answer was 55.

 

 

 

 

My new one.

 

int y,x;
int n=0;

for (x=0;x<=10;x++){
for (y=10;y>0;y--){
x++;
if ((x>5)&&(y>1))
n++;
} 
}

What is n?

Link to comment
Share on other sites

Member
(edited)

I have never programmed in C++. I have done a little bit of C, Syntax doesnt really matter, its all the same :P It looks like Java to me.

 

Ok, I rethought this,

 

im confused because you have two x's

 

the varible x that is incremented on the inside loop and the outter for loop has x as a counter. perhaps make these two separate varibles?

 

if we had

int y,x,i;

int n=0;

 

for (i=0;i<=10;i++){

for (y=10;y>0;y--){

x++;

if ((x>5)&&(y>1))

n++;

}

}

 

I think the answer would be.....

Edited by NOFX
Link to comment
Share on other sites

Member
(edited)

everytime the inner loop is completed n++ is called 4 times. If the outer loop is ran 11 times(because x is initally 0 so it would really execute the loop 11 times), the answer would be 44. How about that?

Edited by NOFX
Link to comment
Share on other sites

But NoFX, look at the Y-loop. It increments X.

yea thats what confuses me, thats why I set up three varibles instead of 2. so if we leave it at 2 we have.

int y,x;

int n=0;

 

for (x=0;x<=10;x++){

for (y=10;y>0;y--){

x++;

if ((x>5)&&(y>1))

n++;

}

}

 

What is n?

 

K with this, I think we have x=10, n=4 after the first outer loop. x<=10 so we can do the outer loop once again. This time the inner loop will go run but never increment n, because x will always be greater than 5.

 

so the answer is 4? and the outer loop is useless? where u at bullet?

Link to comment
Share on other sites

Lol forgot about this topic. I went through it and got this.

 

X Y N

0 10 0

1 9 1

2 8 2

3 7 3

5 6 4

6 5 5

7 4

8 3

9 2

10 1

11 0

 

I think thats right. Not really sure since I made it up on the fly. So I got X=11 Y=0 N=5. Not sure if thats right though.

Link to comment
Share on other sites

Lol forgot about this topic. I went through it and got this.

 

X Y N

0 10 0

1 9 1

2 8 2

3 7 3

5 6 4

6 5 5

7 4

8 3

9 2

10 1

11 0

 

I think thats right. Not really sure since I made it up on the fly. So I got X=11 Y=0 N=5. Not sure if thats right though.

I dont think that is right, because your incrementing N on the second pass when x=1 & y=9. Impossible if n++ only when x>5 & y>1

Link to comment
Share on other sites

Oh sorry for the confusion. I just put down each time it changes. Now I fixed it so each row is one pass.

int y,x;

int n=0;

 

for (x=0;x<=10;x++){

for (y=10;y>0;y--){

x++;

if ((x>5)&&(y>1))

n++;

}

}

 

X Y N

0 10 0 <--Starts both for statements

1 9 0 <--\/Cycles down till y gets to 0\/

2 8 0

3 7 0

5 6 0

6 5 1

7 4 2

8 3 3

9 2 4

10 1 5

11 0 5 <-- Exits 2nd for statement

12 0 5 <---final after the initial for statement is done.

Link to comment
Share on other sites

x=0 y=10 n=0

x=1 y=9 n=0

x=2 y=8 n=0

x=3 y=7 n=0

x=4 y=6 n=0

x=5 y=5 n=0

x=6 y=5 n=1

x=6 y=4 n=1

x=7 y=4 n=2

x=7 y=3 n=2

x=8 y=3 n=3

x=8 y=2 n=3

x=9 y=2 n=4

x=9 y=1 n=4

x=10 y=0 n=4

x=11 y=0 n=4

Link to comment
Share on other sites

heh yea I had 4 until I changed his code to where the outer loop didnt use x :P. Someone else post some code, whenever I get some time ill reinstall jsdk, im gonna be needing it for Java III in the fall, will be my third attempt at the class.

 

you could just look up the vbscript syntax, type it in notepad and throw a vbs extension on it and it should work fine.

Link to comment
Share on other sites

Ok I ran this

 

Sub test()
Dim N, X, Y As Integer
N = 0
For X = 0 To 10
For Y = 10 To 1
X = X + 1
If X > 5 And Y > 1 Then
N = N + 1
End If
Next Y
Next X
MsgBox (N)
MsgBox (X)
MsgBox (Y)


End Sub

 

I got N=0 X=11 Y=10. SO, my syntax is wrong in VB. Can anyone look at this and fix it?

Link to comment
Share on other sites

it will never cease to amaze me how often a c-based programmer can't figure out how to do things in VB...I mean seriously...it's SUPPOSED to be a less complicated language here

Yeash. It is very confusing. I never took VB and I probly never will. :sigh: The basics of it is very easy but I still don't get the logic of it.

Link to comment
Share on other sites

Yeah, this is really obscure...

 

FOR Y = 10 TO 1 STEP -1

 

Otherwise BASIC will always assume you want to increment by 1. So, Y = 10, then next loop, Y = 11, which exceeds the bounds....I'm pretty sure it always runs through the loop at least once. Add that STEP -1 though. It's like the y-- operator.

Link to comment
Share on other sites

×
×
  • Create New...