ECE 8473 - Assignment #5 - Due: 27 Oct 2022


Upload source files - individual files or archive (e.g. zip, tar) - to your osp/a5 upload area.

Programs must compile with no warnings or errors using: gcc -std=c11 -pedantic -Wall

Shell scripts should run correctly using dash.

Each source file must start with a comment containing your name and a description.


Using arrays.

  1. [50] a5/p1.c - approximating math.h functions

    Each student has been randomly assigned one of the functions from math.h to approximate using a cubic polynomial over a specified range.

    The assigned function and range are listed in a comment at the top of your initial a5p1.c file in the Assignment Results.

    Write a main program which uses the provided llsq() function (llsq.h, llsq.c) to determine the linear least-square-error cubic polynomial coefficients and then print the function x,y values and polynomial estimates.

    The program output must consist of only the math function x,y values, double-spaced, followed by the polynomial estimates for each x value, single-spaced, so the results can easily be plotted for comparison.

    program outline, sample output, sample plot


  2. [25] a5/p2.c and [25] a5/p2.sh - substitution cipher

    First write a C program p2.c which produces a random permutation of the upper-case alphabetic characters A...Z.

    Sample runs:

      $ ./p2
      IWDJUMCFEBXPQGNZLAYRKVTOSH
      $ ./p2
      WXBRVYKNCIGHFLAUMSOEPTDJZQ
      $ ./p2
      YKZMRLJQNUFXBOAVIEDGSPCHWT
      $
    
    Use srand(time(0)) to initialize rand(), and starting with an array of characters "ABCDEFGHIJKLMNOPQRSTUVWXYZ", put the array into random order using the following algorithm with len=26:
      for j = len, len-1, ..., 2
      {
        i = random index from 0 to j-1
    
        swap array elements i and j-1
      }
    
    A random integer from 0 to n-1 is generated using either rand()%n or (int)(n*(rand()/(RAND_MAX+1.0)))

    Then write a shell script p2.sh which uses p2 to generate a random permutation, then uses tr to encrypt a random upper-case fortune using the permutation for letter substitution, then decrypts and tests that the decrypted result is correct.

    To create an upper-case random fortune input file use:

      fortune | cat -v | tr '[:lower:]' '[:upper:]' > plaintext
    
    Sample runs:
      $ dash ./p2.sh # with no bugs
      generating random fortune > plaintext
      encrypting > ciphertext
      decrypting > p2.tmp
      it worked, p2.tmp matches the plaintext!
      plaintext:
      ONE FAMILY BUILDS A WALL, TWO FAMILIES ENJOY IT.
      ciphertext:
      UJE RAQTXP BNTXIS A ZAXX, WZU RAQTXTES EJFUP TW.
      p2.tmp:
      ONE FAMILY BUILDS A WALL, TWO FAMILIES ENJOY IT.
      
      $ dash ./p2.sh # with bug somewhere
      generating random fortune > plaintext
      encrypting > ciphertext
      decrypting > p2.tmp
      it didn't work, p2.tmp does not match the plaintext
      plaintext:
      BIOLOGY IS THE ONLY SCIENCE IN WHICH MULTIPLICATION MEANS THE SAME THING
      AS DIVISION.
      ciphertext:
      ZSUNULY SA CEO UQNY AKSOQKO SQ RESKE XINCSBNSKFCSUQ XOFQA CEO AFXO CESQL
      FA MSGSASUQ.
      p2.tmp:
      PYEOEQS YR GIX EMOS RUYXMUX YM TIYUI KAOGYJOYUHGYEM KXHMR GIX RHKX GIYMQ
      HR FYNYRYEM.
    
    Manual example using tr:
      $ tr "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "IWDJUMCFEBXPQGNZLAYRKVTOSH" < plaintext > ciphertext
      $ cat ciphertext
      SNKA WUYR DNGYNPIRENG EY RFU FNZU RFIR RFU RFEGCY SNK MIEPUJ RN CUR TUAUG'R
      AUIPPS TNARF FIVEGC.
      $ tr "IWDJUMCFEBXPQGNZLAYRKVTOSH" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" < ciphertext
      YOUR BEST CONSOLATION IS THE HOPE THAT THE THINGS YOU FAILED TO GET WEREN'T
      REALLY WORTH HAVING.
      $