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

The main function may only declare variables, call functions, and manage functio

ID: 3635227 • Letter: T

Question

The main function may only declare variables, call functions, and manage function execution using control structures.
Create one function to prompt the user, get the file name, and return an open file pointer
Create one function to accept a character and a shift and return the encrypted (shifted) version of the character
You may create and use any additional functions you require


Rewrite this Program.

#include <stdio.h>
#include <math.h>

int main()
{
FILE *fp, *out; /*file pointers for input and output*/
char filename[20], ch; /* filename*/
int shift_value = 0; /*shift value*/
/* array to store chatacter counts*/
int count[256], total_chars=0, i, index;
/*to calculate relative frequency and entropy*/
double rel_frequency, entropy = 0.0;

/* reads input filename */
printf("Program: Arrays ");
printf("Enter an input file name: ");
scanf("%s", filename);
/* reads shift value */
printf("Enter an unsigned short int: ");
scanf("%d", &shift_value);

/* initializing array values to zero */
for(i = 0; i < 256; i++)
count[i] = 0;

/* opens input and output files */
fp = fopen(filename, "r");
out = fopen("out.txt","w");

printf(" The input string is "");

/* reads each character from the file
until end of the file reached */
ch = fgetc(fp);
while( ch != EOF)
{
index = ch;
count[index]++;
printf("%c", ch);
total_chars++;

/* Caesar encoding */
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';

/* writes into the out.txt */
fputc(ch,out);
ch = fgetc(fp);
}
printf("" ");
printf(" The character frequencies for "%s" are: ", filename);

/* prints the character frequencies
and calculates entropy values */
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)/log(2.0));
}

/* displaying total entropy */
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 %d ", shift_value);

/* closing files */
fclose(fp);
fclose(out);

return 0;
}

Explanation / Answer

#include <stdio.h>

#include <math.h>

FILE* openFile(char filename[])

{

/* reads input filename */

printf("Enter an input file name: ");

scanf("%s", filename);

return fopen(filename, "r");

}

char shift(char ch, int shift_value)

{

/* Caesar encoding */

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';

return ch;

}

void displayStatistics(int count[], int total_chars)

{

/*to calculate relative frequency and entropy*/

double rel_frequency, entropy = 0.0;

int i;

/* prints the character frequencies

and calculates entropy values */

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)/log(2.0));

}

/* displaying total entropy */

entropy = entropy * -1.0;

printf(" The per character entropy is: %.3f", entropy);

printf(" The Total entropy is: %.3f", entropy * total_chars);

}

int main()

{

FILE *fp, *out; /*file pointers for input and output*/

char ch, filename[20];

int shift_value = 0; /*shift value*/

/* array to store chatacter counts*/

int count[256], total_chars=0, index, i;

printf("Program: Arrays ");

/* opens input and output files */

fp = openFile(filename);

out = fopen("out.txt","w");

/* reads shift value */

printf("Enter an unsigned short int: ");

scanf("%d", &shift_value);

/* initializing array values to zero */

for(i = 0; i < 256; i++)

count[i] = 0;

printf(" The input string is "");

/* reads each character from the file

until end of the file reached */

ch = fgetc(fp);

while( ch != EOF)

{

index = ch;

count[index]++;

printf("%c", ch);

total_chars++;

ch = shift(ch, shift_value);

/* writes into the out.txt */

fputc(ch,out);

ch = fgetc(fp);

}

printf("" ");

printf(" The character frequencies for "%s" are: ", filename);

displayStatistics(count, total_chars);

printf(" The file 'out.txt' has been written using a Caesar shift of %d ", shift_value);

/* closing files */

fclose(fp);

fclose(out);

return 0;

}