Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hello
i am implementing C-36 cipher
C++
const int wheels = 5;
const int positions = 26;
int pin[positions][wheels]={{0,1,0,1,0},{1,1,1,1,1},{1,1,0,0,0},{0,0,1,1,0},{1,1,1,0,1},{1,1,0,1,0},{0,1,1,1,1},{1,0,0,0,0},{0,0,1,0,0},{1,1,1,1,0},{0,0,0,1,1},{1,1,0,1,0},{1,0,1,0,1},{0,1,0,1,1},{1,1,0,0,0},{1,0,1,1,1},{1,0,1,0,1},{0,1,1,0,2},{1,0,1,1,2},{0,0,0,2,2},{0,1,0,2,2},{0,1,2,2,2},{0,0,2,2,2},{1,2,2,2,2},{0,2,2,2,2}};

this array can simply be displayed by
C++
for(int i=0;i<25;i++)
{
     for(int j=0;j<5;j++)
     cout<<pin[i][j];
     cout<<endl;
}

what i want?
when pin[position][wheel]==2
then i need that the values of wheel reset to initial state i.e. state 0
0	1	0	1	0
1	1	1	1	1
1	1	0	0	0
0	0	1	1	0
1	1	1	0	1
1	1	0	1	0
0	1	1	1	1
1	0	0	0	0
0	0	1	0	0
1	1	1	1	0
0	0	0	1	1
1	1	0	1	0
1	0	1	0	1
0	1	0	1	1
1	1	0	0	0
1	0	1	1	1
1	0	1	0	1
0	1	1	0	0
1	0	1	1	1
0	0	0	1	0
0	1	0	1	0
0	1	0	0	1
0	0	1	1	0
1	1	0	0	1
0	1	1	1	0


best regards

[interpretation of the problem by Stefan_Lang]
pin represents an array of patterns. Each pattern is a sequence of 5 values, either 0 or 1. A pin value stored as the value 2, is in fact just a placeholder for a 0 or 1. The actual value a 2 stands for can be directly derived from the first and following patterns, but can only be easily derived if the whole sequence of patterns is interpreted sequentially:

When you iterate through the list of patterns, the first occurence of a 2 in a given position corresponds to the bit value of the first pattern in that same position. The second occurence corresponds to the bit value of the second pattern in that position, and so on.

In the example given above, the first 17 patterns do not contain a 2 and are used unchanged. Pattern 18 contains a 2 in the last position, and in fact all following patterns also have a 2 in that last position. For pattern 18, the last bit should assume the same value as the last bit of the first pattern. Pattern 19 then should use the last bit of the second pattern, etc.. Pattern 20 and the following ones all have a 2 at the second last position, so in pattern 20, that bit should assume the value of the corresponding bit in the first pattern, the next pattern then uses the corresponding bit from pattern 1, etc..

[Sherlock_Holmes_Mode]
The above interpretation raises some questions, such as what would happen if in one pattern the value at a specific position is stored as 2, but not in the following pattern? Or what would happen if the pattern that the bit should be taken from itself has a 2 in that position? Obviously it was assumed this would not happen, but why?

The use of the term wheel made me consider, although not stated in the question, that the series of bit values are actually meant to be repeated periodically. In other words, the bit values in columnn i actually represent values on a wheel with N[i] segments, where - according to the data in array pin - N[0]==25, N[1]==23, N[2]==21, N[3]==19, N[4]==17[. (Note that there are only 25 patterns defined, not 26 (is one missing?))

Based on these assumptions it won't be hard to provide a reasonable solution. There is only one open question, and that is whether or not we know the wheel sizes in advance, or have to derive them from the pin array by searching for the first occurence of a 2. Given no further information, it will have to be the latter.
[/Sherlock_Holmes_Mode]


[/interpretation of the problem by Stefan_Lang]
Posted
Updated 23-Feb-12 5:25am
v3
Comments
soorag2002 22-Feb-12 4:58am    
help me out i am stuck and waiting anxiously for some guidance
Stefan_Lang 22-Feb-12 5:15am    
Sorry. After formatting I had an easier time to read the explanations, but I still don't get the question. you have a 2D-array called pin, with element values in the range of 0, 1, or 2. You say you wish to reset the values of wheel, but there is no variable by that name, or you didn't explain what you refer to.

What is the result you wish to achieve?
krumia 22-Feb-12 5:27am    
Not clear. Do you want to replace the 2s with 0s?
Pablo Aliskevicius 22-Feb-12 5:48am    
cout<<pin[i][j] % 2;

Is this what you wanted? The question is not clear.

It sounds like you want

C++
for(int position=0; position<positions; ++position)
    for(int wheel=0; wheel<wheels; ++wheel) //hard-coding 5 and 25 was a big crime!
        if (pin[position][wheel]==2)
             pin[position][wheel] = 0;


It does not look reasonable if first place, but…
Also, it's not clear how could it be a problem.

—SA
 
Share this answer
 
v3
Given the interpretation I added above, something like this might do:
C++
const unsigned int positions = 25;
const unsigned int wheels = 5;
int pin[positions][wheels]; // add initialization array here

// determine the number of 'sections' for each wheel and store in the sizes array
void getWheelSizes(int array[positions][wheels], unsigned int sizes[wheels]) {
   for (unsigned int wheel = 0; wheel < wheels; ++wheel) {
      unsigned int n = 0;
      while (n < positions && array[n][wheel] < 2)
         ++n;
      sizes[wheel] = n;
   }
}
void print_position(unsigned int position, int array[positions][wheels], unsigned int sizes[wheels]) {
   for (unsigned int wheel = 0; wheel < wheels; ++wheel) {
      // calculate index with modulo operator: result is a value between 0 and sizes[wheel]-1
      unsigned int index = position % sizes[wheel];
      std::cout << array[index][wheel] << ' ';
   }
   std::cout << std::endl; // end of line
}
int main() {
   unsigned int sizes[wheels];
   getWheelSizes(pin, sizes);
   unsigned int loops = 123; // number of iterations doesn't matter, we can go on forever!
   for (unsigned int pos = 0; pos < loops; ++pos) { 
      print_position(pos, pin, sizes);
   }
   return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900