Thread: sorting an array from smallest to largest

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    thanks
    swgh

  2. #17
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >thanks
    >swgh

    Did it work?
    Double Helix STL

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >for( int i = 1; i < total ; i++ )
    That will skip the first record.

    >for( int i = 0; i <= total ; i++ )
    That will exceed the array's boundaries.

    >for( int i = 1; i <= total ; i++ )
    That will skip the first record with the added bonus of exceeding the array's boundaries.

    >but an off-by one error like prelude said is a hard bug to eliminate.
    Especially when you're looking in the wrong function.

    >so if my verbage is incorrect one slap on the wrist is nicely felt but flogging this dead horse is manure
    Uh, I only mentioned it once. Anyway, I took some creative liberty with your program. Perhaps the changes I made will help you finish it up:
    Code:
    /*
      Assignment 5 (Jessie Brown) 11.10.06
    
      -- Edited (Julienne Walker) 11.10.06
    */
    #include <cassert>  // For assert
    #include <ios>      // For streamsize
    #include <iostream>
    #include <limits>   // For numeric_limits
    
    using namespace std;
    
    void menu_selection();
    bool add_account ( int account[], double balance[], int index );
    void account_report ( int account[], double balance[], int total );
    bool deposit_funds ( int account[], double balance[], int total  );
    bool withdraw_funds ( int account[], double balance[], int total  );
    
    int main()
    {
      menu_selection();
    
      cout<<"Press <ENTER> to continue . . .";
      cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
      cin.get();
    }
    
    // Primary driver
    void menu_selection()
    {
      const int LIMIT = 10;
    
      int index = 0;
      int account[LIMIT];
      double balance[LIMIT];
    
      bool done = false;
    
      do {
        int option;
    
        cout<<"1. Add an account\n"
          <<"2. Remove an account\n"
          <<"3. Deposit Funds\n"
          <<"4. Withdraw Funds\n"
          <<"5. View account report\n"
          <<"6. Exit program\n"
          <<"Selection: ";
    
        // Localize error discovery to this point
        while ( !( cin>> option ) || ( option < 0 || 7 < option ) ) {
          cout<<"Invalid selection. Please try again: ";
          cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
          cin.clear();
        }
    
        switch ( option ) {
        case 1:
          // Take care not to update the index on failure
          if ( index < LIMIT ) {
            if ( !add_account ( account, balance, index ) )
              cout<<"Error adding account\n";
            else
              ++index;
          }
          else
            cout<<"Limit reached; No more accounts can be added\n";
          break;
        case 2:
          // Not yet implemented
          break;
        case 3:
          if ( !deposit_funds ( account, balance, index ) )
            cout<<"Error with deposit\n";
          break;
        case 4:
          if ( !withdraw_funds ( account, balance, index ) )
            cout<<"Error with withdrawl\n";
          break;
        case 5:
          account_report ( account, balance, index );
          break;
        case 6:
          done = true;
          break;
        default:
          assert ( 0 ); // Impossible case
        }
      } while ( !done );
    }
    
    bool add_account ( int account[], double balance[], int index )
    {
      int acct;
      double bal;
    
      for ( ; ; ) {
        cout<<"Please enter a customer account and balance: ";
    
        if ( !( cin>> acct >> bal ) )
          return false;
        else if ( bal < 10 )
          cout<<"Balance must be at least $10.00\n";
        else
          break;
      }
    
      account[index] = acct;
      balance[index] = bal;
    
      return true;
    }
    
    void account_report ( int account[], double balance[], int total )
    {
      double sum = 0;
    
      cout<<"\nAccount\tBalance\n";
      cout.setf ( ios_base::fixed );
      cout.precision ( 2 );
    
      for( int i = 0; i < total; i++ ) {
        cout<< account[i] <<'\t'<< balance[i] <<'\n';
        sum += balance[i];
      }
    
      cout<<"Total:\t"<< sum <<"\n\n";
    }
    
    bool deposit_funds ( int account[], double balance[], int total )
    {
      int acct;
      double credit;
    
      cout<<"Please enter a customer account and credit amount: ";
    
      if ( !( cin>> acct >> credit ) )
        return false;
    
      // Naive search
      for ( int i = 0; i < total; i++ ) {
        if ( account[i] == acct ) {
          balance[i] += credit;
          return true;
        }
      }
    
      // Account not found
      return false;
    }
    
    bool withdraw_funds ( int account[], double balance[], int total  )
    {
      int acct;
      double debit;
    
      cout<<"Please enter a customer account and debit amount: ";
    
      if ( !( cin>> acct >> debit ) )
        return false;
    
      // Naive search
      for ( int i = 0; i < total; i++ ) {
        if ( account[i] == acct ) {
          // Take care not to withdraw if there's not enough
          if ( balance[i] < debit ) {
            cout<<"Insufficient funds\n";
            return false;
          }
          else {
            balance[i] -= debit;
            return true;
          }
        }
      }
    
      // Account not found
      return false;
    }
    My best code is written with the delete key.

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