please show me step by step and before you post please run it to make sure the p
ID: 3633795 • Letter: P
Question
please show me step by step and before you post please run it to make sure the program works. Thank YouInstructions
Create a program
Prompt the user for an input file name
Prompt the user for an unsigned short int value
Display the absolute and relative frequency of each character in the file
Display the per-character and total entropy for the input file
Encrypt the file using a Caesar Cipher, with a shift value as specified in (2), and write the encrypted test to the file "out.txt"
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.
Sample Output
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
Dear,
#include <stdio.h>
#include <math.h>
int main()
{
FILE *fp, *out;
char filename[20], ch;
unsigned short shift_value;
int count[256];
int total_chars=0, i, index;
double rel_frequency, entropy = 0.0;
printf("Program: Arrays ");
printf("Enter an input file name: ");
scanf("%s", filename);
printf("Enter an unsigned short int: ");
scanf("%u", &shift_value);
for(i = 0; i < 256; i++)
count[i] = 0;
fp = fopen(filename, "r");
out = fopen("out.txt","w");
printf("The input string is "");
ch = fgetc(fp);
while( ch != EOF)
{
index = ch;
count[index]++;
printf("%c", ch);
total_chars++;
if(ch >= 'a' && ch <= 'z')
ch = ((ch - 'a') + shift_value) % 26 + 'a';
else if(ch >= 'A' && ch <= 'Z')
ch = ((ch - 'A') + shift_value) % 26 + 'A';
fputc(ch,out);
ch = fgetc(fp);
}
printf("" ");
printf("The character frequencies for "%s" are: ", filename);
for(i=0; i<256; i++)
if(count[i] > 0)
{
rel_frequency = ((double)count[i])/total_chars;
printf(" '%c' occurs %d times for a relative frequency of %.2f", i, count[i], rel_frequency);
entropy += rel_frequency * log(rel_frequency);
}
entropy = entropy * -1.0;
printf(" The per character entropy is: %.3f", entropy);
printf(" The Total entropy is: %.3f", entropy * total_chars);
printf(" The file 'out.txt' has been written using a Caesar shift of %u ", shift_value);
fclose(fp);
fclose(out);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.