How can I fill a 2d array recursively? - c#

I have a string filled with 9 numbers. I want to fill a 3x3 array with the numbers. I've managed to do it using a foreach and 2 for loops, but I feel this is quite messy. How can I modify my code to recursively enter the values into the array? This is my code currently:
int[,] matrix = new int[3, 3];
if(key.Length < 9 || key.Length > 9)
{
keyfield.GetComponent<InputField>().text = " Key Not Valid";
}
else
{
foreach(char c in key)
{
for (int k = 0; k < 3; k++)
{
for (int j = 0; j < 3; j++)
{
matrix[j, k] = c - 0;
}
}
}
}
Note, I'm working with Unity.

Well, just iterate over the chars in your key and assign them to the array, one field at a time. It's just a matter of calculating the correct columns and rows:
for (int i = 0; i < 9; ++i)
{
int row = i / 3;
int col = i % 3;
matrix[row, col] = (int)key[i];
}
Also note that neither the code in your question nor the code in my answer solves the problem in a recursive way. Recursion is given when a method calls itself directly or indirectly, which is not required to solve this particular problem.

Related

How to populate 2D array with char array in C#

I'm trying to fill a 2D char array with characters form a char array
I have tried this code but not finding the problem
public void FillArray(char[,] array, char[] values)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
for (int k = 0; k < values.Length; k++)
{
array[i, j] = values[k];
}
}
}
}
Your first for loop is responsible for iteration over first dimension of array (i) It's okay.
Your second for loop is responsible for iteration over second dimension of array (j). It's okay.
Your third loop is responsible for iteration over char values array (k) Here's your bug.
For a given set of values of i and j which represents dimensions indexes of array, your function iterates through all positions of values array. So for each k value i and j values remain unchanged. Therefore you sequentially put all the values of values array (k+1)times into the same cell of two dimension array, ultimately leaving it with value of values[values.Length] as it is the highest possible value of k in the most nested loop.
I'd suggest solution similar to what #adv12 has proposed with slight modification as I am not sure if the k value would be 0 during first iteration of the nested for loop. It is also more readable IMO.
int k = 0;
public void FillArray(char[,] array, char[] values)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = values[k];
k++
if (k >= values.Length)
{
k = 0;
}
}
}
}
Here's my best guess at what you want based on your comments:
int k = 0;
public void FillArray(char[,] array, char[] values)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = values[k++];
if (k >= values.Length)
{
k = 0;
}
}
}
}

Simple Program Taking Extremely Long Time To Finish [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i am trying to create minesweeper in c# to do this i create an multidimensional array and then randomly pick positions using
Random rand = new Random;
the problem comes when trying to run the code, when i run it it takes an extremely long time to finish, i suspect it is because of the while loop and the if statements, if the rand.Next keeps getting the same values it could cause the loop to run for a long time. is there something im doing that is afftecting the pseudo randomness of my numbers. code below.
after returning to this questing a while later in an attempt to make it less horrendous it is clear that the reason for the program taking so long to run lies within the
while (counter < numberOfMines)
{
if (Minefield[randomNumber1, randomNumber2] == 9)
{
counter--;
}
else
{
Minefield[randomNumber1, randomNumber2] = 9;
counter++;
}
randomNumber1 = (rand.Next(4)) + 1;
randomNumber2 = (rand.Next(4)) + 1;
}
section of the code. The reason that it was taking so long is that i was treating the while loop like a for loop and taking away from the counter when it was failing to place a mine. this is incorrect and i should have just not added to it.
using System;
class MainClass
{
static void Main(string[] args)
{
int boardSize = 5;
int numberOfMines = 25;
GenerateMinefield(boardSize,numberOfMines);
Console.ReadKey();
}
static int[,] GenerateMinefield(int boardSize, int numberOfMines)
{
int[,] Minefield = new int[boardSize + 2, boardSize + 2];
for (int i = 0; i <= boardSize; i++)
{
for (int j = 0; j <= boardSize; j++)
{
Minefield[j, i] = 0;
}
}
Random rand = new Random();
int randomNumber1 = (rand.Next(4)) + 1;
int randomNumber2 = (rand.Next(4)) + 1;
int counter = 0;
while (counter < numberOfMines)
{
if (Minefield[randomNumber1, randomNumber2] == 9)
{
counter--;
}
else
{
Minefield[randomNumber1, randomNumber2] = 9;
counter++;
}
randomNumber1 = (rand.Next(4)) + 1;
randomNumber2 = (rand.Next(4)) + 1;
}
for (int i = 0; i <= boardSize; i++)
{
for (int j = 0; j <= boardSize; j++)
{
Console.Write(Minefield[j, i]);
}
Console.WriteLine(Minefield[boardSize, i]);
}
return Minefield;
}
}
Looks like your problem is in the counter--. You're not removing a mine, you're simply failing to place a mine, so counter should be unchanged.
If I understand correctly the while loop should look something like this:
while (counter < numberOfMines)
{
if (Minefield[randomNumber1, randomNumber2] != 9)
{
Minefield[randomNumber1, randomNumber2] = 9;
counter++;
}
randomNumber1 = (rand.Next(4)) + 1;
randomNumber2 = (rand.Next(4)) + 1;
}
It looks like you're trying to do the following:
Create a minefield board of size (N+2, N+2)
Fill the interior of the board (excluding the cells on the border) randomly with a specified number of mines.
It's possible to do this in linear time (i.e. O(N)) as follows:
Create a 1D array of bools with the same number of elements as the interior of the minefield, excluding the border cells.
Fill the start of array with a number of true values equal to the number of mines that you want.
Shuffle the array - now the "mines" are in random locations.
Go through the array and for every true value set the corresponding value in the minefield to 9.
It's slightly inefficient to create a temporary array almost the same size as the minefield, but this has the advantage that it will always run in linear time.
Putting this together:
static int[,] GenerateMinefield(int boardSize, int numberOfMines, Random rng)
{
if (numberOfMines > boardSize)
throw new InvalidOperationException($"{nameof(numberOfMines)} exceeds {nameof(boardSize)}");
int[,] minefield = new int[boardSize + 2, boardSize + 2]; // Already filled with zeros.
bool[] mines = new bool[(boardSize-2)*2];
for (int i = 0; i < numberOfMines; ++i) // Set the first 'numberOfMines' to 9.
mines[i] = true;
Shuffle(mines, rng); // Now the mines are in random locations.
for (int i = 1, n = 0; i < boardSize; i++)
{
for (int j = 1; j < boardSize; j++, n++)
{
if (mines[n])
minefield[j, i] = 9;
}
}
return minefield;
}
static void Shuffle<T>(IList<T> array, Random rng)
{
for (int n = array.Count; n > 1;)
{
int k = rng.Next(n);
--n;
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}
(That's using a standard shuffle algorithm.)
There's a better but more subtle way of doing this that doesn't require a separate array of bools. You can use the following code:
static int[,] GenerateMinefield(int boardSize, int numberOfMines, Random rng)
{
if (numberOfMines > boardSize)
throw new InvalidOperationException($"{nameof(numberOfMines)} exceeds {nameof(boardSize)}");
int[,] minefield = new int[boardSize + 2, boardSize + 2]; // Already filled with zeros.
int available = boardSize*2;
for (int i = 1, n = 0; i < boardSize; i++)
{
for (int j = 1; j < boardSize; j++, n++)
{
if (rng.NextDouble() < numberOfMines / (double)available)
{
minefield[j, i] = 9;
--numberOfMines;
}
}
}
return minefield;
}
The drawback of this approach is that it is not obvious how it works! It's also not completely evenly distributed, but I suspect that wouldn't matter for this program. See here for further discussion.

C# Splitting a multidimensional array

So I have been searching for an answer for this for about an hour now and I was unable to find one so I'm going to try my luck here.
The problem:
I have a 3 dimensional Array that contains Containers. This array is for an algoritm for placing containers on a cargoship. The array consists of Length, width and height. I am trying to split the array at the middle of the width.
The only solution I have been able to sort of make work is making 2, 3 dimensional arrays and then using 6 for loops to copy the big array:
Container[,,] leftSideOfShip = new Container[ship.length, ((ship.width) / 2), ship.height];
Container[,,] rightSideOfShip = new Container[ship.length, ((ship.width) / 2), ship.height];
for(int a = 0; a < ship.length; a++)
{
for(int b = 0; b < ship.width/2; b++)
{
for(int c = 0; c < ship.height; c++)
{
if(ship.position[a,b,c] != null)
{
leftSideOfShip[a, b, c] = ship.position[a, b, c];
}
}
}
}
for (int d = 0; d < ship.length; d++)
{
for (int e = ship.width/2; e < ship.width; e++)
{
for (int f = 0; f < ship.height; f++)
{
if(ship.position[d,e,f] != null)
{
rightSideOfShip[d, e, f] = ship.position[d, e, f];
}
}
}
}
The 3 for loops for the leftSideOfShip work as expected but the second one gives an indexOutOfRangeException.
Is there a better way to split this and if so how?
First of all, you need to be mindful of how you round side width in case the width of the ship is odd. For example, you can always assume, that the left side is wider in case the width of the ship is odd, something like that:
int leftSideWidth = ship.width % 2 == 0 ? ship.width / 2 : (ship.width / 2) + 1;
int rightSideWidth = ship.width / 2;
Second of all, you have an error in your second loop block:
since you are looping for (int e = ship.width/2; e < ship.width; e++) e is greater than second dimension of rightSideOfShip array.
Full solution will look something like this:
Container[,,] leftSideOfShip = new Container[ship.length, leftSideWidth, ship.height];
Container[,,] rightSideOfShip = new Container[ship.length, rightSideWidth, ship.height];
for (int i = 0; i < ship.length; i++)
{
for (int j = 0; j < ship.width; j++)
{
for (int k = 0; k < ship.height; k++)
{
if (j < leftSideWidth)
{
leftSideOfShip[i, j, k] = ship.position[i, j, k];
}
else
{
rightSideOfShip[i, j - leftSideWidth, k] = ship.position[i, j, k];
}
}
}
}

How to eliminate duplicates in 2D arrays in C#

just started to learn programming and I need 2D array without duplicates. This code (well edited for 1D) worked just fine for 1D but doesn't for 2D and have no clue why.
Would be very happy if someone helped me. Thanks.
Random r = new Random();
int[,] array = new int[10, 8];
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array[i, j] = r.Next(10, 100);
for (int k = 0; k < i; k++)
{
for (int l = 0; l < j; l++)
{
if (array[i,j] == array[k,l])
{
i--;
j--;
break;
}
}
}
}
}
With the nested j loop you are filling the entire second dimension for each i, but in the k and l loops you are only checking the grid to the upper and left of current cell. You could place a number twice because you are not checking every previously filled location.
If we change the code to this:
for (int k = 0; k < array.GetLength(0); k++)
{
for (int l = 0; l < array.GetLength(1); l++)
{
if (i != k && j != l && array[i, j] == array[k, l])
{
i--;
j--;
break;
}
}
}
Then you eliminate that problem, but you very quickly find that you get a IndexOutOfRangeException because you're decrementing both i & j at the same time. That's not moving you to the previous value - it's jumping back a whole row and left one cell - and that's ultimately sending i or j to -1 and that's not good.
If you want to do it like you're attempting then you need to have a way to simply move back to the previously filled cell, regardless of the row or column you're on.
Try this:
for (int x = 0; x < array.GetLength(0) * array.GetLength(1); x++)
{
array[x % array.GetLength(0), x / array.GetLength(0)] = r.Next(10, 100);
for (int y = 0; y < x; y++)
{
if (array[x % array.GetLength(0), x / array.GetLength(0)] == array[y % array.GetLength(0), y / array.GetLength(0)])
{
x--;
break;
};
}
}
However, that's not very efficient. Try this instead:
var values = Enumerable.Range(10, 90).OrderBy(_ => r.Next()).ToArray();
for (int x = 0; x < array.GetLength(0) * array.GetLength(1); x++)
{
array[x % array.GetLength(0), x / array.GetLength(0)] = values[x];
}
So, first of all, that just seems inefficient. Not sure why you want to do it this way, but then again, don't know the reason. Looks like a programming assignment.
My guess, you need a sort of double break going on. When you break finding a match, you don't break out to the "k" for loop, so you continue looking for a match even though you found one. Try setting a boolean value to say it's found, then use that in the criteria for the for loop for k. That'll break it out and let you start over on the outside loops for i and j.
Even then, it won't work because you indescriminately subtracted i and j. So if you're on position 1,2 you will jump back to 0,1 rather than 1,2. So you need to subtract j, if it drops below 0, then subtract from i and add "array.GetLength(1)" to j.
This solution depends on HashSet's property of containing unique elements. It has an Add method that returns false when we try to add existing elements.
Random r = new Random();
int[,] array = new int[10, 8];
var usedValues = new HashSet<int>();
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
int uniqueValue;
while (true)
{
uniqueValue = r.Next(10, 100);
if (usedValues.Add(uniqueValue)) break; // It is unique indeed
// It is not unique, try again.
}
array[i, j] = uniqueValue;
}
}
The above solution is more suitable when the range of the acceptable unique values is large. In this specific case the range is very small (10-99), and the solution offered by #Enigmativity is preferable.

IndexOutOfRangeException C#

My goal is to make a triple for loop to multiply matrix X matrix, i get in input the matrix and i have to get matrix^2.
I get the error "IndexOutOfRangeException" - index was outside the bounds of the array when i debug the following code:
for (int i = 1; i < nodeList.Count+1; i++)
{
for (int j = 1; j < nodeList.Count+1; j++)
{
result[i, j] = "0";
for (int k = 1; k < nodeList.Count+1; i++)
{
if ((matrix[i, k] != null) && (matrix[k, j] != null))
{
n1 = Convert.ToInt32(matrix[i, k]);
n2 = Convert.ToInt32(matrix[k, j]);
n3 = Convert.ToInt32(result[i, j]);
total = n3 + n1 * n2;
_total = total.ToString();
result[i, j] = _total;
}
}
}
}
where the variables are:
1. matrix that is type String[,] and the dimensions are (nodelist+1,nodelist+1)
2.result that is is the same type and dimension of the matrix, where i want to put the resultant matrix
3.nodelist is the array of the names of the nodes that i have in the diagram
4. n1,n2,n3 are int, I put in them the convert int from the matrix
5.total is the result of the operation for the multiplication
6._total convert total int in total string for the result matrix
So i set the right dimensions for every array and matrix but i get constantly the same error. I don't get it why. Can please someone help to notice the error, because i don't see it.
In the k loop, you are incrementing i.
for (int k = 1; k < nodeList.Count+1; i++) <-- you are incrementing i, it should be incrementing k.
like this:
for (int k = 1; k < nodeList.Count+1; k++)
Arrays are 0-based in C# -- the first element is at position 0 instead of position 1.
for (int i = 1; i < nodeList.Count+1; i++)
... should be ...
for (int i = 0; i < nodeList.Count; i++)
You also have what appears to be a copy-paste error for the k-loop.
for (int k = 1; k < nodeList.Count+1; i++) // should be k++?
The standard way to use a for loop with an array is to use
for(int x= 0; x < arry.count ;x++)
using 1 and +1 as the conditional will assure that you get an index out of rage as c# arrays are indexed by 0
As mentioned you are incrementing by i in the K loop.
Also you will be getting the out of bounds error every time you try to access an matrix on the last iteration of the loops. Either you need to go from 0 to Count in your loops or you need to put a -1 in all of your matrix operations. ex:
results[i-1, j-1] = _total;
The matrix indexes start at 0.

Categories

Resources