hello members...........i need a help from someone to give me an idea of how can i write a code for cryptex

i have an assignment and i couldn't figure out how to start writing a code

i just need an idea of how the program will work in order to write the code

please reply to me as soon as u can before sunday 27th of december

the assignment is :

Question 1: This question is taken from the ACM programming competition.
Leonardo Da Vinci, the famous inventor and artist, was fond of cryptography and has
invented many devices and techniques to hide messages. One such invention is the
cryptex. A cryptex is a small device used to carry a secret message and is made of one
or more rings. Each ring has the 26 uppercase letters written in some random order. It
is by aligning these rings in one specific way that the secret message is revealed. The
secret message is made of two words, each of length N. The first word of the secret
message is called the unlocking word and the second is called the secret word. To
properly align the cryptex, you need to know the unlocking word. Once you have the
cryptex and the unlocking word, all you have to do is align the rings on the cryptex to
spell the unlocking word. The letters on the rings, though randomly ordered, are
arranged in such a way that when the cryptex is aligned to spell the unlocking word,
one of the other 25 strings would spell the secret word. To reveal the secret message
you’ll need to know at least one letter from the secret word.
Take for example the following cryptex made of five rings (each line constitutes a
ring:)
KFZLQMDWJUSHGCEIXRAOPNVTYB
IMWZPFJBKLTNOEQDHUXGVYASRC
FAMIETZORWPSQUNGLDYBKXHCVJ
XNAKVPICQHDFWEGBRTMLZOUSYJ
ZSYFDOWIJCAKPBTXLRUNGQMVHE
The unlocking word is “GREEN” and we know that the second letter of the secret
word is “P”. By aligning the rings to spell the unlocking word, the cryptex now looks
like this:
GCEIXRAOPNVTYBKFZLQMDWJUSH
RCIMWZPFJBKLTNOEQDHUXGVYAS
ETZORWPSQUNGLDYBKXHCVJFAMI
EGBRTMLZOUSYJXNAKVPICQHDFW
NGQMVHEZSYFDOWIJCAKPBTXLRU
The secret word is revealed by looking for the word whose second letter is ’P’.
Input Format
Your program will be tested on one or more test cases. The first line of the input
specifies a single integer D which represents the number of test cases. Each test case
is specified using N +1 lines. The first line of each test case has the following format:
N U S
N is a positive integer which is the number of rings. No cryptex will have more than
1000 rings. U is the unlocking word while S describes the secret word. S is made of N
characters, all underscore characters (’_’) except exactly one. For example, having S
equal to "_ P _ _ _" says that the second letter of the secret word is "P".
The remaining N lines specifies the N rings, one on each line. Each line is made of
(different) 26 uppercase letters.
Consecutive test cases are separated by a single blank line.
Output Format
For each test case, write on a separate line, the unlocking word, followed by the secret
word, separated by a single space.
Sample Input/Output

bye

Salem commented: Bye, you just dumped your assignment in a word doc, and made no effort. -3

Hi green_leav,

Who provides the test cases?

What I mean by this is, do you have to devise the rings and secret words yourself i.e

KFZLQMDWJUSHGCEIXRAOPNVTYB
IMWZPFJBKLTNOEQDHUXGVYASRC
FAMIETZORWPSQUNGLDYBKXHCVJ
XNAKVPICQHDFWEGBRTMLZOUSYJ
ZSYFDOWIJCAKPBTXLRUNGQMVHE

or do you have to devise the program which at runtime will take a set of keys during the test?

If you are to provide your own rings then the first step is to devise your unlocking word and then your secret word else

You would likely need a 2d array of char something like this:

``````void align(char s[][26])
{
....................
}
int main(int argc, char **argv)
{
int n;
std::cout<<"Enter number of Rings"<<std::endl;
std::cin>>n;
char s[n][26];
align(s);

................................

return 0;
}``````

This is very skeletal but I think should give ideas.

Feel free to tell me what you think.

dear GrubSchumi,

first of all the user will enter the number of test cases (2) then the number of rings (5) and next the unlocking word which is GREEN and finally the secret word _P___ but my problem is how can i let the program find the rest of the secret word lettes? and how can i get a help from the unlocking word?

dear GrubSchumi,

first of all the user will enter the number of test cases (2) then the number of rings (5) and next the unlocking word which is GREEN and finally the secret word _P___ but my problem is how can i let the program find the rest of the secret word lettes? and how can i get a help from the unlocking word?

It is likely that the unlocking word is a char array itself.

Use a loop to search the first sequence

e.g. GREEN being the unlocking word

``````char unlocking_word [] = "GREEN";

void align(char s[][26], char [] unlocking_word)
{}``````

then you have a loop that looks for the G in

KFZLQMDWJUSHGCEIXRAOPNVTYB
While looping saves the chars in green into another temp char array. Once finding G we then replace the Green chars in the original char with G and the proceeding black characters then concatenate the green characters from the temp char array back onto the original char array etc...

Sorry if that is not too clear. Again come back if you want further help.

well....yes i need a help in the last point first of all how can i access this comand
char unlocking_word [] = "GREEN"; ?
the user will enter GREEN and this is an array with empty number of components is it ok?
second thing is : how can i check if the user entered all the letters in uppercase not in lowercase?

third thing is : i want to use sorting in order to make the unlocking word letters at the begining of each ring but how? I didnt understand the way u write it how can i save the green chars in a temp then puting them?

i know that iam annoying u but i really have difficulties in c++ and this assignment must be done on sunday but could u write a simple example of this code in order to understand it more?

thank u for every thing

Hi green_leav

I have sort of a solution but I not going to give it all to you but rather guide you.
First let me suggest you use strings if you want to avoid the 2d char arrays at least directly seeing as you are new to c++;

Let us look at the example in your assignment description:

KFZLQMDWJUSHGCEIXRAOPNVTYB
IMWZPFJBKLTNOEQDHUXGVYASRC
FAMIETZORWPSQUNGLDYBKXHCVJ
XNAKVPICQHDFWEGBRTMLZOUSYJ
ZSYFDOWIJCAKPBTXLRUNGQMVHE

The Unlocking word is Green with the secret word being Apple right?

``````void align(std::string keys], std::string unlocking_word)
{
for(int a=0;a <= unlocking_word.length()-1;a++)
{
while(keys[a].at(i)!= unlocking_word.at(a))
{
}
}
int main()
{
int n;
std::cout<<"Enter number of keys"<<std::endl;
std::cin>>n; //=5
std::string keys[n]; //we declare an string array

//here are the keys from your assignment description.
s[0] = "KFZLQMDWJUSH[B]G[/B]CEIXRAOPNVTYB";
s[1] = "IMWZPFJBKLTNOEQDHUXGVYAS[B]R[/B]C";
s[2] = "FAMI[B]E[/B]TZORWPSQUNGLDYBKXHCVJ";
s[3] = "XNAKVPICQHDFW[B]E[/B]GBRTMLZOUSYJ";
s[4] = "ZSYFDOWIJCAKPBTXLRU[B]N[/B]GQMVHE";

std::string unlock_word = "GREEN"; //the unlocking word
[B]align(keys, unlock_word)[/B]; // we pass the keys and unlock word(GREEN) as //parameters to the method align
return 0;
}``````

In the function void align(std::string str[], std::string unlocking_word) we need start a for loop to loop once for each letter in the unlocking word GREEN which is 5 but c++ string are char arrays which start at 0. so we need to stop it one short of 5 because 0 is a valid position ant that is where G goes R goes to 1. E goes to 2 E goes to 3 and N goes to 4 \n goes to 5 this terminates the string e.g:
[0][1][2][3][4][\n]
G R E E N

inside this for loop we need a while loop to traverse the letters in red
KFZLQMDWJUSHGCEIXRAOPNVTYB
IMWZPFJBKLTNOEQDHUXGVYASRC
FAMIETZORWPSQUNGLDYBKXHCVJ
XNAKVPICQHDFWEGBRTMLZOUSYJ
ZSYFDOWIJCAKPBTXLRUNGQMVHE

During the while loop we save the red letters in a temp string object. We must keep a counter so when the loop breaks after having found the letter we are looking for it then erase the string from 0 to where the counter stopped e.g keys.erase(0, i) for the first key i = 12, so keys.erase(0, 12) for the second i = 24 so keys.erase(0, 24) etc...

After erasing enough of the string we then add the temp at the end of the new string.

The example code I have given you above is not far from complete, Work with it and if you need more help I am willing to help.

thank u very much and sorry if iam annoying u with my assignment

ill work it and see what is missing and will tell u the results

bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.