Thread: sorting an array from smallest to largest

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

    sorting an array from smallest to largest

    I have already turned in this assignment but there are several things that I could not get to work.

    The specific issues I can not wrap my brain around are:

    1. in the menu choice number 1:
    a. I allows you to put in 11 choiced and the negative number instead of 10 choices only with one of them being the negative number

    b. I need to be able to sort the array each time i put in a new account number in descending order, I know how to keep both arrays together so that data matches but can not figure out how to code the actual sort of accounts.

    There are I think for specific problems that I am having and I do not like to muddy stuff so I am posting each problem seperately.

    any input would be greatly appriecated, this is a first year c++ program

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

    forgot to include the code!!!

    I for got to include the code for this, what i have done so far

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Project was created in Boreland IDE.
    My co-workers and I got a chuckle out of this.

    >void main()
    int main(). No excuses.

    >fflush(stdin);
    That's wrong in so many ways. Not the least of which is you forgot to include <cstdio> and fflush is undefined for input streams.

    It also looks like you're missing braces in several parts of your program.

    >a. I allows you to put in 11 choiced and the negative number instead of 10 choices only with
    >one of them being the negative number
    Walk through your logic for adding values and updating the index. You've got an off-by-one error.

    >I know how to keep both arrays together so that data matches but can not figure out how to
    >code the actual sort of accounts.
    You can find more than you ever wanted to know about sorting here.
    My best code is written with the delete key.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
              cout << endl
                   << "1. Add a distinct account number: \n \n"
                   << "2. Remove an existing account number: \n \n"
                   << "3. Deposit Funds into distinct account: \n \n"
                   << "4. Withdraw Funds from distinct account: \n \n"
                   << "5. View all Customer accounts and Balance: \n \n"
                   << "6. Total Funds available from All accounts: \n \n"
                   << "7. Exit program: \n \n";
    There's no need to print a space between newlines. "\n \n" could become "\n\n".
    Code:
         if (result>-1)
    
            account[result] = account[result+1];
            balance[result] = balance [result+1];
            
            total--;
     }
    I think you may be missing some curly braces here.

    You seem to have a lot of arguments called int NUMBER that aren't used. If an argument isn't used, in C++ you can leave out its name:
    Code:
    int main(int, char **) {}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    yes i know i am missing brackets, i stoped as it needs to loop,
    yes i am using borland (this program was provided my the college) and have not waisted my time taking out its little msg.
    I recieved NO errors on my end running this program it simply is not doing what i want it to
    thanks for the link on sort,
    int NUMBERS is the constant that is declaring the size of the arrays used, not getting an error but again I am sure it is being passed in places not needed. just not getting that error.

    If some of you are running this little file and getting errors could you please tell me what program you are using as this might help me more by purchasing and using the program that your using.

    glad you and your co workers got a laugh; wish i could have shared it!!

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Well, since main returns an int, use a compiler that will flag void main as an error. Free ones that are standard compliant are:

    DevC++ - free from bloodshed website - get version 4.992 - No more patches are made for this

    Microsoft Visual C++ 2005 Express edition - free download from Microsofts website - very
    compliant, gets updates, but you need to download the platform SDK to get all the windows functions.
    Double Helix STL

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    thanks,
    will look into that!

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    i know that i am off by one, although it does not give me an error, it just does its own thing.

    what i can not get past it how to fix the off by one and make it work in the menu choice number 5 also

    thanks
    jess

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I could be wrong, but an off by one error in the function prelude stated is on this line:

    Code:
     while ((next >= 0) && (index < NUMBER))
    One possibility is to change

    Code:
    index < NUMBER
    To this

    Code:
    index <= NUMBER
    Double Helix STL

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    tried that but will try again
    thanks

    i was up for over 24 hours, not all working on this program but the last 14 of it was!!!

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >yes i know i am missing brackets, i stoped as it needs to loop
    Eh? What does that have to do with broken logic due to missing braces?

    >I recieved NO errors on my end running this program it simply is not doing what i want it to
    Incorrect behavior is an error.

    >If some of you are running this little file and getting errors
    I didn't run it, I simply glanced over it.

    >glad you and your co workers got a laugh; wish i could have shared it!!
    The joke we got out of it was Boreland instead of Borland. It's an amusing typo.

    >i know that i am off by one, although it does not give me an error
    Stop this train of thought immediately. A lack of errors doesn't mean your program is bug free. It just means that your bugs haven't manifested as errors yet or they're quietly destroying something. Bug free code is a lot harder to write than just getting a clean compile and not crashing at runtime.
    My best code is written with the delete key.

  12. #12
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    what is the function called in your program that controls action 5 in your list? I am looking at your program at the same time - trying to locate the off by one error
    Double Helix STL

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    swgh

    print_all_accounts(account,balance,total); prints the code
    enter_accounts (account,balance,NUMBER,total); inputs the numbers

    thanks for looking

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Like before:

    Try changing this:

    Code:
    for( int i = 0; i < total ; i++ )
    // print_all_accounts function

    to either this:

    Code:
    for( int i = 1; i < total ; i++ )
    This:

    Code:
    for( int i = 1; i <= total ; i++ )
    Or this:

    Code:
    for( int i = 0; i <= total ; i++ )
    The changes are very slight, but an off-by one error like prelude said is a hard bug to
    eliminate.
    Double Helix STL

  15. #15
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    prelude

    missing brackets, stopped because after being up all night brain said enough, that "error" has been fixed (as i said in original post some of it has been fixed)

    i understand and maybe my verbage on this board needs polishing (i KNOW that if it does not run like intended the program is not complete and if it hangs up during run there is a bug, I KNOW that the compiler does not "catch" logical issues, and)

    again, wish i could have shared the laugh, especially today

    when i say does not "give" me an error i meant that the compiler is not hanging up - the fact that it does not "produce" the outcome desired (clean running entity) is why i am here trying to pick you brain

    so if my verbage is incorrect one slap on the wrist is nicely felt but flogging this dead horse is manure

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-14-2009, 01:39 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. sorting a randomly generated dynamic array
    By Led Zeppelin in forum C Programming
    Replies: 4
    Last Post: 05-03-2002, 09:10 AM