Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My function returns an int array with 2 values Let's say, res[0] and res[1]

When I'm calling the array in a for loop for multiple times, and when I'm storing the results in another array, in each iteration the results are over-written with the new results.

If the first call returns [0,1] the array will store [0,1] at index [0] and [1], which is fine, but when I'm calling the function again, the new results are stored at the same indexes [0] and [1] How can I avoid that?

The code is:

C#
    class Hill_Cipher
    {
        
        string AtoZ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        
 
        public string Hill_Cipher_Enc(string input, int[,] key)
        {
            int[] encChars  = new int[input.Length];
            string res = "";
            //Input to uppercase 
            input = input.ToUpper();
            //Remove Spaces
            input = input.Replace(" ", String.Empty);
            StringBuilder sb = new StringBuilder(input);
            //Make input dividable by the key length by adding x if necessary
            while (sb.Length % key.GetLength(0) != 0)
            {
                sb = sb.Append('X');
            }

            input = sb.ToString();

            //Dividing the input into chunks of the key length
            IEnumerable<string> chunks = Enumerable.Range(0, input.Length / key.GetLength(0))
    .Select(x => input.Substring(x * key.GetLength(0), key.GetLength(0)));
            string[] outPut = chunks.ToArray();

            //for every chunk, apply multiplication
            for (int i = 0; i < outPut.Length; i++)
            {
                if (i == 0)
                {
                    MatrixMul(outPut[i], key, encChars);
                }
                else
                MatrixMul(outPut[i], key,encChars);

            }

            for (int i = 0; i < encChars.Length; i++)
            {
                encChars[i] = encChars[i] % 26;
                
            }         

            return res;
        }

        //Matrices Multiplication
        private void MatrixMul(string subInput, int[,] key, int [] result)
        {
            for (int row = 0; row < key.GetLength(0); row++)
            {
                for (int col = 0; col < key.GetLength(0); col++)
                {
                   result[row]+= key[row, col] * AtoZ.IndexOf(subInput[col]);   
                }
            }
        }
}


For example, my outPut contains the following: "TH","IS","AT" when I'm calling the function with the first element of array "TH", it converts "T" to its equivalent number and apply some calculations and same with "H". Let's say the final answer is 20 for "T" and 30 for "H". The problem is that every time, encChars will store the values at index 0 and 1: encChars[0]=20 encChars[1]=30 When I call the function again it will store the new values at 0 and 1.... That's because I'm not changing the index value for encChars on each call, so how do I do that?
Posted
Updated 9-Oct-14 5:57am
v2
Comments
Maciej Los 9-Oct-14 8:55am    
Why are you doing it?
Wael Tayara 9-Oct-14 9:31am    
Matrix Multiplication, part of Hill Cipher algorithm

1 solution

Have a look here: Classical Encryption Techniques[^]. There you'll find Hll Cipher algorithm implementation. It might help to solve your issue.

MatrixMul function expects result variable as an array of integer. Why don't you use it as an output?
C#
private void MatrixMul(string subInput, int[,] key, out int [] result)

Now, you're able to add retruned values to the destination array.

Please, see:
Passing Arrays as Arguments (C# Programming Guide)[^]
Passing Arrays Using ref and out (C# Programming Guide)[^]

Got it?
 
Share this answer
 
v4
Comments
Wael Tayara 9-Oct-14 10:55am    
Thank you, I know that Thread, but I need to do it by myself :)
Any other tips?
Maciej Los 9-Oct-14 10:56am    
See updated answer ;)
Wael Tayara 9-Oct-14 11:02am    
Could you please explain more about the out parameter?
I have to assign it to a value before using it, and I'm really lost here
Maciej Los 9-Oct-14 11:05am    
Follow the links ;)

Straight from the head:

static void MatrixMul(int first, int second, out int[] result)
{
result = new int[2];

//later
result[0] = first;
result[1] = second;
}

static void Main(string[] args)
{

int []tempArray;
int [,]destArray=new int[2,10];

for (int i = 0; i < 9; i++)
{
MatrixMul(i, (int)i/2, out tempArray);
destArray[0, i] = tempArray[0];
destArray[1, i] = tempArray[1];
}

for (int i = 0; i < 9; i++)
{
Console.WriteLine("Step {0} -values: {1} {2}", i, destArray[0, i], destArray[1, i]);
}

Console.ReadKey();
}
Wael Tayara 9-Oct-14 11:55am    
Thank you so much, the problem is that result could be 2,3,4,5,6 or even 7 and it's not a fixed array, that's where I'm having a hard time :/
if my string is "test" then the function will take for example:
"te" and apply multiplication and return them to an array, then when I want to apply the function on "st" again, the results will overwrite the previous ones.
Every time I run the method/function, the array stores the values at the beginning of the array and I can see that clearly, but I'm not able to find a way that satisfies what I need...

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