In: Computer Science
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR A SINGLE WORD remember, read, understand, and use the waterpump.cpp program will process an arbitrary amount of input text, however long. Arrange a stream in a two dimensional array of characters by filling up the array a line at a time, from leftmost column to rightmost column. DO NOT USE A GETLINE: this program should be able to input an entire library, not a single line or word. Remember: we're building tools real people would use for real tasks. Remember, read, and use the "waterpump" design, that is why it was provided you. Then, to encypher the text, print out the array from top row to bottom row, leftmost column to rightmost colum. In Programmer Speak, we create a Row-Major character matrix, then print it as a Column-Major character matrix. Example: Here's a message: "meetmeinsaintlouis" It has 17 characters. We need a square array of char to hold it...but 17 isn't a square number. What's the next-largest square (bigger than 17?) 2 . 2 = 4 3 . 3 = 9 4 . 4 = 16 5 . 5 = 25 HAH! 25. (And what's the square root of 25? 5 ) So create char matrix[5][5] ; So write the message into a 5 x 5 square going left to right/top to bottom: meetm einsa intlo uisxx xxxxx (Note we padded out the extra cells with 'x'. In reality, we'd use random letters.) Print it out going top to bottom THEN left to right: STOP: BE SURE YOU UNDERSTAND THE PRECEEDING SENTENCE. CAN YOU SEE HOW TO DO IT IN YOUR MIND? If not, keep thinking about it. Here's the enciphered message: meiuxeinixentsxtslxxmaexx input: Karla: The young woman named Alexandra Borisovna Ostrakhova is your daughter. OUTPUT: KonasaoeqauanokurbrnmdvhrnhlgernodwcawdaavaldToABOaurahmlosigbreaertshbzynxirytmo Tools needed for this Assignment: std::string string::length() string::erase() string::[] If there is ANY EXTRANEOUS TEXT in the stream, (LIKE FROM EXTRA PROMPTS AND WARNINGS FROM YOUR PROGRAM) your code will only produce gibberish. We're here to build tools for serious workers to use. Please keep that goal uppermost in your mind, and never add anything unnecessary. Good machines are simple and solid. Build only good machines. All contents copyright (C) T.E.H., all rights reserved. None of this code may be displayed or stored on any public access or private access system without the written consent of the author. This means any and all commercial cheating ("Homework Helping/Counselling") services. Since we're desling with functions() in the text this week, let's break down the parts fo the program into functions: int side_length(int length) { // call side_length with the length of the message // it should return you an integer which is the // correct length of the square array of characters // to hold the message, plus any padding characters. } int randchar(void) { //return a random lower-case alphabetic character when called // note: (((rand() * some multiplier) % 26 )+ 'a') will produce such a character }
#include<conio.h>
#include<math.h>
#include<iostream>
using namespace std;
//random character generator function
int randchar()
{
return(((rand() * 67) % 26) + 'a');
}
//sidelength generator function
int side_length(int len)
{
int temp = sqrt(len);
if(pow(temp, 2) < len)
temp = temp + 1;
else
temp = temp;
return temp;
}
//main function
int main()
{
char s[100] ;//strore input string
char input[100][100];// temp string
gets_s(s);// store input string into s variable
int len = strlen(s);// calculate length of string
int temp = side_length(len);// take ow or column length of square
matrix
int i = 0, j = 0; int k = 0;
for (i = 0; i < len; i++)//remove blank from input string
{
if ((s[i] >= 65 && s[i] <= 90) || (s[i]
>= 97 && s[i] <= 122))
{
s[j++] = s[i];
}
}
//store input and padding in temp string
for (int i=0;i<temp;i++)
{
for (int j = 0; j < temp; j++)
{
if (k < len) {
input[i][j] = s[k];
k++;
}
else
input[i][j] = randchar();
}
}
//display output
for (int i = 0; i < temp; i++)
{
for (int j = 0; j < temp; j++)
cout << input[j][j];
}
_getch();
return 0;
}