Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Urgent Help Nedded!!!

Options
2»

Comments

  • Registered Users Posts: 2,243 ✭✭✭zoro


    When is your exam? Monday? Tuesday?
    You've still got a lot of work to do man ... really, it's simple logic to figure this out.

    What does a for loop mean?
    for (int i = 0; i < 10; i++)
    { }

    The first part, int i = 0, declares the variable that will be used to control the for loop.

    Next, i < 10, creates the bounds for the loop ... ie: when it will stop. In my case, it will stop when i >= 10

    Next, i++, shows the calculation to be completed after each loop that relates to the increment (or decrement) of the counter. It can be i++, i--, i + 10, i + 672, i - 1, i/2, i/33, i*5 ..... and so on

    Now have a look at your while loop and see if you can fill in the gaps.
    int x=11, y=14;
    while (x<=27)
    {
    System.out.println ("The value of y-x is:" +y-x);
    x+=2;
    }
    
    You have the second variable, the bounds - as it's in the while loop. so variable 2 = x <= 27
    You also have the last variable, the calculation - variable 3 = x += 2
    Now, if you look closely, you also have the first variable. Variable 1 = int x = 11

    So now that you have all that you need to create the for loop - put up what you think is the answer. Taking for granted that I've almost given you the answer, it's not that hard!


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    the ans is
    y=14
    for (int x=11, x<=27; x+=2)
    {
    System.out.println ("The value of y-x is:" +y-x);
    }


  • Registered Users Posts: 2,243 ✭✭✭zoro


    the ans is
    y=14
    for (int x=11, x<=27; x+=2)
    {
    System.out.println ("The value of y-x is:" +y-x);
    }

    Yup pretty much! :)
    Now, wasn't that difficult was it? :p Break the problem up into smaller parts as I have done, then put them all together into the bigger picture.

    One thing to note is that alot of programmers tend to avoid the = sign in loops unless neccessary - ie: x <= 27 becomes x < 28

    We're all taught to obey the off-by-one thingy, such as in an array, the first element is 0, not 1, and I suppose that that's the reason that <28 just seems more natural than <=27 ..... but ignore my ramblings :p


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    What is the execution result of the following piece of code?
    (Show clearly how you calculate the output)

    int x=0;
    int y=0;
    for (int i=2; i<8; i+=2)
    {
    while (x<4)
    {
    x+=1;
    }
    y+=1;
    }
    System.out.println ("x=" +x+", y="+y);

    how do you do this? any help would be appreciated!!!


  • Registered Users Posts: 2,243 ✭✭✭zoro


    What is the execution result of the following piece of code? (Show clearly how you calculate the output)
    01: int x=0;
    02: int y=0;
    03: for (int i=2; i<8; i+=2)
    04: {
    05:    while (x<4)
    06:    {
    07:       x+=1;
    08:    }
    09:    y+=1;
    10: }
    11: System.out.println ("x=" +x+", y="+y);
    
    I think it's time that you worked it out galwaydude18 ...

    Step through it slowly. What exactly do you think is happening to the variables at each line of code?


  • Advertisement
  • Registered Users Posts: 2,243 ✭✭✭zoro


    Well you're not being very responsive - here's a nudge in the right direction.

    Everything in the for loop is going to be repeated a number of times. Inside that, the while loop will be repeated a number of times.

    As both x and y are in different scopes in the two loops, and they are both incrementing, x will increment at a faster rate than y.....


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    A friend helped me with this. he explained it to me but i still not fully understand it. from what he told me the while loop runs until it reaches 4 then it stops and comes out of the equation then the for loop continues on until it reaches8 and then your result is finally displayed! correct me if i am wrong here? heres the results i got

    i x y
    2 0 0
    2 1 0
    2 2 0
    2 3 0
    2 4 1
    4 4 2
    6 4 3
    8 4 4


  • Registered Users Posts: 2,243 ✭✭✭zoro


    Actually, my apologies, I misread the loop variables.
    i   x  y
    2  0  0
    2  1  0
    2  2  0
    2  3  1
    4  3  2
    6  3  3
    8 -> exit loop as bound: i < 8 (I must be less than 8 to continue the loop)
    
    So, that should print:

    x=3,y=3

    according to your program


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    zoro my friend is absolutly 100% convienced he is right!! can you explain to me how you got them results! my friend did it twice and got two different sets of results. your help will be appreciated in this!!!


  • Registered Users Posts: 2,243 ✭✭✭zoro


    ok, lets check it out so
    int x, y=0;
    for (int i=2; i<8; i+=2)
    {
       while (x<4)
       {
          x+=1;
       }
       y+=1;
    }
    System.out.println ("x=" +x+", y="+y);
    

    When you enter the for() loop, x and y are 0, and i = 2... 2-0-0
    On the first iteration of the for() loop, you reach a while() loop, while increments x by 1 while it is < 4....
    2-1-0
    2-2-0
    2-3-0
    2-4-0 <-- I've just added this as I forgot to increment x in my previous answer

    The while() loop now exits as x no longer satisfies the "x<4" boundary.
    Onto y+=1:
    2-4-1

    Now, on the next iteration of the for() loop, i = 4. BUT x = 4 too ... so the while() is never entered...
    4-4-2
    then...
    6-4-3

    now, i is 8, and no longer satisfies the "i<8" boundary, so the loop exits.

    From what I can see, the correct answer is:
    x=4,y=3
    :)


  • Advertisement
  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    can you please verify the very last part of the answer? my friend still reckons the last line on the table will read

    i x y
    8 4 4


  • Registered Users Posts: 2,243 ✭✭✭zoro


    That's one of the parts that I assume is intended to trick you.

    As the program never enters the for() loop once i = 8, y cannot be incremented to 4, and so stays at 3.

    Does that make sense?


  • Registered Users Posts: 2,243 ✭✭✭zoro


    And I've just tested it in java ...

    x=4, y=3


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    why can you not increment y to be 4?


  • Registered Users Posts: 2,243 ✭✭✭zoro


    Just before i is incremented to 8, y = 3
    The for loop states, that it will continue to proceed through the loop until ( i < 8 ) is no longer true.

    As soon as i becomes 8, this evaluation becomes false, so the for() loop is never entered again, and so y is never incremented (as that increment is inside the for() loop)


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    ah right!!! i see what you mean now!!


  • Registered Users Posts: 2,243 ✭✭✭zoro


    *slaps forehead* :)


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    thanks man! i will try another example or two from past papers when i finish work today and will get back on to you with how i got on!!!!


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    zoro can you change this so it is in java language! the reason i am asking this is because they only started teaching java to first years this year. this question out the paper is in C++ as far as i can figure. here it is:


    int y=13;
    for (int x=5; x<30; x+=5)
    {
    y=y+x;
    cout<< "The value of y is:" <<endl;
    }

    thanks a million!!!!


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    can someone please change this so it is in java language! the reason i am asking this is because they only started teaching java to first years this year. this question out the paper is in C++ as far as i can figure. here it is:


    int y=13;
    for (int x=5; x<30; x+=5)
    {
    y=y+x;
    cout<< "The value of y is:" <<endl;
    }

    thanks a million!!!!


  • Advertisement
  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    i love you remove yourself from here!!! we are trying to discuss something important here! everyone is being serious! offering their advice and being serious not messing about like you!!!! YOU MOTHER F***R GET LOST!!!!!!!1


  • Registered Users Posts: 2,243 ✭✭✭zoro


    int y=13;
    for (int x=5; x<30; x+=5)
    {
    y=y+x;
    cout<< "The value of y is:" <<endl;
    }
    C++ to Java? Strange ...

    Anyway, everything there is fine except the last line.
    cout << STRING << endl;
    is the same as:
    System.out.println(STRING);
    without the "endl" at the end, it'd be:
    System.out.print(STRING);


  • Registered Users Posts: 2,243 ✭✭✭zoro


    i love you remove yourself from here!!! we are trying to discuss something important here! everyone is being serious! offering their advice and being serious not messing about like you!!!! YOU MOTHER F***R GET LOST!!!!!!!1
    relax man, it looks like his account has been hacked. calm down.


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    hey zoro this guy is a joke! did you check the boards.ie home page! i see his name in almost every thread!!! this guy needs to be banned urgently!!!!!!!!!!! he is ruining everyones experience here with an annoying picture!


  • Registered Users Posts: 2,243 ✭✭✭zoro


    hey zoro this guy is a joke! did you check the boards.ie home page! i see his name in almost every thread!!! this guy needs to be banned urgently!!!!!!!!!!! he is ruining everyones experience here with an annoying picture!
    his account has been hacked - it's not really him. the mess will be sorted out by the uber-admins eventually, until then we've to put up with whoever this cooldood is. ignore him.

    Did you understand my answer?


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    zoro wrote:
    relax man, it looks like his account has been hacked. calm down.

    His account isn't hacked. If it was there would be other posts by the original person (as no one can delete old posts). Instead it is just automated spam.

    Put him on ignore and it will go away.


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    ya understand your answer! thanks for you help and patience!!! its been an excellent source of information!!!!!!!!!!


  • Registered Users Posts: 3,695 ✭✭✭galwaydude18


    hi is this the correct answer for the question that i posted?

    x y
    5 0
    10 0
    15 0
    20 0
    25 0
    30


Advertisement