Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a program called arrays.c that does the following: The absolute frequency

ID: 3540721 • Letter: C

Question

Create a program called arrays.c that does the following:

The absolute frequency of a character is the number of times the character appears.  For example, in the string "aaab" the absolute frequency of 'a' is 3, and the absolute frequency of 'b' is 1.

The relative frequency of a character is the absolute frequency divided by the total number of characters.  For example, in the string "aaab" the relative frequency of 'a' is 3/4 = 0.75, and the relative frequency of 'b' is 1/4 = 0.25.  Relative frequency is always a value between 0 and 1, and the sum of all of the relative frequencies should equal 1.0

The sub-entropy  of a character is the relative frequency multiplied by the log of the relative frequency.  For example, in the string "aaab" the sub-entropy of 'a' is .75*log2(.75), and the sub-entropy of 'b' is .25*log2(.25).  The base of the log does not really matter, but by convention base 2 logs are used.

The per-character entropy of a string is the sum of all sub-entropy values multiplied by -1.  For example, in the string "aaab" the per-character entropy is (.75*log2(.75) + .25*log2(.25)) * -1.0 = (-.311 + -.500) * -1.0 = .811

The total entropy of a string is the per-character entropy multiplied by the length of the string.  For example, in the string "aaab" the total entropy is .811 * 4 = 3.245

Entropy is a fundamental concept in Computer Science and Engineering, and represents the "information content" of a string.  In the example "aaab" the per-character calculation says that each character is only about half used, and the total entropy calculation says that the 4-character string contains only about 3.2 characters worth of information.  In other words, if we wanted to compress the string (zip it) we could remove perhaps 1 unnecessary character.

A Caesar Cipher is a classic encryption technique where one letter is substituted for another.  The "shift value" specifies how the substitution is made.  Consider the alphabet: ABCDEFGHIJKLMNOPQRSTUVQXYZ.  In C terms, A is at position 0, and Z is at position 25.  To encrypt a character you merely add the shift value to the position.  For example, if the shift is 3, then A becomes D (since A is at position 0 and D is at position 3), M becomes P (since M is at position 12 and P is at position 15), and Z becomes C.  Mathematically, if p is the position of the letter, and s is the shift, then (p + s) % 26 is the position of the encrypted version of the letter.

Note that in this exam there are really 256 characters (one for each ASCII code), but you should only shift letters (a - z).  Upper-case should be shifted to upper-case, and lower-case should be shifted to lower-case.

Your program must adhere to the class coding standards, and will be graded using the assignment grading criteria.  Submit your source code file to the Desire2Learn dropbox for this assignment.

Program: Arrays
Enter an input file name: input.txt
Enter an unsigned short int: 5
The input string is "aaab"
The character frequencies for "input.txt" are:
'a' occurs 3 times for a relative frequency of .75
'b' occurs 1 times for a relative frequency of .25
The per-character entropy is .811
The total entropy is 3.245
The file 'out.txt' has been written using a Caesar shift of 5

[done]

Explanation / Answer

#include<math.h>
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
using namespace std;
main()
{
      char input[100];
      unsigned int shift;
      int *frequency;char *freq_index;float *rel_freq;
      printf("Program: Arrays Enter an input file name:");
      gets(input);
      FILE * pFile;
      long lSize;
      char * buffer;
      size_t result;
      int i,j,k=0,flag,freq_size=0;float pcent=0;
      pFile = fopen (input, "rb" );
      if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
      fseek (pFile , 0 , SEEK_END);// obtain file size:
      lSize = ftell (pFile);
      rewind (pFile);
      buffer = (char*) malloc (sizeof(char)*(lSize+1));// allocate memory to contain the whole file:
      buffer[lSize]='';
      if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
      result = fread (buffer,1,lSize,pFile);// copy the file into the buffer:
      if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
      fclose (pFile);
      printf("Enter an unsigned short int:");
      scanf("%u",&shift);
      printf("The input string is "%s" ",buffer);
      printf("The character frequencies for "%s" are: ",input);
      for(i=32;i<126;i++)//finding no of distinct character
      {
              flag=0;
              for(j=0;j<lSize;j++)
              {
              if(buffer[j]==i)
              flag=1;
              }
              if(flag==1)
              freq_size++;
      }
      frequency= new int[freq_size];
      freq_index= new char[freq_size];
      rel_freq= new float[freq_size];
      for(i=32;i<126;i++)//calculation of frequency
      {
              flag=0;
              for(j=0;j<lSize;j++)
              {
              if(buffer[j]==i)
              flag++;
              }
              if(flag>0)
              {
              freq_index[k]=i;
              frequency[k]=flag;
              k++;
              }
            
      }
      for(i=0;i<freq_size;i++)
      {
              rel_freq[i]=(float)frequency[i]/result;
              printf("'%c' occurs %d times with a relative frequency of %f ",freq_index[i],frequency[i],rel_freq[i]);
              pcent+=(-1)*rel_freq[i]*log(rel_freq[i])/log(2);
      }
      printf("The per-character entropy is:%f ",pcent);
      printf("The total entropy is:%f ",pcent*result);
      //ceaser cipher
      for(i=0;i<result;i++)
      {
               if(buffer[i]>=97 && buffer[i]<=122)//if character is lowercase
               {
               if((buffer[i]+shift)<=122)
               buffer[i]=buffer[i]+shift;
               else
               buffer[i]=buffer[i]+shift-26;
               }
               if(buffer[i]>=65 && buffer[i]<=90)//if character is uppercase
               {
               if((buffer[i]+shift)<=90)
               buffer[i]=buffer[i]+shift;
               else
               buffer[i]=buffer[i]+shift-26;
               }
      }
      //write to out.txt
      pFile = fopen ("out.txt", "wb");
      fwrite (buffer ,1,result, pFile);
      fclose (pFile);
      printf("The file 'out.txt' has been written using a Caesar shift of %u [done]",shift);     
      delete frequency;
      delete freq_index;
      delete rel_freq;
}
    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote