off by one and help with swap

This is a discussion on off by one and help with swap within the C++ Programming forums, part of the General Programming Boards category; yesterday I posted on this same code and for the most part your help was great. but no one answered ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Exclamation off by one and help with swap

    yesterday I posted on this same code and for the most part your help was great. but no one answered this particular problem

    i have a list of accounts, i need to place them in descending order, preferable while they are being entered.

    there is the code i have so far:

    Code:
    -------------------------------------------------------------------------------------------------
     void sort(int accounts[],double balance[], int& total)
    {
        int index_of_next_smallest;
        for (int index = 0; index < total - 1; index++)
        {
            index_of_next_smallest =
                         index_of_smallest(a, index, number_used);
            swap_values(a[index], a[index_of_next_smallest]);
            //a[0] <= a[1] <=...<= a[index] are the smallest of the original array
            //elements. The rest of the elements are in the remaining positions.
        }
    }
     */
    /*----------------------------------------------------------------------------*/
    /*----------------------------------------------------------------------------
    int index_of_smallest(int account[],int NUMBER int account[0], int& total)
    {
        int min = a[start_index],
            index_of_min = start_index;
        for (int index = start_index + 1; index < number_used; index++)
            if (a[index] < min)
            {
                min = a[index];
                index_of_min = index;
                //min is the smallest of a[start_index] through a[index]
            }
    
        return index_of_min;
    } */
    /*------------------------------------------------------------------------------
    void swap_values(int& v1, int& v2)
    {
        int temp;
        temp = v1;
        v1 = v2;
        v2 = temp;
    }
    ------------------------------------------------------------------------------*/


    also the off by one problem I was able to solve and we all missed it.
    when i initially called up menu one - enter an account , an account was entered even before the loop started and index was not initialiazed.

    thanks to everyone for there help, this is really helping me learn, especially when i see routes that others would take.

    jess

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but no one answered this particular problem
    I thought I gave you a link to a website that could help far better than a handful of posts.

    >also the off by one problem I was able to solve and we all missed it.
    Not all of us.

    >an account was entered even before the loop started and index was not initialiazed.
    index was initialized, you simply failed to update it properly. It's nice that you found it without me having to point it out in more detail though. That's a good sign.

    >i have a list of accounts, i need to place them in descending order, preferable while they are being entered.
    This isn't a question. Just posting code with a hint that it's not what you want is a pretty good way to get yourself ignored or flamed.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21