Tuesday, March 20, 2012

Fix: Code::Blocks won't Build and Run

I currently using Dev-C++ IDE but since Dev-C++ project seems to be abandoned and the compiler sometimes makes me crazy, I've decided to turn back to Code::Blocks which I've been using in my programming class back then during my Engineering's Foundation courses.
I've installed Code::Blocks using the installer from my harddrive and what happen is the program won't build and run.When compiling an empty project, the build log shows nothing but the error
"firstTry - Debug" uses an invalid compiler. Skipping..
shows up when compiling a project.So I've start Googling for the solution and I've found this thread
http://forums.codeblocks.org/index.php?topic=14814.0;prev_next=next
Same issues and the problem is the default setting's wrongly locating the MinGW compiler in my directory. Its should be C:/Program Files/CodeBlocks/MinGw rather than C:/MinGW .But then I realise that the installer package I'm using for Code::Blocks is not including the MinGW setup.Damn!!
So what about using the MinGW from my existing Dev-C++??Yes!What a brilliant I am..LOL
So following the instruction from the link above, I should go to Settings>Compiler and debugger>toolchain executables tab and relocate the directory for the MinGW folder and voila!!Its works..!!damn noob I am..zzzz..
By the way just click the Auto Detect button if your Code::Blocks already has MinGW.

Friday, March 16, 2012

1) A Computer Simulator: Simpletron

Simpletron computer simulator using Simpletron Machine Language(SML)
/*
 *   Title: Pointers 728 : A Computer Simulator
 *   Author: aeriqusyairi
 *   Date: Mac11 2012
 */
 
#include<stdio.h>

#define LIMIT 100
#define SENTINEL -99999

//operationcode
#define READ 10
#define WRITE 11
#define LOAD 20
#define STORE 21
#define ADD 30
#define SUBTRACT 31
#define DIVIDE 32
#define MULTIPLY 33
#define BRANCH 40
#define BRANCHNEG 41
#define BRANCHZERO 42
#define HALT 43

#define TERMINATED 50

void intro( void );//display intro interface
void instruct( int memory[] );//get the instruction from user
void sml( int memory[] );//the processor

int main( void ){
    
   int memory[ LIMIT ] = { 0 };

   intro();
   instruct( memory );
   sml( memory );
   
   system("pause");
   return 0;
}

void intro( void ){
   printf("                         *** Welcome to Simpletron! ***\n"
          "***********************************************************************************\n"
          "*** Please enter your program one instruction *** INSTRUCTION                   ***\n"
          "*** (or data word) at a time.                 ***   READ       10    HALT 43    ***\n"
          "***                                           ***   WRITE      11               ***\n"
          "*** I will type the location number and a     ***   LOAD       20               ***\n"
          "*** question mark (?).                        ***   STORE      21               ***\n"
          "***                                           ***   ADD        30               ***\n"
          "*** You then type the word for that location. ***   SUBSTRACT  31               ***\n"
          "***                                           ***   DIVIDE     32               ***\n"
          "*** Type the sentinel -99999 to stop entering ***   MULTIPLY   33               ***\n"
          "*** your program.                             ***   BRANCH     40               ***\n"
          "***                                           ***   BRANCHNEG  41               ***\n"
          "*** @aeriqusyairi                             ***   BRANCHZERO 42               ***\n"
          "***********************************************************************************\n\n");   
}

void instruct( int memory[] ){
   int i = 0, j = 0;
   
   while(j != SENTINEL && i <= LIMIT){
           
      if(i == LIMIT){
         printf("Memory Full!\n");
         break;     
      }
      
     do{
      
         if(i < 10)
            printf("-> 0%d ? ", i );
         else
            printf("-> %d ? ", i );
      
         scanf("%d", &j ); 
         
         if((j < -9999 || j > 9999) && j != SENTINEL)
            printf("\a-> ERROR: INVALID INPUT!\n");
         
      }while((j < -9999 || j > 9999) && j != SENTINEL);
      
      if(j != SENTINEL){
         memory[ i ] = j;     
      }
      
      i++;       
   }
   
   printf("\n*** Program loading completed ***\n"
          "*** Program execution begins. ***\n\n");
}

void sml( int memory[] ){
   void dump( int memory[], int accumulator, int instructionCounter, int instructionRegister,
              int operationCode, int operand );//dump the server data
   
   int accumulator = 0;
   int instructionCounter = 0;
   int instructionRegister = 0;
   int operationCode = 0;
   int operand = 0; 
   
   while(instructionCounter < LIMIT){
      
      //fetch the data
      instructionRegister = memory[ instructionCounter ];
      operationCode = instructionRegister / 100;
      operand = instructionRegister % 100;
      //printf("%d\n%d\n%d\n", instructionRegister, operationCode, operand );
      
      switch(operationCode){
         
         case READ: printf("READ: ");
                    scanf("%d", &memory[ operand ] );
            break;
         
         case WRITE: printf("WRITE: ");
                     printf("%d", memory[ operand ] );
            break;
         
         case LOAD: printf("LOAD: %d\n", memory[ operand ] );
                    accumulator = memory[ operand ];
            break;
         
         case STORE: printf("STORE: %d\n", accumulator);
                     memory[ operand ] = accumulator;
            break;
         
         case ADD: printf("ADD: %d\n", memory[ operand ] );
                   accumulator += memory[ operand ]; 
            break;
         
         case SUBTRACT: printf("SUBTRACT: %d\n", memory[ operand ] );
                        accumulator -= memory[ operand ];
            break;
         
         case DIVIDE: printf("DIVIDE: %d\n", memory[ operand ] );
                      if(memory[ operand ] == 0){
                         printf("\a*** FATAL ERROR: Attempt to divide by zero ***\n");
                         operationCode = TERMINATED;
                      }else{
                         accumulator /= memory[ operand ];
                      }
            break;
         
         case MULTIPLY: printf("MULTIPLY: %d\n", memory[ operand ] );
                        accumulator *= memory[ operand ];
            break;
         
         case BRANCH: printf("BRANCH TO %d\n", operand - 1);
                      instructionCounter = operand - 1;
            break;
         
         case BRANCHNEG: printf("BRANCHNEG TO %d\n", operand - 1);
                         if(accumulator < 0)
                            instructionCounter = operand - 1;
            break;
            
         case BRANCHZERO: printf("BRANCHZERO TO %d\n", operand - 1);
                          if(accumulator == 0)
                             instructionCounter = operand - 1;
            break; 
            
         default: if(operationCode != HALT){
                     printf("\a*** FATAL ERROR: Attempt to execute invalid operation ***\n");
                     operationCode = TERMINATED;
                  }                   
         
      }
      
      if(accumulator > 9999 || accumulator < -9999){
         printf("\a*** FATAL ERROR: accumulator overflows ***\n");
         operationCode = TERMINATED;              
      }
      
      if(operationCode == HALT){
         printf("\n\n*** Simpletron execution terminated ***\n");
         break;
      }else if(operationCode == TERMINATED){
         printf("*** Simpletron execution abnormally terminated ***\n");
         dump( memory, accumulator, instructionCounter, instructionRegister, operationCode, operand );
         break;   
      }
      
      instructionCounter++;
      
   }    
}

void dump( int memory[], int accumulator, int instructionCounter, int instructionRegister,
           int operationCode, int operand ){
   
   int i, j;
   
   printf("\nREGISTERS:\n"
          "accumulator            +%d\n"
          "instructionCounter      %d\n"
          "instructionRegister    +%d\n"
          "operationCode           %d\n"
          "operand                 %d\n\n"
          "MEMORY:\n  ", 
          accumulator, instructionCounter, instructionRegister, operationCode, operand);
   for(i = 0; i < 10; i++){
      printf("%7d", i );
   }
   printf("\n");
   for(i = 0; i < LIMIT; i += 10){
      
      if(i == 0)
         printf("%d ", i);
      else
         printf("%d", i);
         
      for(j = i; j < i + 10; j++ ){
         if(memory[ j ] == 0)
            printf("  +0000");
         else
            printf("  %+d", memory[ j ]);
      }
      printf("\n");
   }   
}

Arrays of Pointers to Functions

Program below uses pointers to functions to simulate a menu-driven system.
Note that one restriction on using arrays of pointers to functions is that all the pointers must have the same type.The pointers must be to functions of the same return type that receive arguments of the same type.
/*
 *   Title: Pointers 725 : Array of Pointer to Function
 *   Author: aeriqusyairi
 *   Date: Mac10 2012
 */
#include
#define STUDENTS 3
#define EXAMS 4

void minimum( const int grades[][ EXAMS ], int pupils, int tests );
void maximum( const int grades[][ EXAMS ], int pupils, int tests );
void average( const int grades[][ EXAMS ], int pupils, int tests );
void printArray( const int grades[][ EXAMS ], int pupils, int tests );

int main( void ){
   int student, choice = 0;
   
   const int studentGrades[ STUDENTS ][ EXAMS ] = 
      {{ 77, 68, 86, 73 },
       { 96, 87, 89, 78 },
       { 70, 90, 86, 81 }};
       
   void (*processGrades[ 4 ])( const int, int, int ) = { printArray, minimum, maximum, average };
       
   printf("Enter a choice:\n"
          " 0 Print the array of grades\n"
          " 1 Find the minimum grade\n"
          " 2 Find the maximum grade\n"
          " 3 Print the average on all tests for each student\n"
          " 4 End program\n\n"
          "choice: ");
          
   scanf("%d", &choice );
   printf("\n");
   
   while(choice >= 0 && choice < 4){
      (*processGrades[ choice ])( studentGrades, STUDENTS, EXAMS );
      
      printf("\n\n");
      system("pause");
      system("cls");
      printf("Enter a choice:\n"
          " 0 Print the array of grades\n"
          " 1 Find the minimum grade\n"
          " 2 Find the maximum grade\n"
          " 3 Print the average on all tests for each student\n"
          " 4 End program\n\n"
          "choice: ");
          
      scanf("%d", &choice );
      printf("\n");
   }
   
   printf("Program execution completed\n");
   system("pause");
   return 0;
}

void minimum( const int grades[][ EXAMS ], int pupils, int tests ){
  int i, j;
  int lowGrade = 100;
  
  for(i = 0; i < pupils; i++){
     for(j = 0; j  highGrade){
            highGrade = grades[ i ][ j ];           
         }
      }      
   }
   
   printf("Highest grade: %d", highGrade);
}

void average( const int grades[][ EXAMS ], int pupils, int tests ){
   int i, j;
   int total;
   
   for(i = 0; i < pupils; i++){
      
      total = 0;
      
      for(j = 0; j < tests; j++){
          total += grades[ i ][ j ];
      }
      printf("The average grade for student %d is %.2f\n", i, (double) total / tests );
   }
}

void printArray( const int grades[][ EXAMS ], int pupils, int tests ){
   int i, j;
   
   printf("The array is:\n");
   printf("                 [0]  [1]  [2]  [3]");
   
   for(i = 0; i < pupils; i++){
      printf("\nstudentGrades[%d] ", i );
      
      for(j = 0; j < tests; j++){
         printf("%-5d", grades[ i ][ j ] );
      }
   }
}

2) Maze: Generating Maze Randomly and Maze Traversal

My version of random maze generator and maze traversal.
This program used Depth First Search Algorithm to generate random maze according to the size input by user, then this program used the Right Hand Wall algorithm for the traversal.
Source:
Depth First Search Algorithm - http://www.mazeworks.com/mazegen/mazetut/index.htm
The recursive version of the maze generator can be found here - http://azerdark.wordpress.com/2009/03/29/588/
/*
 *   Title: Pointers 723 : Generating Maze Randomly & Maze Traverse
 *   Author: aeriqusyairi
 *   Date: Mac5 2012
 *   Algorithm: Depth First Search
 *   
 *      create a CellStack (LIFO) to hold a list of cell locations
 *      set TotalCells = number of cells in grid
 *      choose a cell at random and call it CurrentCell
 *      set VisitedCells = 1 
 *
 *      while VisitedCells < TotalCells 
 *
 *         find all neighbors of CurrentCell with all walls intact
 *         if one or more found
 *            choose one at random
 *            knock down the wall between it and CurrentCell
 *            push CurrentCell location on the CellStack
 *            make the new cell CurrentCell
 *            add 1 to VisitedCells 
 *         else
 *            pop the most recent cell entry off the CellStack
 *            make it CurrentCell 
 *         endIf 
 *
 *      endWhile
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define MAX 101 //maxRow + maxColumn + 1
#define WALL 1 //indicate the wall is up
#define PATH 0 //indicate the cells is previous path
#define CELLS 2500 //row * column
#define START 3 //start position
#define END 4 //end position
 
void mazeGenerator( int wMaze[ MAX ][ MAX ], int wSize ); //generate the maze
void startEnd( int wMaze[ MAX ][ MAX ], int wSize, int* wSRow, int* wSCol );//generate start and end position
int mazeTraverse( int maze[ MAX ][ MAX ], int currentRow, int currentColumn, int size );//Automatic maze solver
void printMaze( int wMaze[ MAX ][ MAX ], int wSize );//display the maze
 
int main(void){
   
   int size = 3, sRow = 0, sCol = 0, state;
   int maze[ MAX ][ MAX ]; //maze array 
   
   printf("My Maze Generator\n*****************\n\n");
   printf("Input the size of your desired maze (3 to 50): ");
   do{
      if(size < 3 || size > 50)
         printf("Invalid size! Input again: ");
            
      scanf("%d", &size );//get the desired size from user
   }while(size < 3 || size > 50);
   
   mazeGenerator( maze, size );
   startEnd( maze, size, &sRow, &sCol );
   printMaze( maze, size );
   state = mazeTraverse( maze, sRow, sCol, size );
   
   if(state == 0)
      printf("Player found the exit\n\n");
    
   system("pause");
   return 0;
}
//////////////////
//MAZE GENERATOR//
//////////////////
void mazeGenerator( int wMaze[ MAX ][ MAX ], int wSize ){
   
   srand(time( 0 ));
   
   void initMaze( int wMaze[ MAX ][ MAX ] ); //initalize the maze array
   int walled( int wMaze[ MAX ][ MAX ], int wRow, int wCol );//check neighbouring cells
   
   initMaze( wMaze );
   
   int counter = 0, row = 1, col = 1, visited = 1;
   int validNeighbour, valid, randValid, move;
   int neighbourRow[ 4 ];
   int neighbourCol[ 4 ];
   int step[ 4 ];
   int btRow[ CELLS ]; //backtrack row array
   int btCol[ CELLS ];//backtrack column array
   
   //start backtracking with first cell
   btRow[ 0 ] = 1;
   btCol[ 0 ] = 1;
      
   /*while VisitedCells < TotalCells*/
   while(visited < wSize * wSize){
      //initialize
      validNeighbour = -1;
      
      /*find all neighbors of CurrentCell with all walls intact*/
      //NORTH neighbour
      if(row - 2 > 0 && walled( wMaze, row - 2, col )){
         validNeighbour++;
         neighbourRow[ validNeighbour ] = row - 2;
         neighbourCol[ validNeighbour ] = col;
         step[ validNeighbour ] = 1;       
      } 
      //WEST neighbour
      if(col - 2 > 0 && walled( wMaze, row, col - 2 )){
         validNeighbour++;
         neighbourRow[ validNeighbour ] = row;
         neighbourCol[ validNeighbour ] = col - 2;
         step[ validNeighbour ] = 2;         
      }
      //EAST neighbour
      if(col + 2 < wSize * 2 + 1 && walled( wMaze, row, col + 2 )){
         validNeighbour++;
         neighbourRow[ validNeighbour ] = row;
         neighbourCol[ validNeighbour ] = col + 2;
         step[ validNeighbour ] = 3;         
      } 
      //SOUTH neighbour
      //size * 2 + 1 -> size + wall + outerWall
      if(row + 2 < wSize * 2 + 1 && walled( wMaze, row + 2, col )){
         validNeighbour++;
         neighbourRow[ validNeighbour ] = row + 2;
         neighbourCol[ validNeighbour ] = col;
         step[ validNeighbour ] = 4;         
      }
      
      //if one or more found
      if(validNeighbour != -1){
         //choose one at random
         valid = validNeighbour + 1;//number of valid neighbour
         randValid = rand() % valid;
         
         //make the new cell CurrentCell
         row = neighbourRow[ randValid ];
         col = neighbourCol[ randValid ];
         
         counter++;
         
         btRow[ counter ] = row;
         btCol[ counter ] = col;
         
         move = step[ randValid ];
         
         //knock down the wall between it and CurrentCell
         //NORTH
         if(move == 1){
            wMaze[ row + 1 ][ col ] = PATH;        
         }
         //WEST
         else if(move == 2){
            wMaze[ row ][ col + 1 ] = PATH;     
         }
         //EAST
         else if(move == 3){
            wMaze[ row ][ col - 1 ] = PATH;     
         }
         //SOUTH
         else if(move == 4){
            wMaze[ row - 1 ][ col ] = PATH;     
         }
         
         //add 1 to VisitedCells
         visited++;
      }
      //if none found 
      else{
         //backtracking
         row = btRow[ counter ];
         col = btCol[ counter ];
         counter--;      
      }
       
   }                           
}

void initMaze( int wMaze[ MAX ][ MAX ] ){
   int a, b;
   
   for(a  = 0; a < MAX; a++){
      for(b = 0; b < MAX; b++){
         if(a % 2 == 0 || b % 2 == 0){
            wMaze[ a ][ b ] = WALL;   
         }else{
            wMaze[ a ][ b ] = PATH;      
         }
      }       
   }     
}

void printMaze( int wMaze[ MAX ][ MAX ], int wSize ){
   int row, col;
   
   printf("\n");
   
   for(row = 0; row < wSize * 2 + 1; row++){
           
      if(wMaze[ row ][ 0 ] == START)
         printf(" START -> ");
      else
         printf("          ");
         
      for(col = 0; col < wSize * 2 + 1; col++){
         if(wMaze[ row ][ col ] == WALL)
            printf("#");
         else if(wMaze[ row ][ col ] == START)
            printf("X");
         else
            printf(" ");        
      } 
      
      if(wMaze[ row ][ wSize * 2 ] == END)
         printf(" <- END");
                
      printf("\n");
   }  
   printf("\n"); 
   system("pause");
   system("cls");     
}

int walled( int wMaze[ MAX ][ MAX ], int wRow, int wCol ){
   if(wMaze[ wRow - 1 ][ wCol ] == WALL &&
      wMaze[ wRow + 1 ][ wCol ] == WALL &&
      wMaze[ wRow ][ wCol + 1 ] == WALL &&
      wMaze[ wRow ][ wCol - 1 ] == WALL )
      return 1;
      
   return 0;    
}
////////////////////////////////////
//START AND END POSITION GENERATOR//
////////////////////////////////////
void startEnd( int wMaze[ MAX ][ MAX ], int wSize, int* wSRow, int* wSCol ){
   int start, end;
   
   do{
      start = rand() % (wSize * 2);//size + wall
   }while(start == 0 || wMaze[ start ][ 1 ] == WALL );
          
   do{
      end = rand() % (wSize * 2);   
   }while(end == 0 || wMaze[ end ][ (wSize * 2) - 1 ] == WALL);  
   
   wMaze[ start ][ 0 ] =  START;
   wMaze[ end ][ wSize * 2 ] = END; 
   
   *wSRow = start;
   *wSCol = 0; 
}
/////////////////
//MAZE TRAVERSE//
/////////////////

//global
int startingFlag = 1, direction = 0;

//enumeration
enum Stat { OVER, NOT_OVER };

int mazeTraverse( int maze[ MAX ][ MAX ], int currentRow, int currentColumn, int size ){
   //sub-function
   int gameOver( const int currentRow, const int currentColumn, int size );
   int move( int maze[ MAX ][ MAX ], int *currentRow, int *currentColumn, int *currentDirection, int size );
   void printPosition( int maze[ MAX ][ MAX ], const int currentRow, const int currentColumn, int size );
   
   enum Stat State;
   
   //check if game is over
   State = gameOver( currentRow, currentColumn, size );

   if(State == OVER && startingFlag == 0){
      printPosition( maze, currentRow, currentColumn, size );
      return OVER; //return OVER indicating succesfully find the exit
      //algorithm for if this function can't find the exit is not available       
   }
   
   //indicate player is ready to move
   if(startingFlag == 1){
      //print initial maze
      printf("Player's ready\n**************\n");
      printPosition( maze, currentRow, currentColumn, size );
      system("pause");
      system("cls");
      //set first direction based on starting position
      if(currentRow == 0){
         direction = 1;              
      }else if(currentRow == 11){
         direction = 0;      
      }else if(currentColumn == 0){
         direction = 2;      
      }else if(currentColumn == 11){
         direction == 3;      
      }
      startingFlag = 0;             
   }  
   
   //seek for next move
   move( maze, ¤tRow, ¤tColumn, &direction, size );
   
   //system("pause");//activate this to see each move
   system("cls");
   
   mazeTraverse( maze, currentRow, currentColumn, size );
   
   return OVER;   
}

/*seek for next move*/
enum Direction { NORTH, SOUTH, EAST, WEST };
int move( int maze[ MAX ][ MAX ], int *currentRow, int *currentColumn, int *currentDirection, int size ){
   int posibble[ 4 ] = { 0 };// 1 -> North; 2 -> South; 3 -> East; 4 -> West;
   int counter = 0;
   
   enum Direction Seek; 
   
   Seek = *currentDirection;
   
   /*move the player respect to current direction*/
   
   //cover the current position
   maze[ *currentRow ][ *currentColumn ] = 0;
   
   //move the player respect to current direction
   if(Seek == NORTH){
      //print direction
      printf("direction = NORTH\n");
      //move north
      *currentRow -= 1;
   }else if(Seek == SOUTH){
      //print direction
      printf("direction = SOUTH\n");
      //move south
      *currentRow +=1;      
   }else if(Seek == EAST){
      //print direction
      printf("direction = EAST\n");
      //move east
      *currentColumn += 1;   
   }else if(Seek == WEST){
      //print direction
      printf("direction = WEST\n");
      //move west
      *currentColumn -= 1;
   }
   
   maze[ *currentRow ][ *currentColumn ] = 3;
   
   //print each move
   printPosition( maze, *currentRow, *currentColumn, size );// print maze with player current position
   
   /*analyse for next direction*/
   
   //seek posibble direction
   printf("Seek next direction...\n\n");
   if(maze[ *currentRow - 1 ][ *currentColumn ] == 0 && Seek != SOUTH){
      printf("NORTH is possible\n");
      posibble[ 0 ] = 1;
      counter++;
   }
   if(maze[ *currentRow + 1 ][ *currentColumn ] == 0 && Seek != NORTH){
      printf("SOUTH is possible\n"); 
      posibble[ 1 ] = 1;
      counter++; 
   } 
   if(maze[ *currentRow ][ *currentColumn + 1 ] == 0 && Seek != WEST && 
      maze[ *currentRow ][ *currentColumn + 1 ] != 4){
      printf("EAST is possible\n"); 
      posibble[ 2 ] = 1; 
      counter++; 
   } 
   if(maze[ *currentRow ][ *currentColumn - 1 ] == 0 && Seek != EAST){
      printf("WEST is possible\n");  
      posibble[ 3 ] = 1;
      counter++; 
   } 
   //if exit found
   if(maze[ *currentRow ][ *currentColumn + 1 ] == 4 && Seek != WEST){
      printf("Exit found\n"); 
      posibble[ 2 ] = 1; 
      counter++; 
   } 
   printf("\n");
   
   //follow right wall
   //Direction { NORTH, SOUTH, EAST, WEST };
   if(counter == 1){
      if(posibble[ 1 ] == 1){//south
         *currentDirection = 1;
      }else if(posibble[ 2 ] == 1){//east
         *currentDirection = 2;
      }else if(posibble[ 0 ] == 1){//north
         *currentDirection = 0;      
      }else if(posibble[ 3 ] == 1){//west
         *currentDirection = 3;
      }
   }else if(counter == 2){
      if(posibble[ 2 ] == 1 && posibble[ 3 ] == 1){// posibble: EAST, WEST
         if(Seek == SOUTH){
            *currentDirection = 3;
         }else if(Seek == NORTH){
            *currentDirection = 2;      
         }                
      }else if(posibble[ 0 ] == 1 && posibble[ 1 ] == 1){// posibble: NORTH,SOUTH
         if(Seek == EAST){
            *currentDirection = 1;        
         }else if(Seek == WEST){
            *currentDirection = 0;      
         }
      }else if(posibble[ 0 ] == 1 && posibble[ 3 ] == 1){// NORTHWEST
            *currentDirection = 0;        
      }else if(posibble[ 0 ] == 1 && posibble[ 2 ] == 1){// NORTHEAST
            *currentDirection = 2;        
      }else if(posibble[ 1 ] == 1 && posibble[ 2 ] == 1){// SOUTHEAST
            *currentDirection = 1;        
      }else if(posibble[ 1 ] == 1 && posibble[ 3 ] == 1){// SOUTHWEST
            *currentDirection = 3;        
      }
   }else if(counter == 3){
      if(Seek == NORTH){
         *currentDirection = 2;        
      }else if(Seek == SOUTH){
         *currentDirection = 3;      
      }else if(Seek == EAST){
         *currentDirection = 1;      
      }else if(Seek == WEST){
         *currentDirection = 0;      
      }
   }else if(counter == 0){
      //dead end
      if(Seek == NORTH){
         *currentDirection = 1;         
      }else if(Seek == SOUTH){
         *currentDirection = 0;      
      }else if(Seek == EAST){
         *currentDirection = 3;      
      }else if(Seek == WEST){
         *currentDirection = 2;      
      }
   }
   
}

/*check if game is over*/
int gameOver( const int currentRow, const int currentColumn, int size ){
   if(currentColumn == size * 2){
      return OVER;
   }else{
      return NOT_OVER;      
   }
   printf("position: (%d, %d)\n\n", currentRow, currentColumn);
}

/*print current position*/
 void printPosition( int maze[ MAX ][ MAX ], const int currentRow, const int currentColumn, int size ){
    int row, col;
   
   printf("\n");
   
   for(row = 0; row < size * 2 + 1; row++){
           
      if(maze[ row ][ 0 ] == START)
         printf(" START -> ");
      else
         printf("          ");
         
      for(col = 0; col < size * 2 + 1; col++){
         if(maze[ row ][ col ] == WALL)
            printf("#");
         else if(maze[ row ][ col ] == START)
            printf("X");
         else
            printf(" ");        
      } 
      
      if(maze[ row ][ size * 2 ] == END)
         printf(" <- END");
                
      printf("\n");
   }  
   printf("\n");  
 }

1) Maze: Maze Traversal

The recursive function mazeTraverse() below will walk through the maze using right hand wall method.
/*
 *   Title: Pointers 722 : Traversal Maze
 *          -Using Right Hand Wall Method/Algorithm
 *   Author: aeriqusyairi
 *   Date: Mac1 2012
 */
#include<stdio.h>
#include<stdlib.h> 

//main-function
int mazeTraverse( char mazePtr[ 12 ][ 12 ], int currentRow, int currentColumn );
void printMaze( char maze[ 12 ][ 12 ], const int currentRow, const int currentColumn );

int main( void ){
   int startRow = 2, startColumn = 0, state = 0;
                            // 0    1    2    3    4    5    6    7    8    9    10   11
   char maze[ 12 ][ 12 ] = {{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},  // 0
                             {'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'},  // 1
                             {'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'},  // 2
                             {'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},  // 3
                             {'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'},  // 4
                             {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},  // 5
                             {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},  // 6
                             {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},  // 7
                             {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},  // 8
                             {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},  // 9
                             {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},  // 10
                             {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };// 11
   
   printf("The maze\n********\n");
   printMaze( maze, 12, 12 );
   printf("  This program is brought to you\n  by aeriqusyairi...\n\n  Enjoy!!\n\n");
   system("pause");
   system("cls");
   //printf("\n");
   
   state = mazeTraverse( maze, startRow, startColumn );
   
   if(state == 0){
      printf("\nPlayer found the exit!\n\n");         
   }
   
   system("pause");
   return 0;
}

//global
int startingFlag = 1, direction = 0;

//enumeration
enum Stat { OVER, NOT_OVER };

/*core function*/
int mazeTraverse( char maze[ 12 ][ 12 ], int currentRow, int currentColumn ){
   //sub-function
   int gameOver( const int currentRow, const int currentColumn );
   int move( char maze[ 12 ][ 12 ], int *currentRow, int *currentColumn, int *currentDirection );
   
   enum Stat State;
   
   //check if game is over
   State = gameOver( currentRow, currentColumn );

   if(State == OVER && startingFlag == 0){
      printMaze( maze, currentRow, currentColumn );
      return OVER; //return OVER indicating succesfully find the exit
      //algorithm for if this function can't find the exit is not available       
   }
   
   //indicate player is ready to move
   if(startingFlag == 1){
      //print initial maze
      printf("Player's ready\n**************\n");
      printMaze( maze, currentRow, currentColumn );
      system("pause");
      system("cls");
      //set first direction based on starting position
      if(currentRow == 0){
         direction = 1;              
      }else if(currentRow == 11){
         direction = 0;      
      }else if(currentColumn == 0){
         direction = 2;      
      }else if(currentColumn == 11){
         direction == 3;      
      }
      startingFlag = 0;             
   }  
   
   //seek for next move
   move( maze, ¤tRow, ¤tColumn, &direction );
   
   //system("pause");//activate this to see each move
   system("cls");
   
   mazeTraverse( maze, currentRow, currentColumn );
   
   return OVER;
}

/*seek for next move*/
enum Direction { NORTH, SOUTH, EAST, WEST };
int move( char maze[ 12 ][ 12 ], int *currentRow, int *currentColumn, int *currentDirection ){
   int posibble[ 4 ] = { 0 };// 1 -> North; 2 -> South; 3 -> East; 4 -> West;
   int counter = 0;
   
   enum Direction Seek; 
   
   Seek = *currentDirection;
   
   /*move the player respect to current direction*/
   
   //cover the current position
   maze[ *currentRow ][ *currentColumn ] = '.';
   
   //move the player respect to current direction
   if(Seek == NORTH){
      //print direction
      printf("direction = NORTH\n");
      //move north
      *currentRow -= 1;
   }else if(Seek == SOUTH){
      //print direction
      printf("direction = SOUTH\n");
      //move south
      *currentRow +=1;      
   }else if(Seek == EAST){
      //print direction
      printf("direction = EAST\n");
      //move east
      *currentColumn += 1;   
   }else if(Seek == WEST){
      //print direction
      printf("direction = WEST\n");
      //move west
      *currentColumn -= 1;
   }
   
   //print each move
   printMaze( maze, *currentRow, *currentColumn );// print maze with player current position
   
   /*analyse for next direction*/
   
   //seek posibble direction
   printf("Seek next direction...\n\n");
   if(maze[ *currentRow - 1 ][ *currentColumn ] == '.' && Seek != SOUTH){
      printf("NORTH is possible\n");
      posibble[ 0 ] = 1;
      counter++;
   }
   if(maze[ *currentRow + 1 ][ *currentColumn ] == '.' && Seek != NORTH){
      printf("SOUTH is possible\n"); 
      posibble[ 1 ] = 1;
      counter++; 
   } 
   if(maze[ *currentRow ][ *currentColumn + 1 ] == '.' && Seek != WEST){
      printf("EAST is possible\n"); 
      posibble[ 2 ] = 1; 
      counter++; 
   } 
   if(maze[ *currentRow ][ *currentColumn - 1 ] == '.' && Seek != EAST){
      printf("WEST is possible\n");  
      posibble[ 3 ] = 1;
      counter++; 
   }  
   printf("\n");
   
   //follow right wall
   //Direction { NORTH, SOUTH, EAST, WEST };
   if(counter == 1){
      if(posibble[ 1 ] == 1){//south
         *currentDirection = 1;
      }else if(posibble[ 2 ] == 1){//east
         *currentDirection = 2;
      }else if(posibble[ 0 ] == 1){//north
         *currentDirection = 0;      
      }else if(posibble[ 3 ] == 1){//west
         *currentDirection = 3;
      }
   }else if(counter == 2){
      if(posibble[ 2 ] == 1 && posibble[ 3 ] == 1){// posibble: EAST, WEST
         if(Seek == SOUTH){
            *currentDirection = 3;
         }else if(Seek == NORTH){
            *currentDirection = 2;      
         }                
      }else if(posibble[ 0 ] == 1 && posibble[ 1 ] == 1){// posibble: NORTH,SOUTH
         if(Seek == EAST){
            *currentDirection = 1;        
         }else if(Seek == WEST){
            *currentDirection = 0;      
         }
      }else if(posibble[ 0 ] == 1 && posibble[ 3 ] == 1){// NORTHWEST
            *currentDirection = 0;        
      }else if(posibble[ 0 ] == 1 && posibble[ 2 ] == 1){// NORTHEAST
            *currentDirection = 2;        
      }else if(posibble[ 1 ] == 1 && posibble[ 2 ] == 1){// SOUTHEAST
            *currentDirection = 1;        
      }else if(posibble[ 1 ] == 1 && posibble[ 3 ] == 1){// SOUTHWEST
            *currentDirection = 3;        
      }
   }else if(counter == 3){
      if(Seek == NORTH){
         *currentDirection = 2;        
      }else if(Seek == SOUTH){
         *currentDirection = 3;      
      }else if(Seek == EAST){
         *currentDirection = 1;      
      }else if(Seek == WEST){
         *currentDirection = 0;      
      }
   }else if(counter == 0){
      //dead end
      if(Seek == NORTH){
         *currentDirection = 1;         
      }else if(Seek == SOUTH){
         *currentDirection = 0;      
      }else if(Seek == EAST){
         *currentDirection = 3;      
      }else if(Seek == WEST){
         *currentDirection = 2;      
      }
   }
   
}

/*check if game is over*/
int gameOver( const int currentRow, const int currentColumn ){
   if(currentRow == 0 || currentRow == 11 || currentColumn == 0 || currentColumn == 11 ){
      return OVER;
   }else{
      return NOT_OVER;      
   }
}

/*print current maze*/
 void printMaze( char maze[ 12 ][ 12 ], const int currentRow, const int currentColumn ){
    int mazeRow, mazeColumn;
    
    printf("\n    "); 
    for(mazeRow = 0; mazeRow < 12; mazeRow++){
      for(mazeColumn = 0; mazeColumn < 12; mazeColumn++){
         if(mazeRow == currentRow && mazeColumn == currentColumn){
            maze[ mazeRow ][ mazeColumn ] = 'X';
         }
         printf("%2c", maze[ mazeRow ][ mazeColumn ] );
         
      }
      printf("\n    ");
   }   
   printf("\n");   
 }

Simulation: The Tortoise and the Hare

This program recreate one of the truly great moments in history, namely the classic race of the tortoise and the hare.
So our contenders begin the race at "square 1" of 70 square.Each square represents a possible position along the race course.The finish line is at square 70.The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground.There is a clock that ticks once per second.With each tick of the clock, this program will adjust the position of the animals according to the following rules:

Animal Move type Percentage of the time Actual move
Tortoise Fast plod 50% 3 squares to the right
Slip 20% 6 squares to the left
Slow pod 30% 1 squares to the right
Hare Sleep 20% No move at all
Big hop 20% 9 squares to the right
Big slip 10% 12 squares to the left
Small hop 30% 1 square to the right
Small slip 20% 2 squares to the left

/*
 *    Title: Pointers 717 : Simulasi Kura-kura dan Arnab
 *    Author: aeriqusyairi
 *    Date: 29Feb 2012
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define FINISH 70

void moveTortoise( int* tortoisePtr );
void moveHare( int* harePtr );
void printPosition( const int* const tortoisePtr, const int* const harePtr );
 
int main( void ){
   int tortoise = 1, hare = 1, timer = 0;
   
   srand( time( 0 ) );
   
   //interface
   printf("Tortoise and Hare\n*****************\n\n");
   printf("BANG !!!!!\nAND THEY'RE OFF !!!!\n\n");
   
   //move untill either contender finish the race
   while(tortoise != FINISH && hare != FINISH){
      //move the tortoise
      moveTortoise( &tortoise );
      //move the hare
      moveHare( &hare );
      //print current position
      printPosition( &tortoise, &hare ); 
      //calculate the timer
      timer++;      
   }
   
   //the result
   if(tortoise > hare){
      printf("TORTOISE WIN !!! YAY !!!\n");         
   }else if(tortoise < hare){
      printf("HARE WIN !!! YUCH !!!\n");      
   }
   
   printf("Time Elapsed: %d seconds\n", timer);
   
   
   system("pause");
   return 0;
}

void moveTortoise( int* tortoisePtr ){
   int i;
   
   i = rand() % 10;
   
   if(i >= 1 && i <= 5){
      //1 - 5 : Fast plod  
      *tortoisePtr += 3; //move 3 square to the right  
   }else if(i >= 6 && i <= 7){
      //6 - 7 : Slip      
      *tortoisePtr -= 6; //move 6 square to the left
   }else{
      //8 - 10 : Slow plod      
      *tortoisePtr += 1; //move 1 square to the right
   }
   
   if(*tortoisePtr < 1)
      *tortoisePtr = 1;
   else if(*tortoisePtr > FINISH)
      *tortoisePtr = FINISH;
}

void moveHare( int* harePtr ){
   int i;
   
   i = rand() % 10;
   
   if(i >= 1 && i <= 2){
      //1 - 2 : Sleep   
      *harePtr = *harePtr;  
   }else if(i >= 3 && i <= 4){
      //3 - 4 : Big hop   
      *harePtr += 9;   
   }else if(i == 5){
      //5 : Big slip
      *harePtr -= 12;
   }else if(i >= 6 && i<= 8){
      //6 - 8 : Small hop
      *harePtr += 1;
   }else{
      //9 - 10 : Small slip
      *harePtr -= 2;      
   }
   
   if(*harePtr < 1)
      *harePtr = 1;
   else if(*harePtr > FINISH)
      *harePtr = FINISH;
}

void printPosition( const int* const tortoisePtr, const int* const harePtr ){
   int i;
   
   if(*tortoisePtr == *harePtr){
      for(i = 1; i < *tortoisePtr; i++){
         printf("%s", " " );
      }
      printf("OUCH!!!");                
   }else if(*tortoisePtr < *harePtr){
      for(i = 1; i < *tortoisePtr; i++ ){
         printf("%s", " " );
      }   
      printf("T");
      
      for(i = 1; i < (*harePtr - *tortoisePtr); i++){
         printf("%s", " " );
      }
      printf("H");
   }else{
      for(i = 1; i < *harePtr; i++){
         printf("%s", " ");      
      }     
      printf("H");
      
      for( i = 0; i < (*tortoisePtr - *harePtr); i++){
         printf("%s", " " );     
      }
      printf("T");
   }
   
   printf("\n");
}

6) Card Shuffling And Dealing: Modification

The previous shuffling and dealing algorithm is inefficient which may caused the possibility of indefinite postponement.Program below improvised that previous algorithm to avoid indefinite postponement.
/*
 *  Pointers 716 : Card Shuffling And Dealing Modification
 *
 *  Created on: Feb 29, 2012
 *      Author: aeriquyairi
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void shuffle( int wDeck[][ 13 ] );
void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[] );
void printDeck( const int wDeck[][ 13 ] );

int main(){
   const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
   
   const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",
                              "Five", "Six", "Seven", "Eight",
                              "Nine", "Ten", "Jack", "Queen", "King" };
   
   //initialize deck array                          
   int deck[ 4 ][ 13 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
                           14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 
                           27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
                           40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 };
   
   srand( time( 0 ) );
   printf("Initial Deck\n************\n\n");
   printDeck( deck );
   shuffle( deck );
   printf("Shuffled Deck\n*************\n\n");
   printDeck( deck );
   printf("Deal\n****\n\n");
   deal( deck, face, suit );
   
   system("pause");
   return 0;
}

void shuffle( int wDeck[][ 13 ] ){
   //shuffling algorithm improvised
   int row, column, swapRow, swapColumn, hold;
   
   for(row = 0; row < 4; row++){
      for(column = 0; column < 13; column++){
         do{
            swapRow = rand() % 4;
            swapColumn = rand() % 13;
         }while(wDeck[ swapRow ][ swapColumn ] == wDeck[ row ][ column ]);
         
         //swap each element
         hold = wDeck[ row ][ column ];
         wDeck[ row ][ column ] = wDeck[ swapRow ][ swapColumn ];
         wDeck[ swapRow ][ swapColumn ] = hold;
      }
   }
}

void printDeck( const int wDeck[][ 13 ] ){
   int row, column;
   
   for(row = 0; row < 4; row++){
      for(column = 0; column < 13; column++){
         printf("%5d", wDeck[ row ][ column ]);
      }
      
      printf("\n");
   }   
   
   printf("\n");  
}

void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[] ){
   //dealing algorithm improvised
   int card, row, column, dealt;
   
   for(card = 1, dealt = 0; card <= 52; card++, dealt = 0){
      for(row = 0; row <= 3; row++){
         for(column = 0; column <= 12; column++){
            if(wDeck[ row ][ column ] == card){
               printf("%5s of %-8s%c", wFace[ column ], wSuit[ row ], card % 2 == 0 ? '\n' : '\t' );
               dealt = 1;
               break;
            }
         }
         
         if(dealt == 1){
            break;     
         }
      }
   }
}

5) Card Shuffling And Dealing: Five-card Poker Hand Players Option

Now the player got option to draw up to three cards similar to the dealer. :)
/*
 *  Pointers 715 : Simulate Dealers Hand And Players Option
 *
 *  Created on: Feb 28, 2012
 *      Author: aeriqusyairi
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
//main functions
void shuffle( int wDeck[][ 13 ] ); 
void deal( const int wDeck[][ 13 ], const char *wStoreFace[], const char *wStoreSuit[] );
void initialDisplay( const char *wPFace[], const char *wPSuit[], const char *wDFace[], const char *wDSuit[] );

int main(){
   //initialize suit array
   const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    
   //initalize face array
   const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",
                               "Five", "Six", "Seven", "Eight",
                               "Nine", "Ten", "Jack", "Queen", "King" };
    
   //initialize deck array
   int deck[ 4 ][ 13 ] = { 0 };
    
   //seed random number generator
   srand(time(0));
    
   shuffle( deck );//shuffle the deck
   deal( deck, face, suit );//deal two five-hand card and evaluate
    
   system("pause");
   return 0;    
}
 
void shuffle( int wDeck[][ 13 ] ){
   int row, column, card;

	for(card = 1; card <= 52; card++){
		do{
			row = rand() % 4;
			column = rand() % 13;
		}while(wDeck[ row ][ column ] != 0);

		wDeck[ row ][ column ] = card;
		
	}
}

void deal( const int wDeck[][ 13 ], const char *wStoreFace[], const char *wStoreSuit[] ){
   //operational functions
   void aPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] );
   void twoPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] );
   void threeKind( const char *wfFace[], const char *wPDSuit[], int result[], int wPair[] );
   void fourKind( const char *wfFace[], const char *wPDSuit[], int result[], int wPair[] );
   void flush( const char *wPDFace[], const char *wPDSuit[], int result[] );
   void straight( const char *wPDFace[], const char *wPDSuit[], const char *wStoreFace[], int result[] );
   
   //sub-operational functions
   void draw( const int wDeck[][ 13 ], char *wDFace[], char *wDSuit[], const char *wStoreFace[], const char *wStoreSuit[], int wPair[] );
   void evalDisplay( const char *wPDFace[], const char *wPDSuit[], const int wPair[], const int wType );
   void final( const int wPResult[], const int wDResult[] );
   
   //variable
	int card, row, column, i, j = 0, k = 0;
	int pair[ 5 ] = { 0 };
	int change[ 5 ] = { 0 };
	
	//storage for two five-hand card
	//result boolean: 0:pair 1:two pair 2:three of a kind 3:four of a kind 4:flush 5:straight
	
	//player
   char *PSuit[ 5 ] ;//= { "Hearts", "Hearts", "Hearts", "Hearts", "Hearts" };
   char *PFace[ 5 ] ;//= { "Ace", "Deuce", "Seven", "Three", "Four" };
   int PResult[ 6 ] = { 0 };
   //dealer
   char *DSuit[ 5 ] ;//= { "Spades", "Hearts", "Hearts", "Hearts", "Hearts" };
   char *DFace[ 5 ] ;//= { "Ace", "Ace", "Three", "Six", "Five" };
   int DResult[ 6 ] = { 0 };




	//deal 5-card poker hand 
	for(card = 1, i = 0; card <= 10; card++, i++){
		for(row = 0; row < 4; row++){
			for(column = 0; column < 13; column++){
				if(wDeck[ row ][ column ] == card && i < 5){
               PSuit[ i ] = wStoreSuit[ row ];
               PFace[ i ] = wStoreFace[ column ];
	         }else if(wDeck[ row ][ column ] == card && i > 4){
               DSuit[ j ] = wStoreSuit[ row ];
               DFace[ j ] = wStoreFace[ column ];
               j++;      
            }
			}
		}
	}

	//display two five-card poker hand
	initialDisplay( PFace, PSuit, DFace, DSuit );
	
	//dealer modification
	//debug
	//printf("\nDealer modify\n***********\n\n");
	
	straight( DFace, DSuit, wStoreFace, DResult ); 
   //printf("\n  DResult[ 5 ] = %d\n", DResult[ 5 ] );
   if(DResult[ 5 ] == 0){
      flush( DFace, DSuit, DResult );
	   //printf("\n  DResult[ 4 ] = %d\n", DResult[ 4 ] );
	   if(DResult[ 4 ] == 0){
         fourKind( DFace, DSuit, DResult, pair );
	      //printf("\n  DResult[ 3 ] = %d\n", DResult[ 3 ] );
	      if(DResult[ 3 ] == 0){
            threeKind( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 2 ] = %d\n", DResult[ 2 ] );
	         if(DResult[ 2 ] == 0){
            twoPair( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 1 ] = %d\n", DResult[ 1 ] );
	            if(DResult[ 1 ] == 0){
                  aPair( DFace, DSuit, DResult, pair );
	               //printf("\n  DResult[ 0 ] = %d\n", DResult[ 0 ] );
	               if(DResult[ 0 ] == 0){
                     //debug
                     //printf("Very weak!\n");
                     
                     draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair ); 
                  }else if(DResult[ 0 ] == 1){
                     draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );         
                  }
               }else if(DResult[ 1 ] == 1){
                  draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );        
               }
            }else if(DResult[ 2 ] == 1){
               draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );        
            }
         }else if(DResult[ 3 ] == 1){
            draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );       
         }
      }
   }
   
   //player modification
   printf("\n****************\nModify your card\n****************\n\n");
   //lets user modify their card
	printf("Which card you wanna change?(1 = change, 0 = don't change)\n\n");
	for(i = 0; i < 5; i++){
      printf("Card %d: ", i + 1);  
      scanf("%d", &change[ i ]); 
      if(change[ i ] == 0){
         change[ i ] = 1;
      }else if(change[ i ] == 1){
         change[ i ] = 0;      
      }else{
         printf("Invalid!\n");
         i--;           
      }  
   }
   
   //check if any modification needed
   for(i = 0; i < 5; i++){
      if(change[ i ] == 0)
         k++;      
   }
   
   if(k != 0){
      draw( wDeck, PFace, PSuit, wStoreFace, wStoreSuit, change );  
      initialDisplay( PFace, PSuit, DFace, DSuit );    
   }else{
      printf("No modification made.\n");      
   }
	
	//evaluate
	printf("\n******************\nEvaluate both hand\n******************\n\nPlayer hand\n**********\n\n");
	straight( PFace, PSuit, wStoreFace, PResult ); 
   //printf("\n  PResult[ 5 ] = %d\n", PResult[ 5 ] );
   if(PResult[ 5 ] == 0){
      flush( PFace, PSuit, PResult );
	   //printf("\n  PResult[ 4 ] = %d\n", PResult[ 4 ] );
	   if(PResult[ 4 ] == 0){
	      fourKind( PFace, PSuit, PResult, pair );
	      //printf("\n  PResult[ 3 ] = %d\n", PResult[ 3 ] );
	      if(PResult[ 3 ] == 0){
	         threeKind( PFace, PSuit, PResult, pair );
	         //printf("\n  PResult[ 2 ] = %d\n", PResult[ 2 ] );
	         if(PResult[ 2 ] == 0){
	            twoPair( PFace, PSuit, PResult, pair );
	            //printf("\n  PResult[ 1 ] = %d\n", PResult[ 1 ] );
	            if(PResult[ 1 ] == 0){
	               aPair( PFace, PSuit, PResult, pair );
	               //printf("\n  PResult[ 0 ] = %d\n", PResult[ 0 ] );
	               if(PResult[ 0 ] == 0){
                     printf("  Very weak!\n");
                  }else if(PResult[ 0 ] == 1){
                     evalDisplay( PFace, PSuit, pair, 0 );         
                  }
               }else if(PResult[ 1 ] == 1){
                  evalDisplay( PFace, PSuit, pair, 1 );        
               }
            }else if(PResult[ 2 ] == 1){
               evalDisplay( PFace, PSuit, pair, 2 );        
            }
         }else if(PResult[ 3 ] == 1){
            evalDisplay( PFace, PSuit, pair, 3 );        
         }
      }
   }
	
	printf("\nDealer hand\n***********\n\n");
	straight( DFace, DSuit, wStoreFace, DResult ); 
   //printf("\n  DResult[ 5 ] = %d\n", DResult[ 5 ] );
   if(DResult[ 5 ] == 0){
      flush( DFace, DSuit, DResult );
	   //printf("\n  DResult[ 4 ] = %d\n", DResult[ 4 ] );
	   if(DResult[ 4 ] == 0){
         fourKind( DFace, DSuit, DResult, pair );
	      //printf("\n  DResult[ 3 ] = %d\n", DResult[ 3 ] );
	      if(DResult[ 3 ] == 0){
            threeKind( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 2 ] = %d\n", DResult[ 2 ] );
	         if(DResult[ 2 ] == 0){
            twoPair( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 1 ] = %d\n", DResult[ 1 ] );
	            if(DResult[ 1 ] == 0){
                  aPair( DFace, DSuit, DResult, pair );
	               //printf("\n  DResult[ 0 ] = %d\n", DResult[ 0 ] );
	               if(DResult[ 0 ] == 0){
                     printf("  Very weak!\n");
                  }else if(DResult[ 0 ] == 1){
                     evalDisplay( DFace, DSuit, pair, 0 );         
                  }
               }else if(DResult[ 1 ] == 1){
                  evalDisplay( DFace, DSuit, pair, 1 );        
               }
            }else if(DResult[ 2 ] == 1){
               evalDisplay( DFace, DSuit, pair, 2 );        
            }
         }else if(DResult[ 3 ] == 1){
            evalDisplay( DFace, DSuit, pair, 3 );        
         }
      }
   }
	
	//result
	final( PResult, DResult );
	
}

void draw( const int wDeck[][ 13 ], char *wDFace[], char *wDSuit[], const char *wStoreFace[], const char *wStoreSuit[], int wPair[] ){
   int i, j, zero = 0, card, row, column;
   
   //find how many zero
   for(i = 0 ; i < 5 ; i++){
      if(wPair[ i ] == 0)
         zero++;         
   }    
   if(zero == 5){
      zero = 2;
   }else{
      zero -= 1;
   }
   
   //debug
   //printf("zero = %d\n", zero); 
    
   
   //draw new card respect to number of zero
   for(card = 11; card <= (11 + zero); card++){
		for(row = 0; row < 4; row++){
			for(column = 0; column < 13; column++){
				if(wDeck[ row ][ column ] == card){
               //printf("%5d\n", card);             
               for(j = 0; j < 5; j++){
                  if(wPair[ j ] == 0){
                     wDSuit[ j ] = wStoreSuit[ row ];
                     wDFace[ j ] = wStoreFace[ column ];
                     wPair[ j ] = 1;
                     break;
                  }
               }
	         }
			}
		}
	}
	
	//debug
	/*for(i = 0; i < 5; i++){
		printf("%10s of %-8s\n", wDFace[ i ], wDSuit[ i ] );
	}*/
}

void initialDisplay( const char *wPFace[], const char *wPSuit[], const char *wDFace[], const char *wDSuit[] ){
   int i;
   
   printf("*************************\nYour five-card poker hand\n*************************\n\nPlayer hand %28s\n*********** %28s\n\n", "Dealer hand", "***********");
	for(i = 0; i < 5; i++){
		printf("%d.%9s of %-8s %10s of %-8s\n", i + 1,wPFace[ i ], wPSuit[ i ], "*****", "*****" );
	}  
}

void evalDisplay( const char *wPDFace[], const char *wPDSuit[], const int wPair[], const int wType ){
   int card;
   char *type[ 4 ] = { "Pair", "Two-pair", "Three of a kind", "Four of a kind" };
   
   printf("\n%s...\n", type[ wType ] );
   for(card = 0; card < 5; card++){
      if(wPair[ card ] != 0){
         printf("\n%7s of %-9s\n", wPDFace[ card ], wPDSuit[ card ] );
      }
   }             
}

void final( const int wPResult[], const int wDResult[] ){
   int i, x = 0, y = 0;
   
   printf("\n******\nResult\n******\n\n");
   
   //evaluate both hand
   for(i = 0; i < 6; i++){
      if(wPResult[ i ] == 1)
         x = i + 1;
      if(wDResult[ i ] == 1)
         y = i + 1;      
   }
   
   //debug
   //printf("x = %d  y = %5d\n\n", x, y);
   
   if(x > y)
      printf("Players win!\n\n");
   else if(y > x)
      printf("Dealers win!\n\n");
   else if(x == y)
      printf("Its a tie!\n\n");
   else
      printf("Unable to evaluate!\n\n");
}

void aPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;

   //compute whether the five-hand card contains a pair
   for(card = 0; card < 5; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            j++;
            wPair[ card ] = 1;
            wPair[ i ] = 1;
  	 	   }
 	   }
   }
    
   //assign result
   if(j == 1){
      result[ 0 ] = 1;
   }    
}

void twoPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether the five-hand card contains two pair
   for(card = 0; card < 5; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            j++;
            wPair[ card ] = 1;
            wPair[ i ] = 1;
 	 	   }  
      }
   }
    
   //assign result
   if(j == 2){
      result[ 1 ] = 1;
   }  
}

void threeKind( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k, x;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether the five-hand card contains three of a kind
   for(card = 0; card < 3; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            for(x = i + 1; x < 5; x++){
               if(wPDFace[ i ] == wPDFace[ x ]){
                  j++;
                  wPair[ card ] = 1;
                  wPair[ i ] = 1;
                  wPair[ x ] = 1;          
               }      
            }
    	   }  
      }
   }
    
   //assign result
   if(j == 1){
      result[ 2 ] = 1;
   }          
}

void fourKind( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, indi = 0;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether your five-hand card contains four-of-a-kind
   for(card = 0; card < 2; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){  
            indi++;
            if(indi == 1)
               wPair[ card ] = 1;
            wPair[ i ] = 1;    
             
         }
      }         
      if(indi == 3)
         break;
      else if(indi > 3)
         break;
      else{
         indi = 0;
         for(i = 0; i < 5; i++)
            wPair[ i ] = 0;
      }
          
   }     
    
   //assign result
   if(indi == 3){
      result[ 3 ] = 1;
   } 
}

void flush( const char *wPDFace[], const char *wPDSuit[], int result[] ){
   int i, j = 0;
   
   //compute whether your five-hand card contains a flush
   for(i = 1; i < 5; i++){
      if(wPDSuit[ 0 ] == wPDSuit[ i ] )
         j++;
   }
   
   //display result
   if(j == 4){
      result[ 4 ] = 1;
      printf("\nFlush...\n");
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wPDFace[ i ], wPDSuit[ i ] );
      }
   }   
}

void straight( const char *wPDFace[], const char *wPDSuit[], const char *wStoreFace[], int result[] ){
   int i, j, k, pass, count, hold, check = 1;
   int faceValue[ 5 ] = { 0 };
   
   //locate face value and store in an array
   for(i = 0 ; i < 5; i++){
      for(j = 0 ; j < 13; j++){
         if(wPDFace[ i ] == wStoreFace[ j ]){
            faceValue[ i ] = j;           
         }      
      }
   }
   
   //sort face value in ascending order using bubble sort
   for(pass = 0; pass < 4; pass++){
      for(count = 0; count < 4; count++){
         if(faceValue[ count ] > faceValue[ count + 1 ]){
            //swap
            hold = faceValue[ count ];
            faceValue[ count ] = faceValue[ count + 1 ];
            faceValue[ count + 1 ] = hold;               
         }          
      }         
   }
   
   //check if the hand contains a straight
   for(i = 0; i < 4; i++){
      if(faceValue[ i ] + 1 != faceValue[ i + 1 ]){
         check = 0;              
      }      
   }
   
   //display result
   if(check == 1){
      result[ 5 ] = 1;
      printf("\nStraight...\n");
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wPDFace[ i ], wPDSuit[ i ] );
      }
   }    
}

4) Card Shuffling and Dealing: Five-card poker Hand Dealer's Simulation

Simulate the dealer in five-card poker hand.
The dealer's five-card hand is dealt "face down" so the player can't see it.The program will evaluate the dealer's hand, and based on the quality of the hand, the dealer will automatically draw one, two, or three more cards to replace the corresponding number of unneeded cards in the original hands.Then the program will re-evaluate the dealer's hand.
/*
 *  Pointers 714 : Simulate Dealers Hand
 *
 *  Created on: Feb 28, 2012
 *      Author: aeriqusyairi
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
//main functions
void shuffle( int wDeck[][ 13 ] ); 
void deal( const int wDeck[][ 13 ], const char *wStoreFace[], const char *wStoreSuit[] );
void initialDisplay( const char *wPFace[], const char *wPSuit[], const char *wDFace[], const char *wDSuit[] );

int main(){
   //initialize suit array
   const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    
   //initalize face array
   const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",
                               "Five", "Six", "Seven", "Eight",
                               "Nine", "Ten", "Jack", "Queen", "King" };
    
   //initialize deck array
   int deck[ 4 ][ 13 ] = { 0 };
    
   //seed random number generator
   srand(time(0));
    
   shuffle( deck );//shuffle the deck
   deal( deck, face, suit );//deal two five-hand card and evaluate
    
   system("pause");
   return 0;    
}
 
void shuffle( int wDeck[][ 13 ] ){
   int row, column, card;

	for(card = 1; card <= 52; card++){
		do{
			row = rand() % 4;
			column = rand() % 13;
		}while(wDeck[ row ][ column ] != 0);

		wDeck[ row ][ column ] = card;
		
	}
}

void deal( const int wDeck[][ 13 ], const char *wStoreFace[], const char *wStoreSuit[] ){
   //operational functions
   void aPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] );
   void twoPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] );
   void threeKind( const char *wfFace[], const char *wPDSuit[], int result[], int wPair[] );
   void fourKind( const char *wfFace[], const char *wPDSuit[], int result[], int wPair[] );
   void flush( const char *wPDFace[], const char *wPDSuit[], int result[] );
   void straight( const char *wPDFace[], const char *wPDSuit[], const char *wStoreFace[], int result[] );
   
   //sub-operational functions
   void draw( const int wDeck[][ 13 ], char *wDFace[], char *wDSuit[], const char *wStoreFace[], const char *wStoreSuit[], int wPair[] );
   void evalDisplay( const char *wPDFace[], const char *wPDSuit[], const int wPair[], const int wType );
   void final( const int wPResult[], const int wDResult[] );
   
   //variable
	int card, row, column, i, j = 0;
	int pair[ 5 ] = { 0 };
	
	//storage for two five-hand card
	//result boolean: 0:pair 1:two pair 2:three of a kind 3:four of a kind 4:flush 5:straight
	
	//player
        char *PSuit[ 5 ] ;//= { "Hearts", "Hearts", "Hearts", "Hearts", "Hearts" };
        char *PFace[ 5 ] ;//= { "Ace", "Deuce", "Seven", "Three", "Four" };
        int PResult[ 6 ] = { 0 };
        //dealer
        char *DSuit[ 5 ] ;//= { "Spades", "Hearts", "Hearts", "Hearts", "Hearts" };
        char *DFace[ 5 ] ;//= { "Ace", "Ace", "Three", "Six", "Five" };
        int DResult[ 6 ] = { 0 };

	//deal 5-card poker hand 
	for(card = 1, i = 0; card <= 10; card++, i++){
		for(row = 0; row < 4; row++){
			for(column = 0; column < 13; column++){
				if(wDeck[ row ][ column ] == card && i < 5){
               PSuit[ i ] = wStoreSuit[ row ];
               PFace[ i ] = wStoreFace[ column ];
	         }else if(wDeck[ row ][ column ] == card && i > 4){
               DSuit[ j ] = wStoreSuit[ row ];
               DFace[ j ] = wStoreFace[ column ];
               j++;      
            }
			}
		}
	}

	//display two five-card poker hand
	initialDisplay( PFace, PSuit, DFace, DSuit );
	
	//dealer modification
	//debug
	//printf("\nDealer modify\n***********\n\n");
	
	straight( DFace, DSuit, wStoreFace, DResult ); 
        //printf("\n  DResult[ 5 ] = %d\n", DResult[ 5 ] );
        if(DResult[ 5 ] == 0){
           flush( DFace, DSuit, DResult );
	   //printf("\n  DResult[ 4 ] = %d\n", DResult[ 4 ] );
	   if(DResult[ 4 ] == 0){
               fourKind( DFace, DSuit, DResult, pair );
	      //printf("\n  DResult[ 3 ] = %d\n", DResult[ 3 ] );
	      if(DResult[ 3 ] == 0){
                 threeKind( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 2 ] = %d\n", DResult[ 2 ] );
	         if(DResult[ 2 ] == 0){
                 twoPair( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 1 ] = %d\n", DResult[ 1 ] );
	            if(DResult[ 1 ] == 0){
                       aPair( DFace, DSuit, DResult, pair );
	               //printf("\n  DResult[ 0 ] = %d\n", DResult[ 0 ] );
	               if(DResult[ 0 ] == 0){
                          //debug
                          //printf("Very weak!\n");
                     
                          draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair ); 
                       }else if(DResult[ 0 ] == 1){
                          draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );         
                       }
                    }else if(DResult[ 1 ] == 1){
                        draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );        
                    }
                }else if(DResult[ 2 ] == 1){
                   draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );        
                }
            }else if(DResult[ 3 ] == 1){
               draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );       
            }
         }
       }
	
	//evaluate
	printf("\n******************\nEvaluate both hand\n******************\n\nPlayer hand\n**********\n\n");
	straight( PFace, PSuit, wStoreFace, PResult ); 
        //printf("\n  PResult[ 5 ] = %d\n", PResult[ 5 ] );
        if(PResult[ 5 ] == 0){
           flush( PFace, PSuit, PResult );
	   //printf("\n  PResult[ 4 ] = %d\n", PResult[ 4 ] );
	   if(PResult[ 4 ] == 0){
	      fourKind( PFace, PSuit, PResult, pair );
	      //printf("\n  PResult[ 3 ] = %d\n", PResult[ 3 ] );
	      if(PResult[ 3 ] == 0){
	         threeKind( PFace, PSuit, PResult, pair );
	         //printf("\n  PResult[ 2 ] = %d\n", PResult[ 2 ] );
	         if(PResult[ 2 ] == 0){
	            twoPair( PFace, PSuit, PResult, pair );
	            //printf("\n  PResult[ 1 ] = %d\n", PResult[ 1 ] );
	            if(PResult[ 1 ] == 0){
	               aPair( PFace, PSuit, PResult, pair );
	               //printf("\n  PResult[ 0 ] = %d\n", PResult[ 0 ] );
	               if(PResult[ 0 ] == 0){
                     printf("Very weak!\n");
                  }else if(PResult[ 0 ] == 1){
                     evalDisplay( PFace, PSuit, pair, 0 );         
                  }
               }else if(PResult[ 1 ] == 1){
                  evalDisplay( PFace, PSuit, pair, 1 );        
               }
            }else if(PResult[ 2 ] == 1){
               evalDisplay( PFace, PSuit, pair, 2 );        
            }
         }else if(PResult[ 3 ] == 1){
            evalDisplay( PFace, PSuit, pair, 3 );        
         }
      }
   }
	
	printf("\nDealer hand\n***********\n\n");
	straight( DFace, DSuit, wStoreFace, DResult ); 
        //printf("\n  DResult[ 5 ] = %d\n", DResult[ 5 ] );
        if(DResult[ 5 ] == 0){
           flush( DFace, DSuit, DResult );
	   //printf("\n  DResult[ 4 ] = %d\n", DResult[ 4 ] );
	   if(DResult[ 4 ] == 0){
              fourKind( DFace, DSuit, DResult, pair );
	      //printf("\n  DResult[ 3 ] = %d\n", DResult[ 3 ] );
	      if(DResult[ 3 ] == 0){
                 threeKind( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 2 ] = %d\n", DResult[ 2 ] );
	         if(DResult[ 2 ] == 0){
                 twoPair( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 1 ] = %d\n", DResult[ 1 ] );
	            if(DResult[ 1 ] == 0){
                       aPair( DFace, DSuit, DResult, pair );
	               //printf("\n  DResult[ 0 ] = %d\n", DResult[ 0 ] );
	               if(DResult[ 0 ] == 0){
                     printf("Very weak!\n");
                  }else if(DResult[ 0 ] == 1){
                     evalDisplay( DFace, DSuit, pair, 0 );         
                  }
               }else if(DResult[ 1 ] == 1){
                  evalDisplay( DFace, DSuit, pair, 1 );        
               }
            }else if(DResult[ 2 ] == 1){
               evalDisplay( DFace, DSuit, pair, 2 );        
            }
         }else if(DResult[ 3 ] == 1){
            evalDisplay( DFace, DSuit, pair, 3 );        
         }
      }
   }
	
	//result
	final( PResult, DResult );
	
}

void draw( const int wDeck[][ 13 ], char *wDFace[], char *wDSuit[], const char *wStoreFace[], const char *wStoreSuit[], int wPair[] ){
   int i, j, zero = 0, card, row, column;
   
   //find how many zero
   for(i = 0 ; i < 5 ; i++){
      if(wPair[ i ] == 0)
         zero++;         
   }    
   if(zero == 5){
      zero = 2;
   }else{
      zero -= 1;
   }
   
   //debug
   //printf("zero = %d\n", zero); 
    
   
   //draw new card respect to number of zero
   for(card = 11; card <= (11 + zero); card++){
		for(row = 0; row < 4; row++){
			for(column = 0; column < 13; column++){
				if(wDeck[ row ][ column ] == card){
               //printf("%5d\n", card);             
               for(j = 0; j < 5; j++){
                  if(wPair[ j ] == 0){
                     wDSuit[ j ] = wStoreSuit[ row ];
                     wDFace[ j ] = wStoreFace[ column ];
                     wPair[ j ] = 1;
                     break;
                  }
               }
	         }
			}
		}
	}
	
	//debug
	/*for(i = 0; i < 5; i++){
		printf("%10s of %-8s\n", wDFace[ i ], wDSuit[ i ] );
	}*/
}

void initialDisplay( const char *wPFace[], const char *wPSuit[], const char *wDFace[], const char *wDSuit[] ){
   int i;
   
   printf("*************************\nYour five-card poker hand\n*************************\n\nPlayer hand %28s\n********** %28s\n\n", "Dealer hand", "***********");
	for(i = 0; i < 5; i++){
		printf("%10s of %-8s %10s of %-8s\n", wPFace[ i ], wPSuit[ i ], "*****", "*****" );
	}  
}

void evalDisplay( const char *wPDFace[], const char *wPDSuit[], const int wPair[], const int wType ){
   int card;
   char *type[ 4 ] = { "Pair", "Two-pair", "Three of a kind", "Four of a kind" };
   
   printf("\n%s...\n", type[ wType ] );
   for(card = 0; card < 5; card++){
      if(wPair[ card ] != 0){
         printf("\n%7s of %-9s\n", wPDFace[ card ], wPDSuit[ card ] );
      }
   }             
}

void aPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;

   //compute whether the five-hand card contains a pair
   for(card = 0; card < 5; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            j++;
            wPair[ card ] = 1;
            wPair[ i ] = 1;
  	 	   }
 	   }
   }
    
   //assign result
   if(j == 1){
      result[ 0 ] = 1;
   }    
}

void twoPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether the five-hand card contains two pair
   for(card = 0; card < 5; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            j++;
            wPair[ card ] = 1;
            wPair[ i ] = 1;
 	 	   }  
      }
   }
    
   //assign result
   if(j == 2){
      result[ 1 ] = 1;
   }  
}

void threeKind( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k, x;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether the five-hand card contains three of a kind
   for(card = 0; card < 3; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            for(x = i + 1; x < 5; x++){
               if(wPDFace[ i ] == wPDFace[ x ]){
                  j++;
                  wPair[ card ] = 1;
                  wPair[ i ] = 1;
                  wPair[ x ] = 1;          
               }      
            }
    	   }  
      }
   }
    
   //assign result
   if(j == 1){
      result[ 2 ] = 1;
   }          
}

void fourKind( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, indi = 0;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether your five-hand card contains four-of-a-kind
   for(card = 0; card < 2; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){  
            indi++;
            if(indi == 1)
               wPair[ card ] = 1;
            wPair[ i ] = 1;    
             
         }
      }         
      if(indi == 3)
         break;
      else if(indi > 3)
         break;
      else{
         indi = 0;
         for(i = 0; i < 5; i++)
            wPair[ i ] = 0;
      }
          
   }     
    
   //assign result
   if(indi == 3){
      result[ 3 ] = 1;
   } 
}

void flush( const char *wPDFace[], const char *wPDSuit[], int result[] ){
   int i, j = 0;
   
   //compute whether your five-hand card contains a flush
   for(i = 1; i < 5; i++){
      if(wPDSuit[ 0 ] == wPDSuit[ i ] )
         j++;
   }
   
   //display result
   if(j == 4){
      result[ 4 ] = 1;
      printf("\nFlush...\n");
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wPDFace[ i ], wPDSuit[ i ] );
      }
   }   
}

void straight( const char *wPDFace[], const char *wPDSuit[], const char *wStoreFace[], int result[] ){
   int i, j, k, pass, count, hold, check = 1;
   int faceValue[ 5 ] = { 0 };
   
   //locate face value and store in an array
   for(i = 0 ; i < 5; i++){
      for(j = 0 ; j < 13; j++){
         if(wPDFace[ i ] == wStoreFace[ j ]){
            faceValue[ i ] = j;           
         }      
      }
   }
   
   //sort face value in ascending order using bubble sort
   for(pass = 0; pass < 4; pass++){
      for(count = 0; count < 4; count++){
         if(faceValue[ count ] > faceValue[ count + 1 ]){
            //swap
            hold = faceValue[ count ];
            faceValue[ count ] = faceValue[ count + 1 ];
            faceValue[ count + 1 ] = hold;               
         }          
      }         
   }
   
   //check if the hand contains a straight
   for(i = 0; i < 4; i++){
      if(faceValue[ i ] + 1 != faceValue[ i + 1 ]){
         check = 0;              
      }      
   }
   
   //display result
   if(check == 1){
      result[ 5 ] = 1;
      printf("\nStraight...\n");
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wPDFace[ i ], wPDSuit[ i ] );
      }
   }    
}

void final( const int wPResult[], const int wDResult[] ){
   int i, x = 0, y = 0;
   
   printf("\n******\nResult\n******\n\n");
   
   //evaluate both hand
   for(i = 0; i < 6; i++){
      if(wPResult[ i ] == 1)
         x = i + 1;
      if(wDResult[ i ] == 1)
         y = i + 1;      
   }
   
   //debug
   //printf("x = %d  y = %5d\n\n", x, y);
   
   if(x > y)
      printf("Players win!\n\n");
   else if(y > x)
      printf("Dealers win!\n\n");
   else if(x == y)
      printf("Its a tie!\n\n");
   else
      printf("Unable to evaluate!\n\n");
}

3) Card Shuffling and Dealing: Five-card Poker Hand Evaluation

Deals two five-card poker hand, evaluates each hand, and determines which is the better hand.
/*
 *  Pointers 713 : Evaluate Five Hand Card
 *
 *  Created on: Feb 23, 2012
 *      Author: aeriqusyairi
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
//main functions
void shuffle( int wDeck[][ 13 ] ); 
void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[] );
void display( const char *wfFace[], const char *wfSuit[], const char *wsFace[], const char *wsSuit[] );

int main(){
   //initialize suit array
   const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    
   //initalize face array
   const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",
                               "Five", "Six", "Seven", "Eight",
                               "Nine", "Ten", "Jack", "Queen", "King" };
    
   //initialize deck array
   int deck[ 4 ][ 13 ] = { 0 };
    
   //seed random number generator
   srand(time(0));
    
   shuffle( deck );//shuffle the deck
   deal( deck, face, suit );//deal five-hand card
    
   system("pause");
   return 0;    
}
 
void shuffle( int wDeck[][ 13 ] ){
   int row, column, card;

 for(card = 1; card <= 52; card++){
  do{
   row = rand() % 4;
   column = rand() % 13;
  }while(wDeck[ row ][ column ] != 0);




  wDeck[ row ][ column ] = card;
 }
}

void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[] ){
   //operational functions
   void pair( const char *wfFace[], const char *wfSuit[], int result[] );
   void twoPair( const char *wfFace[], const char *wfSuit[], int result[] );
   void threeKind( const char *wfFace[], const char *wfSuit[], int result[] );
   void fourKind( const char *wfFace[], const char *wfSuit[], int result[] );
   void flush( const char *wfFace[], const char *wfSuit[], int result[] );
   void straight( const char *wfFace[], const char *wfSuit[], const char *wFace[], int result[] );
   void eval( const int firstResult[], const int secondResult[] );
   
 int card, row, column, i, j = 0;
 
 //storage for two five-hand card
 //result boolean: 0:pair 1:two pair 2:three of a kind 3:four of a kind 4:flush 5:straight
 //first
   char *fSuit[ 5 ] ;//= { "Spades", "Hearts", "Hearts", "Hearts", "Hearts" };
   char *fFace[ 5 ] ;//= { "Ace", "Ace", "Three", "Four", "Five" };
   int fResult[ 6 ] = { 0 };
   //second
   char *sSuit[ 5 ] ;//= { "Spades", "Hearts", "Hearts", "Hearts", "Hearts" };
   char *sFace[ 5 ] ;//= { "Ace", "Six", "Three", "Four", "Five" };
   int sResult[ 6 ] = { 0 };

 //deal 5-card poker hand 
 
 for(card = 1, i = 0; card <= 10; card++, i++){
  for(row = 0; row < 4; row++){
   for(column = 0; column < 13; column++){
    if(wDeck[ row ][ column ] == card && i < 5){
               fSuit[ i ] = wSuit[ row ];
               fFace[ i ] = wFace[ column ];
          }else if(wDeck[ row ][ column ] == card && i > 4){
               sSuit[ j ] = wSuit[ row ];
               sFace[ j ] = wFace[ column ];
               j++;      
            }
   }
  }
 }
 

 //display two five-card poker hand
 display( fFace, fSuit, sFace, sSuit );
 
 //evaluate
 printf("\n******************\nEvaluate both hand\n******************\n\nFirst hand\n**********\n\n");
 pair( fFace, fSuit, fResult );
 //printf("\n  fResult[ 0 ] = %d\n", fResult[ 0 ] );
 twoPair( fFace, fSuit, fResult );
 //printf("\n  fResult[ 1 ] = %d\n", fResult[ 1 ] );
 threeKind( fFace, fSuit, fResult );
 //printf("\n  fResult[ 2 ] = %d\n", fResult[ 2 ] );
 fourKind( fFace, fSuit, fResult );
 //printf("\n  fresult[ 3 ] = %d\n", fResult[ 3 ] );
 flush( fFace, fSuit, fResult );
 //printf("\n  fResult[ 4 ] = %d\n", fResult[ 4 ] );
   straight( fFace, fSuit, wFace, fResult ); 
   //printf("\n  fResult[ 5 ] = %d\n", fResult[ 5 ] );
 
 printf("\nSecond hand\n***********\n\n");
 pair( sFace, sSuit, sResult );
 printf("\n  sResult[ 0 ] = %d\n", sResult[ 0 ] );
 twoPair( sFace, sSuit, sResult );
 printf("\n  sResult[ 1 ] = %d\n", sResult[ 1 ] );
 threeKind( sFace, sSuit, sResult );
 printf("\n  sResult[ 2 ] = %d\n", sResult[ 2 ] );
 fourKind( sFace, sSuit, sResult );
 printf("\n  sResult[ 3 ] = %d\n", sResult[ 3 ] );
 flush( sFace, sSuit, sResult );
 printf("\n  sResult[ 4 ] = %d\n", sResult[ 4 ] );
 straight( sFace, sSuit, wFace, sResult ); 
   printf("\n  sResult[ 5 ] = %d\n", sResult[ 5 ] );
 
 //result
 eval( fResult, sResult );
 
}

void display( const char *wfFace[], const char *wfSuit[], const char *wsFace[], const char *wsSuit[] ){
   int i;
   
   printf("*************************\nYour five-card poker hand\n*************************\n\nFirst hand %28s\n********** %28s\n\n", "Second hand", "***********");
 for(i = 0; i < 5; i++){
  printf("%10s of %-8s %10s of %-8s\n", wfFace[ i ], wfSuit[ i ], wsFace[ i ], wsSuit[ i ] );
 }  
}

void pair( const char *wfFace[], const char *wfSuit[], int result[] ){
   int card, i, j = 0, k;
   int pair[ 5 ] = { 0 };

   //compute whether the five-hand card contains a pair
   printf("\nPair...\n");
   for(card = 0; card < 5; card++){
      for(i = card + 1; i < 5; i++){
         if(wfFace[ card ] == wfFace[ i ]){
            j++;
            pair[ card ] = i;
        }
     }
   }
    
   //display result
   if(j == 1){
      for(card = 0; card < 5; card++){
         if(pair[ card ] != 0){
            k = pair[ card ];
            printf("\n%10s of %-8s and %5s of %-8s\n", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
            result[ 0 ] = 1;
         }
      }
   }else{
      printf("\n  Your five-hand card doesn't contains a pair.\n");      
   }      
}

void twoPair( const char *wfFace[], const char *wfSuit[], int result[] ){
   int card, i, j = 0, k;
   int pair[ 5 ] = { 0 };
    
   //compute whether the five-hand card contains two pair
   printf("\nTwo-pair...\n");
   for(card = 0; card < 5; card++){
      for(i = card + 1; i < 5; i++){
         if(wfFace[ card ] == wfFace[ i ]){
            j++;
            pair[ card ] = i;
       }  
      }
   }
    
   //display result
   if(j == 2){
      result[ 1 ] = 1;
      for(card = 0; card < 5; card++){
         if(pair[ card ] != 0){
            k = pair[ card ];
            printf("\n%10s of %-8s and %5s of %-8s\n", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
         }
      }
   }else{
      printf("\n  Your five-hand card doesn't contains two pair.\n");      
   }    
}

void threeKind( const char *wfFace[], const char *wfSuit[], int result[] ){
   int card, i, j = 0, k, x;
   int pair[ 3 ] = { 0 };
    
   //compute whether the five-hand card contains three of a kind
   printf("\nThree of a kind...\n");
   for(card = 0; card < 3; card++){
      for(i = card + 1; i < 5; i++){
         if(wfFace[ card ] == wfFace[ i ]){
            for(x = i + 1; x < 5; x++){
               if(wfFace[ i ] == wfFace[ x ]){
                  j++;
                  pair[ 0 ] = card;
                  pair[ 1 ] = i;
                  pair[ 2 ] = x;          
               }      
            }
        }  
      }
   }
    
   //display result
   if(j == 1){
      result[ 2 ] = 1;
      for(card = 0; card < 3; card++){
         k = pair[ card ];
         printf("\n%10s of %-8s\n", wfFace[ k ], wfSuit[ k ] );
      }
   }else{
      printf("\n  Your five-hand card doesn't contains three of a kind.\n");      
   }           
}

void fourKind( const char *wfFace[], const char *wfSuit[], int result[] ){
   int card, i, indi = 0, pair[ 5 ] = { 0 };
    
   //compute whether your five-hand card contains four-of-a-kind
   printf("\nFour of a kind...\n");
   for(card = 0; card < 2; card++){
      for(i = card + 1; i < 5; i++){
         if(wfFace[ card ] == wfFace[ i ]){  
            indi++;
            if(indi == 1)
               pair[ i - 1 ] = 1;
            pair[ i ] = 1;    
             
         }
      }         
      if(indi == 3)
         break;
      else if(indi > 3)
         break;
      else{
         indi = 0;
         for(i = 0; i < 5; i++)
            pair[ i ] = 0;
      }
          
   }     
    
   //display result
   if(indi == 3){
      result[ 3 ] = 1;
      for(i = 0; i < 5; i++){
        if( pair[ i ] == 1)
           printf("\n%5s of %-9s\n", wfFace[ i ], wfSuit[ i ] ); 
      }      
      printf("\n"); 
   }else{
      printf("\n  Your five-hand card doesn't contains four-of-a-kind.\n");      
   }   
}

void flush( const char *wfFace[], const char *wfSuit[], int result[] ){
   int i, j = 0;
   
   //compute whether your five-hand card contains a flush
   printf("\nFlush...\n");
   for(i = 1; i < 5; i++){
      if(wfSuit[ 0 ] == wfSuit[ i ] )
         j++;
   }
   
   if(j == 4){
      result[ 4 ] = 1;
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wfFace[ i ], wfSuit[ i ] );
      }
   }else{
      printf("\n  Not flush!\n");   
   }     
}

void straight( const char *wfFace[], const char *wfSuit[], const char *wFace[], int result[] ){
   int i, j, k, pass, count, hold, check = 1;
   int faceValue[ 5 ] = { 0 };
   
   printf("\nStraight...\n");
   
   //locate face value and store in an array
   for(i = 0 ; i < 5; i++){
      for(j = 0 ; j < 13; j++){
         if(wfFace[ i ] == wFace[ j ]){
            faceValue[ i ] = j;           
         }      
      }
   }
   
   //sort face value in ascending order using bubble sort
   for(pass = 0; pass < 4; pass++){
      for(count = 0; count < 4; count++){
         if(faceValue[ count ] > faceValue[ count + 1 ]){
            //swap
            hold = faceValue[ count ];
            faceValue[ count ] = faceValue[ count + 1 ];
            faceValue[ count + 1 ] = hold;               
         }          
      }         
   }
   
   //check if the hand contains a straight
   for(i = 0; i < 4; i++){
      if(faceValue[ i ] + 1 != faceValue[ i + 1 ]){
         check = 0;              
      }      
   }
   
   if(check == 1){
      result[ 5 ] = 1;
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wfFace[ i ], wfSuit[ i ] );
      }
   }else{
      printf("\n  Not a straight hand.\n" );   
   }     
}




void eval( const int firstResult[], const int secondResult[] ){
   int i, x = 0, y = 0;
   
   printf("\n******\nResult\n******\n\n");
   
   //debug
   /*
   for(i = 0; i < 6; i++){
      int j = 0;
      if(firstResult[ i ] == 1 && secondResult[ i ] == 1){
         printf("%d = Its a tie!\n", i ); 
         j = 1;               
      }else if(firstResult[ i ] == 1){
         printf("%d = First hand is better.\n", i ); 
         j = 1;               
      }else if(secondResult[ i ] == 1){
         printf("%d = Second hand is better.\n", i );  
         j = 1;    
      }else
         printf("%d = Both hand's weak!\n", i );   
   }  
   printf("\n");  
   */
   
   //evaluate both hand
   for(i = 0; i < 6; i++){
      if(firstResult[ i ] == 1)
         x = i + 1;
      if( secondResult[ i ] == 1)
         y = i + 1;      
   }
   
   //debug
   printf("x = %d  y = %5d\n\n", x, y);
   
   if(x > y)
      printf("First hand win!\n\n");
   else if(y > x)
      printf("Second hand win!\n\n");
   else if(x == y)
      printf("Its a tie!\n\n");
   else
      printf("Unable to evaluate!\n\n");
}

2) Card Shuffling and Dealing: Basic Five-card Poker Hand

Deals a five-card poker hand and determine if the hand contains a pair, two pair, three of a kind, four of a kind, flush, and straight.
/*
 *  Pointers 712 : Five Hand Card
 *
 *  Created on: Feb 18, 2012
 *      Author: aeriqusyairi
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//main functions
void shuffle( int wDeck[][ 13 ] ); 
void deal( const int wDeck[][ 13 ], const char *wface[], const char *wSuit[], char *wfSuit[], char *wfFace[] );

//operation functions
void pair( const char *wfSuit[], const char *wfFace[]);
void twoPair( const char *wfSuit[], const char *wfFace[] );
void threeKind( const char *wfSuit[], const char *wfFace[] );
void fourKind( const char *wfSuit[], const char *wfFace[] );
void flush( const char *wfSuit[], const char *wfFace[] );
void straight( const char *wfSuit[], const char *wfFace[], const char *wFace[] );

//sub-operation functions
void result( const int compare, const int compareTo, const char *wfFace[], const char *wfSuit[], const char statement[] );

int main(void){
 //initilize suit array
 const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

 //initialize face array
 const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four",
                            "Five", "Six", "Seven", "Eight",
                            "Nine", "Ten", "Jack", "Queen", "King" };

    //initialize suit and face array for storing five-hand card
 char *fSuit[ 5 ] ;// "Hearts", "Hearts", "Hearts", "Hearts", "Hearts" };
 char *fFace[ 5 ] ;// "Seven", "Ace", "Ace", "Four", "Five" };

 //initialize deck array
 int deck[ 4 ][ 13 ] = { 0 };

 //seed random number generator
 srand( time( 0 ) );

 shuffle( deck );//shuffle the deck
 deal( deck, face, suit, fSuit, fFace );//deal five-hand card
 pair( fSuit, fFace );//compute for pair
 twoPair( fSuit, fFace );//compute for two-pair
 threeKind( fSuit, fFace );//compute for three of a kind
 fourKind( fSuit, fFace );//compute for four of the kind
 flush( fSuit, fFace );//compute for flush
 straight( fSuit, fFace, face );//compute for straight

   system("pause");
 return 0;
}

void shuffle( int wDeck[][ 13 ] ){
 int row, column, card;

 for(card = 1; card <= 52; card++){
  do{
   row = rand() % 4;
   column = rand() % 13;
  }while(wDeck[ row ][ column ] != 0);

  wDeck[ row ][ column ] = card;
 }
}

void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[], char *wfSuit[], char *wfFace[] ){
 int card, row, column, i;

 //deal 5-card poker hand
 for(card = 1, i = 0; card <= 5; card++, i++){
  for(row = 0; row < 4; row++){
   for(column = 0; column < 13; column++){
    if(wDeck[ row ][ column ] == card){
               wfSuit[ i ] = wSuit[ row ];
               wfFace[ i ] = wFace[ column ];
          }
   }
  }
 }

 //display 5-card poker hand
 printf("Your five-card poker hand...\n\n");
 for(i = 0; i < 5; i++){
  printf("%10s of %-8s\n", wfFace[ i ], wfSuit[ i ]);
 }
}

void pair(const char *wfSuit[], const char *wfFace[]){
    int card, i, j = 0, k;
    int pair[ 5 ] = { 0 };

    //compute whether the five-hand card contains a pair
    printf("\nPair...\n");
    for(card = 0; card < 5; card++){
       for(i = card + 1; i < 5; i++){
          if(wfFace[ card ] == wfFace[ i ]){
             j++;
             pair[ card ] = i;
        }
      }
    }
    
    //display result
    if(j == 1){
      for(card = 0; card < 5; card++){
         if(pair[ card ] != 0){
            k = pair[ card ];
            printf("\n%10s of %-8s and %5s of %-8s\n", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
         }
      }
   }else{
      printf("\n  Your five-hand card doesn't contains any pair.\n");      
   }  
}

void twoPair( const char *wfSuit[], const char *wfFace[] ){
    int card, i, j = 0, k;
    int pair[ 5 ] = { 0 };
    
    //compute whether the five-hand card contains two pair
    printf("\nTwo-pair...\n");
    for(card = 0; card < 5; card++){
        for(i = card + 1; i < 5; i++){
           if(wfFace[ card ] == wfFace[ i ]){
             j++;
             pair[ card ] = i;
         }  
        }
    }
    
    //display result
    if(j == 2){
       for(card = 0; card < 5; card++){
          if(pair[ card ] != 0){
             k = pair[ card ];
             printf("\n%10s of %-8s and %5s of %-8s\n", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
          }
       }
    }else{
       printf("\n  Your five-hand card doesn't contains two pair.\n");      
    }
}

void threeKind( const char *wfSuit[], const char *wfFace[] ){
    int card, i, j = 0, k, x;
    int pair[ 3 ] = { 0 };
    
    //compute whether the five-hand card contains three of a kind
    printf("\nThree of a kind...\n");
    for(card = 0; card < 3; card++){
        for(i = card + 1; i < 5; i++){
           if(wfFace[ card ] == wfFace[ i ]){
              for(x = i + 1; x < 5; x++){
                 if(wfFace[ i ] == wfFace[ x ]){
                    j++;
                    pair[ 0 ] = card;
                    pair[ 1 ] = i;
                    pair[ 2 ] = x;          
                 }      
              }
         }  
        }
    }
    
    //display result
    if(j == 1){
       for(card = 0; card < 3; card++){
             k = pair[ card ];
             printf("\n%10s of %-8s\n", wfFace[ k ], wfSuit[ k ] );
       }
    }else{
       printf("\n  Your five-hand card doesn't contains three of a kind.\n");      
    }      
}

void fourKind( const char *wfSuit[], const char *wfFace[] ){
    int card, i, indi = 0, pair[ 5 ] = { 0 };
    
    //compute whether your five-hand card contains four-of-a-kind
    printf("\nFour of a kind...\n");
    for(card = 0; card < 2; card++){
       for(i = card + 1; i < 5; i++){
          if(wfFace[ card ] == wfFace[ i ]){  
             indi++;
             if(indi == 1)
                pair[ i - 1 ] = 1;
             pair[ i ] = 1;    
             
          }
       }         
       if(indi == 3)
          break;
       else if(indi > 3)
          break;
       else{
          indi = 0;
          for(i = 0; i < 5; i++)
             pair[ i ] = 0;
       }
          
    }     
    
    //display result
    if(indi == 3){
       for(i = 0; i < 5; i++){
         if( pair[ i ] == 1)
            printf("\n%5s of %-9s\n", wfFace[ i ], wfSuit[ i ] ); 
       }      
       printf("\n"); 
    }else{
       printf("\n  Your five-hand card doesn't contains four-of-a-kind.\n");      
    }
}

void flush ( const char *wfSuit[], const char *wfFace[] ){
   int i, j = 0;
   
   //compute whether your five-hand card contains a flush
   printf("\nFlush...\n");
   for(i = 1; i < 5; i++){
      if(wfSuit[ 0 ] == wfSuit[ i ] )
         j++;
   }
   
   //display result
   result( j, 0, wfFace, wfSuit, "\n  Not flush!\n" );
}

void straight( const char *wfSuit[], const char *wfFace[], const char *wFace[] ){
   int i, j, k, pass, count, hold, check = 1;
   int faceValue[ 5 ] = { 0 };
   
   printf("\nStraight...\n");
   //locate face value and store in an array
   for(i = 0 ; i < 5; i++){
      for(j = 0 ; j < 13; j++){
         if(wfFace[ i ] == wFace[ j ]){
            faceValue[ i ] = j;           
         }      
      }
   }
   
   //sort face value in ascending order using bubble sort
   for(pass = 0; pass < 4; pass++){
      for(count = 0; count < 4; count++){
         if(faceValue[ count ] > faceValue[ count + 1 ]){
            //swap
            hold = faceValue[ count ];
            faceValue[ count ] = faceValue[ count + 1 ];
            faceValue[ count + 1 ] = hold;               
         }          
      }         
   }
   
   //check if the hand contains a straight
   for(i = 0; i < 4; i++){
      if(faceValue[ i ] + 1 != faceValue[ i + 1 ]){
         check = 0;              
      }      
   }
   
   //display result
   result( check, 1, wfFace, wfSuit, "\n  Not a straight hand.\n" );
      
   printf("\n");
}

//sub-operation functions
void result( const int compare, const int compareTo, const char *wfFace[], const char *wfSuit[], const char statement[] ){
   int i;
   
   if(compare == compareTo){
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wfFace[ i ], wfSuit[ i ] );
      }
   }else{
      printf("%s", statement );   
   }
}