CSC1321 Programming Assignment: Arrays and Functions

  1. Write a Cipher

    Write a cipher that encodes a plain text message and decodes back to the original message. The cipher takes an input text message and a key (a sequence of numbers) and then arranges the characters of the message into a 2D grid. And then read off the characters from the grid column by column as an encoded message in the order specified by the column numbers in the key. Decoding is just a reverse process of the encoding above.
    Hint: To convert a digit character to its corresponding number, just use the coding value difference. For example: '5' - '0' gives a number of 5.

    //Sample main() function
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void print(char b[], int size);
    void encode(char m[], int msgSize, char key[], int gridWidth);
    void decode(char m[], int msgSize, char key[], int gridWidth);
    
    int main()
    {
        string msg;   //Original plain text message
        string key;   //A sequence of numbers used for the order of the columns of a grid to be read off. 
        int size;     //The number of characters in the original message
        int length;   //The length of the key for encoding 
    
        cout << "Enter a plain text message for encoding" << endl;
        getline(cin, msg);
    
        cout << "Enter a key for encoding: " << endl;
        cin >> key;
    
        cout << endl;
    
        size = msg.length();
        length = key.length();
    
        //If the length of the string message is not a multiple of the length of the key,
        //cut off some trailing characters of the message to make it so.
    
        msg = msg.substr(0, size / length * length);
        size = msg.length();
    
        //char msg_arr[size + 1];   //Create an array of characters for storing the original message 
        //char key_arr[length + 1];   //Create an array of digits for the key 
    
        char * msg_arr = new char[size + 1];   //Create an array of characters for storing the original message 
        char * key_arr = new char[length + 1];   //Create an array of digits for the key 
    
        strcpy_s(msg_arr, size+1, msg.c_str());  //Copy the original string message to the character array with no more than size + 1 elements
        strcpy_s(key_arr, length+1, key.c_str());  //Copy the key to the character array with no more than length + 1 elements
    
        cout << "Original plain text message: " << endl;
        print(msg_arr, size);
        print(key_arr, length);
    
        cout << endl;
        encode(msg_arr, size, key_arr, length);
        cout << "Encoded message: " << endl;
        print(msg_arr, size);
    
        cout << endl;
        decode(msg_arr, size, key_arr, length);
        cout << "Decoded message: " << endl;
        print(msg_arr, size);
    
        return 0;
    }
    
    //Sample output:
    
    Enter a plain text message for encoding
    YOURCELLPHONEHASBEENSTOLENDELETEDATAATONCE
    Enter a key for encoding: 
    31452
    
    Message looks like in a 2D grid:
    YOURC
    ELLPH
    ONEHA
    SBEEN
    STOLE
    NDELE
    TEDAT
    AATON
    
    Message looks like after transposition:
    ULEEOEDT
    YEOSSNTA
    RPHELLAO
    CHANEETN
    OLNBTDEA
    
    Encoded message: 
    ULEEOEDTYEOSSNTARPHELLAOCHANEETNOLNBTDEA
    
    Decoded message: 
    YOURCELLPHONEHASBEENSTOLENDELETEDATAATON
    
    
  2. Connect Four (Bonus)

    Connect four is a two-player board game in which the players alternately drop colored disks into a seven-column, six-row vertically suspended grid, as shown in this real game picture. The objective of the game is to connect four same-colored disks in a row, a col- umn, or a diagonal before your opponent can do likewise. The program prompts two players to drop a RED or YELLOW disk alternately. Whenever a disk is dropped, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:

  3. //Skeleton program for main function
    int main() 
    {
        char board[6][7];
    
        displayBoard(board);
    
        while (true) {
          // Prompt the first player
          dropADisc('R', board);
          
          displayBoard(board);
          if (isWon(board)) 
    	  {
            cout << "The red player won" << endl;
            break;
          }
          else if (isDraw(board)) 
    	  {
            cout << "No winner" << endl;
            break;
          }
    
          // Prompt the second player
          dropADisc('Y', board);
          displayBoard(board);
    
          if (isWon(board)) 
    	  {
            cout << "The yellow player won" << endl;
            break;
          }
          else if (isDraw(board)) 
    	  {
            cout << "No winner" << endl;
            break;
          }
        }
    }