-
October 9th, 2014, 12:14 AM
#1
Storing multiple function calls in an array
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?
Function Code:
Code:
int[] MatrixMul(string subInput, int[,] key)
{
int[] result = new int[subInput.Length];
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]);
}
}
return result;
}
Calling Code:
Code:
for (int i = 0; i < outPut.Length; i++)
{
encChars = MatrixMul(outPut[i], key);
}
I really need some advice, so I can call my function and store all the values in this array.
-
October 9th, 2014, 06:02 AM
#2
Re: Storing multiple function calls in an array
what exactly are you trying to build do here...?
you realize that subInput.Length is actually the number of characters in the string itself
not a actual value so that if
subInput = "99";
in reality the length of it is 2;
if your intention is that you wish to convert that to a actual value
you should either tryparse the string
or
in fact pass a int to the function
also
your only looping the first dimension of that array if i remember right
either way key.GetLength(0) is being used for the row and col for loops,
which is probably not right
i have a hard time looking at [,] those arrays
i honestly never use anything but one dimensional arrays anymore
while that [,] feels confusing to me, something like this doesn't
Code:
// just a little food for thought
public class Int2D
{
int[] data;
public int Width{get; private set;}
public int Height{get; private set;}
public Int2D(int width,int height)
{
Width = width;
Height = height;
data = new array[Width*Height];
}
public int GetValueAt(int x , int y)
{
return data[x + y * Width];
}
public void SetValueAt(int x , int y, int value)
{
data[x + y * Width] = value;
}
}
Last edited by willmotil; October 9th, 2014 at 06:45 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|