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

Quick function prob I need help with..

Options
  • 23-07-2009 5:41pm
    #1
    Registered Users Posts: 329 ✭✭


    Given the array variable data of type array<N,int>, consider the code:
    for (int i=0; i<N-1; i++) {
    if (data > data[i+1]) {
    int t = data;
    data = data[i+1];
    data[i+1] = t;
    }
    }
    Explain how this code changes the value of data.
    If
    N = 5 and data has the initial value
    < 6; 3; 4; 7; 1 >
    what will be the value of data after executing this code?

    Just want to see am I on right track, for i=0 data changes to
    <3;6;4;7;1>
    for i = 1
    <3;4;6;7;1>
    i=2
    no change
    i=3
    <3;4;6;1;7>

    Hey is the above the anwser? and if not where am II going wrong?

    Cheers...!



Comments

  • Registered Users Posts: 329 ✭✭Nappy


    this is c++ by the way.....


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    What you said looks fine to me, after a quick glance.

    If in doubt, why not just step through it in a debugger, or compile in some print statements to verify your reasoning?


Advertisement