CSC1321 Programming Assignment: Arrays and Functions

  1. Split a List

    Given a list of integers, use the first integer as a split value and move the integers in the list in such a way that the integers smaller than the split value go to the left to the split value and the larger ones to the right and the split value sits in the "middle" of the list between the smaller and larger ones. For example:

    Before moving ...
    9, 6, 18, 10, 2, 4, 14, 5, 1, 15, 7, 21, 
    After moving ...
    5, 6, 7, 1, 2, 4, 9, 14, 10, 15, 18, 21, 
    
    
  2. Find the Largest and the Second Largest Values in a List

    Given a list of integers, write a function that finds the largest and second largest integers. Consider the function prototype below:

    void getTwoLargestIntegers(int arr[], int size, int & largest, int & secondLargest);
    
  3. (Extra Credit) 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.

    //Sample main() function
    
    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 
    
      strcpy(msg_arr, msg.c_str());  //Copy the original string message to the character array 
      strcpy(key_arr, key.c_str());  //Copy the key to the character array 
    
      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