COSC 1315 - Fundamentals of Programming
Allan Kochis

  1. Report writing
    //
    // reports
    //
    #include <iostream>
    #include <iomanip>
     
    using namespace std;
     
    int main()
    {
        float balance, deposit, rate, interest;
        int year;
       
        balance=0;
        deposit=1000;
        rate=.0399;
        year=1;
        cout << "First Try" << endl;
        cout << "Year\tDeposit\tInterest\tBalance" <<endl;
        do {
            interest=balance * rate;
            balance=balance+interest+deposit;
            cout << year << "\t" << deposit << "\t" << interest << "\t\t"
            << balance << endl;
            year++;
        } while (year <4);
    /*
    First Try
    Year    Deposit Interest        Balance
    1       1000    0               1000
    2       1000    39.9            2039.9
    3       1000    81.392          3121.29
    */
        balance=0;
        deposit=1000;
        rate=.0399;
        year=1;
        cout << "Second Try" << endl;
        cout << "Year\tDeposit\tInterest\tBalance" <<endl;
        cout.precision(2);
        cout.setf(ios::showpoint | ios::fixed);
        do {
            interest=balance * rate;
            balance=balance+interest+deposit;
            cout << year << "\t" << deposit << "\t" << interest << "\t\t"
            << balance << endl;
            year++;
        } while (year <4);
    /*
    Second Try
    Year    Deposit Interest        Balance
    1       1000.00 0.00            1000.00
    2       1000.00 39.90           2039.90
    3       1000.00 81.39           3121.29
    */   
        balance=0;
        deposit=1000;
        rate=.0399;
        year=1;
        cout << "Third Try" << endl;
        cout << "Year\tDeposit\tInterest\t  Balance" <<endl;
        cout.precision(2);
        cout.setf(ios::showpoint | ios::fixed);
        do {
            interest=balance * rate;
            balance=balance+interest+deposit;
            cout << setw(4) << year << "\t" << setw(7) << deposit << "\t"
            << setw(8) << interest << "\t"
            << setw(9)<< balance << endl;
            year++;
        } while (year <4);  
    /*
    Third Try
    Year    Deposit Interest          Balance
       1    1000.00     0.00          1000.00
       2    1000.00    39.90          2039.90
       3    1000.00    81.39          3121.29
    */
       
        system("pause");
        return(0);
    }
    

  2. Lock problem
    A safe has five locks 1,2,3,4,5, all of which must be unlocked to open the safe The keys to the locks are distributed to five different people in the following manner;
    Person Lock 1 Lock 2 Lock 3 Lock 4 Lock 5
    Mr. Alden yes   yes    
    Ms. Baker yes     yes  
    Ms. Cable   yes   yes  
    Ms. Deere     yes   yes
    Mr. Evans yes       yes
    Write a program that will determine who is at work and if they can open the safe

  3. IPO Chart
    Input Processing Output
    Request which employees are present.
    Processing items:
    Lock1
    Lock2
    Lock3
    Lock4
    Lock5
    Response
     
    
    Algorithm:
     
    Start
     
    Do
     
       If Mr. Alden present
         set Lock1 to true
         set Lock3 to true
       Endif
     
       If Ms. Baker present
         set Lock1 to true
         set Lock4 to true
       Endif
     
       If Ms. Cable present
         set Lock2 to true
         set Lock4 to true
       Endif
     
       If Ms. Deere present
         set Lock3 to true
         set Lock5 to true
       Endif
     
       If Mr. Evans present
         set Lock1 to true
         set Lock5 to true
       Endif
     
       If Lock1 AND Lock2 AND Lock3 AND Lock4 AND Lock5 is true
          Print "You can open the safe"
       Else
          Print "You can't open the safe"
       EndIf
     
       Prompt "Do over?"
       get Response
     
    Repeat while response = "Yes"
     
    Stop
    
    Whether or not we can open the safe.

  4. Code 1
    //
    // A safe has five locks 1,2,3,4,5,
    // all of which must be unlocked to open the safe
    // The keys to the locks are distributed
    //  to five different people in the following manager;
    //
    //  Mr. Alden has keys for locks 1 and 3
    //  Ms. Baker has keys for locks 1 and 4
    //  Ms. Cable has keys for locks 2 and 4
    //  Ms. Deere has keys for locks 3 and 5
    //  Mr. Evans /has keys for locks 1 and 5
    //
    // Write a program that will determine whop is at work
    // and if they can open the safe
    //
    #include <iostream>
    #include <string>
    using namespace std;
     
     
     
    int main()
    {
     bool bSafe;  // true on open
     bool bLock1; // true if key is available;
     bool bLock2; // true if key is available
     bool bLock3;
     bool bLock4;
     bool bLock5;
     char cResponse;
     
     do {
     
      bLock1=false;
      bLock2=false;
      bLock3=bLock4=bLock5=false;
     
      cout << "Is Mr. Alden at work? :";
      cin >> cResponse;
      if(cResponse == 'y' || cResponse == 'Y')
      {
       bLock1=true;
       bLock3=true;
      }
     
      cout << "Is Ms. Baker at work? :";
      cin >> cResponse;
      if(cResponse == 'y' || cResponse == 'Y')
      {
       bLock1=true;
       bLock4=true;
      }
     
      cout  << "Is Ms. Cable at work? :";
      cin >> cResponse;
      if(cResponse == 'y' || cResponse == 'Y')
      {
       bLock2=true;
       bLock4=true;
      }
      
     
      cout << "Is Ms. Deere at work? :";
      cin >> cResponse;
      if(cResponse == 'y' || cResponse == 'Y')
      {
       bLock3=true;
       bLock5=true;
      }
      
     
      cout << "Is Mr. Evans at work? :";
      cin >> cResponse;
      if(cResponse == 'y' || cResponse == 'Y')
      {
       bLock1=true;
       bLock5=true;
      }
      
     
      bSafe=bLock1 && bLock2 && bLock3 && bLock4 && bLock5;
     
      if(bSafe)
      {
       cout << "You can open the safe" << endl;
      } else {
       cout << "You can not open the safe" << endl;
      }
     
      cout << "Try again ?:";
      cin  >> cResponse;
     } while (cResponse == 'y' || cResponse == 'Y');
     
     system("pause");
     return(0);
    }
    

    Is Mr. Alden at work? :y
    Is Ms. Baker at work? :n
    Is Ms. Cable at work? :y
    Is Ms. Deere at work? :n
    Is Mr. Evans at work? :y
    You can open the safe
    Try again ?:y
    Is Mr. Alden at work? :n
    Is Ms. Baker at work? :y
    Is Ms. Cable at work? :y
    Is Ms. Deere at work? :y
    Is Mr. Evans at work? :n
    You can open the safe
    Try again ?:y
    Is Mr. Alden at work? :y
    Is Ms. Baker at work? :y
    Is Ms. Cable at work? :y
    Is Ms. Deere at work? :n
    Is Mr. Evans at work? :n
    You can not open the safe
    Try again ?:n
    

  5. Refined Code
    //
    // lock program
    //
    // A safe has five locks 1,2,3,4,5,
    // all of which must be unlocked to open the safe
    // The keys to the locks are distributed
    //  to five different people in the following manager;
    //
    //  Mr. Alden has keys for locks 1 and 3
    //  Ms. Baker has keys for locks 1 and 4
    //  Ms. Cable has keys for locks 2 and 4
    //  Ms. Deere has keys for locks 3 and 5
    //  Mr. Evans has keys for locks 1 and 5
     
    #include 
    using namespace std;
    using std::string;
     
     
    bool prompt(string);
    int main()
    {
        bool bSafe;  // true on open
     bool bLock1; // true if key is available;
     bool bLock2; // true if key is available
     bool bLock3;
     bool bLock4;
     bool bLock5;
     
        do {
            bLock1=bLock2=bLock3=bLock4=bLock5=false;
           
            if(prompt("Is Mr. Alden at work? ")) bLock1=bLock3=true;
            if(prompt("Is Ms. Baker at work? ")) bLock1=bLock4=true;
            if(prompt("Is Ms. Cable at work? ")) bLock2=bLock4=true;
            if(prompt("Is Ms. Deere at work? ")) bLock3=bLock5=true;
            if(prompt("Is Mr. Evans at work? ")) bLock1=bLock5=true;
           
            bSafe=bLock1 && bLock2 && bLock3 && bLock4 && bLock5;
           
            if(bSafe)
              cout << "You can open the safe" << endl;
            else
              cout << "You cannot open the safe" << endl;
             
        } while (prompt("try again? "));
       
        system("pause");
        return(0);
    }
     
    bool prompt(string strText)
    {
         char cResponse;
        
         cout << strText;
         cin  >> cResponse;
        
         return(cResponse=='y' || cResponse == 'Y');
    }
    

    Is Mr. Alden at work?y
    Is Ms. Baker at work?n
    Is Ms. Cable at work?y
    Is Ms. Deere at work?n
    Is Mr. Evans at work?y
    You can open the safe
    try again? n
    Press any key to continue . . .
    

  6. Another look at the logic
    //
    // lock program
    //
    // A safe has five locks 1,2,3,4,5,
    // all of which must be unlocked to open the safe
    // The keys to the locks are distributed
    //  to five different people in the following manager;
    //
    //  Mr. Alden has keys for locks 1 and 3
    //  Ms. Baker has keys for locks 1 and 4
    //  Ms. Cable has keys for locks 2 and 4
    //  Ms. Deere has keys for locks 3 and 5
    //  Mr. Evans has keys for locks 1 and 5
     
    #include <iostream>
    using namespace std;
    using std::string;
     
     
    bool prompt(string);
    int main()
    {
       
     
     bool alden,baker,cable,deere,evans;
     
     
        do {
            alden=baker=cable=deere=evans=false;
           
            alden=prompt("Is Mr. Alden at work? ");
            baker=prompt("Is Ms. Baker at work? ");
            cable=prompt("Is Ms. Cable at work? ");
            deere=prompt("Is Ms. Deere at work? ");
            evans=prompt("Is Mr. Evans at work? ");
           
           
           
           
            if((alden||baker||evans) &&
                 cable               &&
               (alden || deere)      &&
               (baker || cable)      &&
               (deere || evans) )
              cout << "You can open the safe" << endl;
            else
              cout << "You cannot open the safe" << endl;
             
        } while (prompt("try again? "));
       
        system("pause");
        return(0);
    }
     
    bool prompt(string strText)
    {
         char cResponse;
        
         cout << strText;
         cin  >> cResponse;
        
         return(cResponse=='y' || cResponse == 'Y');
    }