Difference: CodeMultiArrays (1 vs. 2)

Revision 22017-10-25 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Multidimensional Arrays and string

Changed:
<
<

TickTackToe

>
>

BankAccount.h

  %CODE{"c++"}%
Added:
>
>
//DISPLAY 10.6 /Program to demonstrate the class BankAccount. //Demonstrates an alternative implementation of the class BankAccount.
 #include using namespace std;
Changed:
<
<
void DisplayBoard(char board[][3]) { cout << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << " " << board[i][j]; if (j < 2) { cout << " |"; } } cout << endl; if (i < 2) { cout << "-----------" << endl; } } cout << endl; }
>
>
//Class for a bank account:
 
Changed:
<
<
void nextMove(char player, char board[][3]) { int row, col; bool ok = true; do { ok = true; cout << "Player " << player << " What row (1,2,3) and column (1,2,3)? "; cin >> row >> col; if (row < 1 || row > 3) { cout << "Bad row, must be between 1 and 3." << endl; ok = false; } if (col < 1 || col > 3) { cout << "Bad column, must be between 1 and 3." << endl; ok = false; } if (ok && board[row - 1][col - 1] = ' ') { cout << "Sorry, there is already an " << board[row - 1][col - 1] << " in that position!" << endl; ok = false; } } while (ok); board[row - 1][col - 1] = player;
>
>
class BankAccount { public: BankAccount(int dollars, int cents, double rate); //Initializes the account balance to $dollars.cents and //initializes the interest rate to rate percent.
 
Changed:
<
<
}
>
>
BankAccount(int dollars, double rate); //Initializes the account balance to $dollars.00 and //initializes the interest rate to rate percent.

BankAccount(); //Initializes the account balance to $0.00 and the interest rate to 0.0%.

void update(); //Postcondition: One year of simple interest has been added to the account //balance.

double get_balance(); //Returns the current account balance.

double get_rate(); //Returns the current account interest rate as a percentage.

void output(ostream& outs); //Precondition: If outs is a file output stream, then //outs has already been connected to a file. //Postcondition: Account balance and interest rate //have been written to the stream outs.

 
Changed:
<
<
bool match3(char a, char b, char c) { if (a = b && b = c) { return true;
>
>
friend bool operator>(BankAccount a, BankAccount b); friend BankAccount operator+(BankAccount a, BankAccount b); friend BankAccount operator+(BankAccount a, int b);

private: double balance; double interest_rate; double fraction(double percent); //Converts a percentage to a fraction. For example, fraction(50.3) //returns 0.503.

}; %ENDCODE%

BankAccount.cpp

%CODE{"c++"}% #include "BankAccount.h"

BankAccount::BankAccount(int dollars, int cents, double rate) { if ((dollars < 0) || (cents < 0) || (rate < 0)) { cout << "Illegal values for money or interest rate.\n"; balance = 0; interest_rate = 0;

  } else {
Changed:
<
<
return false;
>
>
balance = dollars + 0.01 * cents; interest_rate = rate;
  } }
Changed:
<
<
bool checkDone(char board[][3], char& winner) { // Checks rows for (int i = 0; i < 3; i++) { if (board[i][0] = ' ') { if (match3(board[i][0],board[i][1],board[i][2])) { winner = board[i][0]; return true; } } } // Checks columns for (int i = 0; i < 3; i++) { if (board[0][i] = ' ') { if (match3(board[0][i],board[1][i],board[2][i])) { winner = board[0][i]; return true; }
>
>
BankAccount::BankAccount(int dollars, double rate) { if ((dollars < 0) || (rate < 0)) { cout << "Illegal values for money or interest rate.\n";
  }
Added:
>
>
balance = dollars; interest_rate = rate;
  }
Changed:
<
<
// Check diagonals if (board[0][0] = ' ') { if (match3(board[0][0],board[1][1],board[2][2])) { winner = board[0][0]; return true; } } if (board[2][0] = ' ') { if (match3(board[2][0],board[1][1],board[0][2])) { winner = board[2][0]; return true; } } return false;
>
>
BankAccount::BankAccount() : balance(0), interest_rate(0.0) { //Body intentionally empty.
 }
Changed:
<
<
/* * */ int main() { char myBoard[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; char winner; do { DisplayBoard(myBoard); nextMove('X', myBoard); if (checkDone(myBoard, winner)) { break; } DisplayBoard(myBoard); nextMove('O', myBoard); if (checkDone(myBoard, winner)) { break; } } while (1); DisplayBoard(myBoard); cout << "You win " << winner << "!!!!" << endl;
>
>
//Uses iostream:
 
Changed:
<
<
return 0;
>
>
void BankAccount::output(ostream& outs) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(2); outs << "Account balance $" << balance << endl; outs << "Interest rate " << interest_rate << "%" << endl;
 }
Added:
>
>
void BankAccount::update() { balance += balance * fraction(interest_rate); }
 
Changed:
<
<
%ENDCODE%

Hello world

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->
>
>
double BankAccount::fraction(double percent) { return percent / 100; }
 
Changed:
<
<

Hello world

%CODE{"c++"}%
>
>
double BankAccount::get_balance() { return balance; }
 
Changed:
<
<
%ENDCODE%
>
>
bool operator>(BankAccount a,BankAccount b) { return a.balance>b.balance; }
 
Changed:
<
<

Hello world

%CODE{"c++"}%
>
>
BankAccount operator+(BankAccount a,BankAccount b) { BankAccount newAccount; newAccount.balance=a.balance+b.balance; newAccount.interest_rate=max(a.interest_rate,b.interest_rate); return newAccount; }
 
Added:
>
>
BankAccount operator+(BankAccount a,int b) { BankAccount newAccount; newAccount.balance=a.balance+b; newAccount.interest_rate=a.interest_rate; return newAccount; }
 %ENDCODE%
Changed:
<
<

Hello world

>
>

main.cpp

  %CODE{"c++"}%
Added:
>
>
#include "BankAccount.h"
 
Changed:
<
<
%ENDCODE%
>
>
int main() { BankAccount account1(100, 8.3), account2(3000, 33, 6.2);
 
Changed:
<
<

Hello world

%CODE{"c++"}%
>
>
cout << "account1 initialized as follows:\n"; account1.output(cout); cout << "account2 initialized as follows:\n"; account2.output(cout);

account1.update(); account1.update(); account1.update(); account1.update();

account2.update();

cout << "account1 is:\n"; account1.output(cout); cout << "account2 is:\n"; account2.output(cout);

 
Added:
>
>
if (account2 > account1) { cout << "2 is has more money"; } else { cout << "1 is has more money"; } BankAccount a3 = account1 + account2; cout << "New account is:\n"; a3.output(cout);

BankAccount a4 = account1 + 1500; cout << "New account is:\n"; a4.output(cout); return 0; }

 %ENDCODE%

Revision 12016-10-23 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Multidimensional Arrays and string

TickTackToe

<-- SyntaxHighlightingPlugin -->
#include <iostream>
using namespace std;

void DisplayBoard(char board[][3]) {
    cout << endl;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << " " << board[i][j];
            if (j < 2) {
                cout << " |";
            }
        }
        cout << endl;
        if (i < 2) {
            cout << "-----------" << endl;
        }
    }
    cout << endl;
}

void nextMove(char player, char board[][3]) {
    int row, col;
    bool ok = true;
    do {
        ok = true;
        cout << "Player " << player << " What row (1,2,3) and column (1,2,3)? ";
        cin >> row >> col;
        if (row < 1 || row > 3) {
            cout << "Bad row, must be between 1 and 3." << endl;
            ok = false;
        }
        if (col < 1 || col > 3) {
            cout << "Bad column, must be between 1 and 3." << endl;
            ok = false;
        }
        if (ok && board[row - 1][col - 1] != ' ') {
            cout << "Sorry, there is already an " << board[row - 1][col - 1] << " in that position!" << endl;
            ok = false;
        }
    } while (!ok);
    board[row - 1][col - 1] = player;

}

bool match3(char a, char b, char c) {
    if (a == b && b == c) {
        return true;
    } else {
        return false;
    }
}

bool checkDone(char board[][3], char& winner) {
    // Checks rows
    for (int i = 0; i < 3; i++) {
        if (board[i][0] != ' ') {
            if (match3(board[i][0],board[i][1],board[i][2])) {
                winner = board[i][0];
                return true;
            }
        }
    }
    // Checks columns
    for (int i = 0; i < 3; i++) {
        if (board[0][i] != ' ') {
            if (match3(board[0][i],board[1][i],board[2][i])) {
                winner = board[0][i];
                return true;
            }
        }
    }

    // Check diagonals
    if (board[0][0] != ' ') {
        if (match3(board[0][0],board[1][1],board[2][2])) {
            winner = board[0][0];
            return true;
        }
    }
    if (board[2][0] != ' ') {
        if (match3(board[2][0],board[1][1],board[0][2])) {
            winner = board[2][0];
            return true;
        }
    }
    return false;
}

/*
 * 
 */
int main() {
    char myBoard[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    char winner;
    do {
        DisplayBoard(myBoard);
        nextMove('X', myBoard);
        if (checkDone(myBoard, winner)) {
            break;
        }
        DisplayBoard(myBoard);
        nextMove('O', myBoard);
        if (checkDone(myBoard, winner)) {
            break;
        }
    } while (1);
    DisplayBoard(myBoard);
    cout << "You win " << winner << "!!!!" << endl;
    
    return 0;
}

<-- end SyntaxHighlightingPlugin -->

Hello world

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Hello world

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Hello world

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Hello world

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

Hello world

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback