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

array problem in c++ need help with...

Options
  • 28-07-2009 1:59pm
    #1
    Registered Users Posts: 329 ✭✭


    int t = a[0]; / first element of array is becomes int t
    i = 0; / initialising i
    while (i < 99) { /starting loop
    a = a[i+1];
    i = i + 1;
    }
    a = t; /the last element of data is now a[99]= 1

    hey just getting my head around arrays, In the code above am I right in assuming say given data < 1,2,3,4,5>
    it is changing it to <2,3,4,5,1> ? I left comments on my understanding of each line, let me know if Im wrong please...


    Cheers




Comments

  • Registered Users Posts: 329 ✭✭Nappy


    sorry where 99 is replace it with 5,

    Thanks


  • Closed Accounts Posts: 7,794 ✭✭✭JC 2K3


    t does nothing, and you'll get a SegFault/Out of Bounds Error/You'll access memory not allocated to your array when i is 5. i.e. you'll be accessing a[6] for a[i+1];

    You need to use a modulus. Also, consider using a for loop.
    int array_size = 5;
    for (int i  = 0; i < array_size; i = i + 1)
    {
      a[i] = a[(i+1) % array_size];
    }
    

    EDIT: missed the last line of your code, the t does something! I prefer using a modulus though.


  • Registered Users Posts: 1,228 ✭✭✭carveone


    Nappy wrote: »

    int t = a[0]; / first element of array is becomes int t
    i = 0; / initialising i
    while (i < 5) { /starting loop
    a = a[i+1];
    i = i + 1;
    }
    a = t; /the last element of data is now a[5]= 1

    hey just getting my head around arrays, In the code above am I right in assuming say given data < 1,2,3,4,5>
    it is changing it to <2,3,4,5,1> ? I left comments on my understanding of each line, let me know if Im wrong please...


    Almost. With arrays you need to count it through in your head and watch your maxs and mins. An array like you mention ranges from a[0] to a[4], i goes from 0 to 4 but you reference i+1 which is a max of 5. So you access a[5] which is wrong.

    Long winded way of saying go from 0 to 4 :rolleyes:
    ie: while (i < 4)

    The rest of it looks right. Loops like this always force you to think really carefully to avoid counting off the end of arrays. I've done it several times in the past with interesting results. Learning to use the assert() function can help. It actually used to be easier with the old Turbo C (1989 vintage, yes I'm old) which had an IDE with single step debugger. So you could press F5 to step through each line of code at a time, watching what all the variables did at each step.


  • Registered Users Posts: 4,769 ✭✭✭cython


    Not quite. Take a look at the termination condition on your loop, and your array indexing. When it finishes, i will be 5, so if you assign to a after the loop, then you'd be assigning to a[5], or the 6th element along the array, assuming the array is large enough to hold 6 elements.

    Also, you're looking at reading from a[5] within your loop, which may in fact be uninitialised, hence the above comment about indexing.


  • Closed Accounts Posts: 5,361 ✭✭✭Boskowski


    That's how I'd do it:

    #define ARRAY_ELMS_CNT 100

    // initilaizing: whether it seems to make sense or not, always do it
    int nArray[ARRAY_ELMS_CNT] = {0};

    // first loop: assign the initial values
    for (int i = 0; i < ARRAY_ELMS_CNT; i++) {
    nArray = i + 1;
    }

    // second loop: shift it around using the modulo operator for the index boundary
    for (int i = 0; i < ARRAY_ELMS_CNT; i++) {
    nArray = i + 1 < ARRAY_ELMS_CNT ? nArray[i + 1] : nArray[(i + 1) % ARRAY_ELMS_CNT];
    }

    Didn't actually test this, also I'm sure there's more elegant solutions, but I think it's right anyway and not as unsafe...


  • Advertisement
  • Closed Accounts Posts: 7,794 ✭✭✭JC 2K3


    Why bother with the condition?
    i + 1 == (i + 1) % ARRAY_ELMS_CNT where i + 1 < ARRAY_ELMS_CNT, there's no point in checking....


  • Closed Accounts Posts: 580 ✭✭✭karlr42


    Why can no-one on the Programming board use [code] tags? :(


  • Registered Users Posts: 1,269 ✭✭✭cocoa


    I'll be happy to admit my mistake if so, but as far as I can tell a lot of people are forgetting that if you simply loop around the array using a modulus, your last write will be incorrect. i.e., <1,2,3,4,5> goes to <2,3,4,5,2> instead of <2,3,4,5,1>. The extra variable 't' is needed.

    My version would be :
    int t = a[0]; // save value of first element in 't'
    int i = 0; // initialising i
    for(i=0;i<array_size-1;i=i+1){
    a[i] = a[i+1];
    }
    a[array_size-1] = t;
    // no need to go trying to figure out what i will be, we just want the last item in the array
    


Advertisement