C++ Programming In this assignment you will write a program that encrypts a text
ID: 3594781 • Letter: C
Question
C++ Programming
In this assignment you will write a program that encrypts a text file. You will use the following encryption scheme.
Ask the user for the name of the original file.
Ask the user for the name of the output file.
Ask the user for the encryption key, n.
Read n2 characters from the file into the n rows and n columns of a 2-dimensional array.
Transpose the array. (Exchange the rows and columns.)
Write the characters from the array to an output file.
Repeat steps 4 – 6 until the input file has been read.
This same program can be used to decrypt a file that has been encrypted in this way.
Your program should have functions for performing the following tasks.
1) Reading the characters from the file into the array. This function should be passed the file stream, the array and the key.
2)Transposing the array. This function should be passed the array and the key.
3) Writing the characters from the array to the file. This function should be passed the file stream, the array and the key.
Your program should get the filenames, open the files and close the files in main.
Your program should also:
Give detailed directions and warnings to the user.
Be readable with appropriate documentation and formatting. (Be sure to include carefully written pre and post conditions for each of your functions.)
Explanation / Answer
PLEASE REFER BELOW CODE
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void read_file(std::ifstream& input_file,char **arr,int n)
{
//read file char by char
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
input_file >> arr[i][j];
}
}
}
void transpose_array(char **arr,int n)
{
//transpose 2-D array
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
arr[i][j] = arr[j][i];
}
}
}
void write_file(std::ofstream &output_file,char **arr, int n)
{
//writing array to file
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
output_file << arr[i][j];
}
}
}
int main()
{
ifstream input_file; //stream for input file
ofstream output_file; //stream for output file
string file_name; //filename
int n; //encryption key
char **arr; //array to store file
//reading file from user; if file not present it will repeatedly ask user until correct file name entered
do{
cout<<"Enter Name of original file : ";
cin>>file_name;
input_file.open(file_name.c_str());
}while(!input_file.is_open());
//taking output file name from user
cout<<"Enter name of the output file : ";
cin>>file_name;
//opening output file name
output_file.open(file_name.c_str());
//taking encryption key from user
cout<<"Enter encryption key : ";
cin>>n;
//creating 2-D array
arr = new char*[n];
for(int i = 0; i < n; i++)
arr[i] = new char[n];
//function to read file from user
read_file(input_file,arr,n);
//transpose an array
transpose_array(arr,n);
//writing array to file
write_file(output_file,arr,n);
//closing both input and output file
input_file.close();
output_file.close();
return 0;
}
samaple test.txt
hkashlajgddaskhdasdsadgdsjdjfhhsdhd
PLEASE REFER BELOW OUTPUT
Enter Name of original file : test.txt
Enter name of the output file : Chegg.txt
Enter encryption key : 6
Process returned 0 (0x0) execution time : 30.512 s
Press any key to continue.
Chegg.txt is generated as below
hasdshajksjsskhadddsadjhsjdjfdhsdhdu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.