C++ Sample Code


  HOME  Arrays   Bubble Sort   Do/While Loop   Float Format   For Loop   If Statement   Pass by Reference  Pass by Value  Switch Statement   While Loop   BACK

Set Float Format
#include <iomanip.h>

void SetFloatFormat ( )
{
   cout.setf (ios::showpoint);  //always show decimal point
   cout.setf (ios::fixed);          //don't show scientific notation
   cout.precison (2);             //set # of places to right of decimal
}

IF Statement
stmt could be an assignment statement, a function call, etc.

if (exp)
   stmt;
else
   stmt;

if (exp)
   {
      multiple statements;
   }
else
   {
      multiple statements;
   }

if (exp)
   stmt;

Example
if (nX > nY)
   cout << "nX is greater than nY";
else
   cout << "nX is not greater than nY";

FOR Loop
Repetition logic for know values. Pre-test.

for ( initialization statement, exit-test expression, increment)
   statement;

for ( _ , _ , _ )
   {
      stmt;
      stmt;
   }

Example
int nExampleIndex;
for ( nExampleIndex = 0, nExampleIndex < 5, nExampleIndex ++)
   cout << nExampleIndex ;

WHILE Loop
Repeition logic for unknown values. Pre-test.

while ( fBankBalance > 0.0 )
   GoShopping ( );  // This has to adjust fBankBalance or you'll never get out of the loop

DO / WHILE Loop
Repition logic for unknown values. Post-test.

Example 1
do
   {
      while ( fBankBalance > 0.0 )
         GoShopping ( );
      GoToATM ( fBankBalance, bCreditOk );
   }
while ( bCreditOk );

Example 2
do
   {
      cout << "Enter your age- ";
      cin >> nAge;
   }
while ( nAge < 0 || nAge > 99 )

SWITCH Statement
Used when you keep testing the same value. If it can be done with a switch do it - you're more likely to make a mistake if you use multiple if statements! C++ defaults to fall-through-logic meaning that if you don't eand a case with a break statement then the program will execute every line for every case below the one it tested true for.

switch ( exp )
{
   case const : stmt ;
                     stmt ;
                     stmt ;
                     break ;
   case const :
   case const : stmt ;
   case const : stmt ; break ;
   default : stmt ;
                stmt;
}

ARRAYS
Not updated yet!

PASS BY VALUE
The value of an actual parameter expression is passed to a corresponding formal parameter local parameter memory location. Basically, this copies the data to local memory for the function.
    Advantages: original data is protected!
    Disadvantages: you have to allocate local memory and copy data into it.

int nA = 1 ;
   x ( nA, nA + 1 ) ;
void x ( int  nR ; int  nS )
   cout << nR ;

PASS BY REFERENCE / ADDRESS
The address of an actual parameter variable memory location is passed to a corresponding formal parameter. Think of this as a hard link in unix. You change one, you change both.
    Advantages: no local memory and you don't have to copy data - faster!
    Disadvantages: original data is vulnerable

int nA = 1 ;
   x ( nA, nA + 1 ) ;
void x ( int & nR ; int & nS )
   cout << nR ;

BUBBLE SORT
Should be used for no more than 30 values because it is a really inefficient way to sort values.

void main ( )
{
const int nSize = 4;
int naValue [nSize + 1] = { 0, 10, 5, 7 , 3 };

for (nPassIndex = 1 ; nPassIndex < nSize ; nPassIndex ++ )
   for (nTestIndex = 1 ; nTestIndex < nSize ;  nTestIndex ++ )
      if (naValue [ nTestIndex ] > naValue [nTestIndex + 1]
         swap (naValue, nTestIndex, nTestIndex + 1) ;
}
void swap ( int naValue [ ], int nSwapIndexA, int nSwapIndexB )
{
     nHold = naValue [nTestIndex];
     naValue [nTestIndex] = naValue [nTestIndex + 1];
     naValue [nTestIndex + 1] = nHold;
}


  Arrays   Bubble Sort   Do/While Loop   Float Format   For Loop   If Statement   Pass by Reference  Pass by Value  Switch Statement   While Loop
HOME   BACK