Question

In: Computer Science

Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR...

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
}

Solutions

Expert Solution

#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;
}


Related Solutions

Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
Assignment in C programming class: read the text file line by line, and place each word,...
Assignment in C programming class: read the text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. Assume tgat you will use no more than 1000 words in the text, and no more than the first 10 characters in a...
Should all industries have to compete globally? This is a read only assignment for you to...
Should all industries have to compete globally? This is a read only assignment for you to review before posting your discussion in unit 4. When the first Japanese cars arrived on the West Coast in the 1970s, no one saw them as a threat to U.S. jobs. Although they were cheaper and more fuel-efficient than U.S.-made cars, most Americans could not be bothered; with gasoline at 30 cents a gallon, the difference in cost between a car that got 30...
C++ program to read line comments. This assignment will give you a little bit of practice...
C++ program to read line comments. This assignment will give you a little bit of practice with string parsing. Your task is to write a program that reads in a .cpp file line-by-line, and prints out only the text that's after the // characters that indicate a single-line comment. You do not have to worry about /* multiline comments */ though there will be a small amount of extra credit for programs that correctly extract these comments as well.
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
NOTE: This assignment is for the design only Nothing you turn in should look like C/C++...
NOTE: This assignment is for the design only Nothing you turn in should look like C/C++ code For this assignment, we are going to design a system to Manage loans from the local public library For this, we will need the following entities, plus collections for each of the entities: Patrons, Books, and Loans The data for a Book will contain at least the following: Author Title ISBN Number Library ID number Cost Current Status (In, Out, Repair, Lost) You...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT