C One method of encoding messages is known as the expanding

C++

One method of encoding messages is known as the \"expanding square code\". This method encodes message by placing the character of the message in an odd order square matrix row by row, and then retrieving them in a clockwise expanding square spiral from the center of the matrix. If the message is not exactly the right length to fill the matrix, the rest of the matrix is filled with asterisk characters (*).


For example, the two square matrices below show the order in which the characters are placed in the matrix, and the order in which they are retrieved. Notice that the order of retrieval begins at the center, then proceed to the right, and then spirals clockwise

Message In: \"This is a test message!!\"

Message Out: \"stssees a a*!!egmtiThis \"


Your program must be able to encode messages by this method. The input (from standard in) will consist of a string of text entered by the user, containing an unknown number of characters N (0 < N <= 1000). From the length of the message, you should determine the minimum odd order required for the matrix, and perform the specified operation. The output for your program will be the encoded message.

Deliverable

main.cpp

Solution

#include <iostream>

#include <string>

#include <vector>

using namespace esc;

int SizeMatrix(string input)

{

int arraylen = 0;

bool b = true;

for(int i=0; b; ++i)

{

if(input.length() > i*i)

++arraylen;

else

b = false;

}

if(arraylen % 2 == 0)

{

++arraylen;

}

return arraylen;

}

void Encode(string input, vector<vector<char>>& output, int size)

{

int posx = (size / 2)+1;

int posy = posx;

output[posx][posy] = input[0];

int counter = 1;

bool negate = false;

for(int i=1; i < size * size; ++i)

{

if(posx == size)

break;

if(negate)

{

for(int j=0; j < counter; ++j)

{

posx -= j;

if(i < input.size())

output[posx][posy] = input[i];

else

output[posx][posy] = \'*\';

}

for(int j=0; j < counter; ++j)

{

posy -= j;

if(i < input.size())

output[posx][posy] = input[i];

else

output[posx][posy] = \'*\';

}

}

else

{

for(int j=0; j < counter; ++j)

{

posx += j;

if(i < input.size())

output[posx][posy] = input[i];

else

output[posx][posy] = \'*\';

}

for(int j=0; j < counter; ++j)

{

posy += j;

if(i < input.size())

output[posx][posy] = input[i];

else

output[posx][posy] = \'*\';

}

}

if(negate == true)

negate = false;

else

negate = true;

++counter;

}

}

int main()

{

string original = \"lolwutlolwutlolwut?\";

int stringSize = SizeMatrix(original);

vector< vector<char> > rearranged;

rearranged.resize(stringSize);

for(int i=0; i < stringSize; ++i)

rearranged[i].resize(stringSize);

for(int i=0; i < stringSize; ++i)

{

for(int j=0; j < stringSize; ++j)

rearranged[i][j] = 0;

}

Encode(original, rearranged, stringSize);

system(\"PAUSE\");

}

C++ One method of encoding messages is known as the \
C++ One method of encoding messages is known as the \
C++ One method of encoding messages is known as the \
C++ One method of encoding messages is known as the \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site