I am trying to calculate the Neighbour near my cells, but the code works for only the middle cells and not for the corner ones, for which I can use many if loops but I wanna know how it works with for and if loops.
I tried creating extra if loops or writing like if row ==0 etc. but everything seems to give me wrong answer. btw bool[,] grid has the values of false and true. and we need to calculate how many trues and false are their in give row/ col.
public static int CountNeighbours(bool[,] grid, int row, int col)
{
int total = 0;
for (int i = row - 1; i < row + 2; i++)
{
for (int j = col - 1; j < col + 2; j++)
{
if (grid[i, j] == grid[row, col])
{
total += 1;
}
}
}
Console.WriteLine("total neighbours for row {0} and column {1}: {2}",row,col,total-1);
return total-1;
}
The result should give perfect answer for corners and rows.
Your problem is that you count the cell itself as its own neighbour and that you don't check the boundaries of your grid.
Change your loop conditions like that:
for (int i = Math.Max(0, row - 1); i < row + 2 && i < grid.GetLength(0); i++)
{
for (int j = Math.Max(0, col - 1); j < col + 2 && j < grid.GetLength(1); j++)
{
if (i == row && j == col) continue; // skip current cell
So with Math.Max() you ensure that you don't use a negative i or j, because that would lead to an IndexOutOfRangeException.
With grid.GetLength() you get the array size in the respective dimension, because using a i or j greater or equal to that length would again raise an IndexOutOfRangeException.
And finally the last line checks that you don't count the cell in question itself.
Related
I'm currently messing around with 2D arrays. I want to Fill a 2D array with a count. I've managed to do this using 2 nested for loops. (That's probably the easiest way of doing it right?)
//create count
int count = 1;
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(0); col++)
{
matrix[row, col] = count++;
}
}
I was just curious, is it also possible to fill this 2D array using only a single for loop?
I thought of making a loop that counts the rows. When the rows reaches the end of the array the column wil be increased by 1. This could probably be done by using some if, if else and else statements right?
Does someone here have any idea how to make this work?
Here you go
int[,] matrix = new int[5, 10];
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
for (int i = 0; i < row * col; i++)
{
matrix[i / col , i % col] = i + 1;
}
https://dotnetfiddle.net/Lv9DvT
Yes, of course you can.
for(int i = 0; i < matrix.GetLength(0) * matrix.GetLength(1); i++)
{
int row = i / matrix.GetLength(1);
int column = i % matrix.GetLength(1);
matrix[row, column] = i;
}
Works with NxN arrays.
I want to fill my array with struct in a way that it will look like this
I have this code so far :
`for (int i = 1; i <= n; i++)
{
for (int j = 0; j <=(n - i); j++)
//i should fill m[,] here
for (int j = 1; j <= i; j++)
//i should fill m[,] here
for (int k = 1; k < i; k++)
//i should fill m[,] here
}
for (int i = n - 1; i >= 1; i--)
{
for (int j = 0; j < (n - i); j++)
//i should fill m[,] here
for (int j = 1; j <= i; j++)
//i should fill m[,] here
for (int k = 1; k < i; k++)
//i should fill m[,] here
}`
but I 'm a little bit confused with the index .
how can I adopt this code?
As it's unclear wether the array always has the size 5 I'll assume it has the size n with n being odd and n > 0 (The size is the x and the y size, as I assume that your matrix is quadratic). Then there are several ways to reach the goal your trying to reach, I'll try to present you one I thought of.
First of all we have to think about the array - as it's the easiest way, I'll assume it consists of boolean values (even though you said "I want to fill my array with struct", but as I'm not quite sure what you wanted to say with that and wether you really meant a struct, I'll let this up to you, as this shouldn't be the most difficult part):
var matrix = new bool[n,n];
Then we have to evalueate which fields have to be filled. Therefore we must realize a few things:
The filled fields are always central in their line
The first and last line always have one filled item
The following line always has two more/less filled items, so that the offset is one more than in the previous line
The center line has most items and is the turning point in the amount of filled items
As a first step in developing the algorithm, I'd write a function to fill lines of the array with specific amounts of fields:
private static void FillLine(int line, int count, bool[,] matrix)
{
//Firstly we have to evaluate the offset:
var offset = (matrix.GetLength(0) - count) / 2;
//Then we have to fill the line
for (var x = offset; x < offset + count; x++)
matrix[x, line] = true;
}
Now we simply have to fill the lines for the whole array:
public static void FillDiamond(bool[,] matrix)
{
var count = 1;
for (var line = 0; line < matrix.GetLength(1) / 2; line++)
{
FillLine(line, count, matrix);
count += 2;
}
FillLine(matrix.GetLength(1) / 2, count, matrix);
count = 1;
for (var line = matrix.GetLength(1) - 1; line > matrix.GetLength(1) / 2; line--)
{
FillLine(line, count, matrix);
count += 2;
}
}
Now in a console application you could use this like that:
using System;
namespace SO_c
{
internal static class Program
{
private static void Main()
{
while (true)
{
var n = int.Parse(Console.ReadLine());
if (n < 1 || n % 2 == 0)
continue;
var matrix = new bool[n, n];
FillDiamond(matrix);
for (var y = 0; y < matrix.GetLength(1); y++)
{
for (var x = 0; x < matrix.GetLength(0); x++)
Console.Write(matrix[x, y] ? "█" : " ");
Console.WriteLine();
}
}
}
private static void FillLine(int line, int count, bool[,] matrix)
{
//Firstly we have to evaluate the offset:
var offset = (matrix.GetLength(0) - count) / 2;
//Then we have to fill the line
for (var x = offset; x < offset + count; x++)
matrix[x, line] = true;
}
public static void FillDiamond(bool[,] matrix)
{
var count = 1;
for (var line = 0; line < matrix.GetLength(1) / 2; line++)
{
FillLine(line, count, matrix);
count += 2;
}
FillLine(matrix.GetLength(1) / 2, count, matrix);
count = 1;
for (var line = matrix.GetLength(1) - 1; line > matrix.GetLength(1) / 2; line--)
{
FillLine(line, count, matrix);
count += 2;
}
}
}
}
This can result in output like that:
That's it! Now you should get your diamond for every matrix that fits the rules :)
One way to do it is to calculate the center of the grid and fill in each row, from the center column. In each successive row, we increment the number of blocks we fill in on each side of the center column until we get to the center row, and then we decrement the number of blocks we fill in until we get to the end.
To determine how many blocks to put on each side of the center column for any given row, we can use the row index for the calculation.
Working our way from the top down to the center, the row index represents the number of blocks to add. So, for row index 0, we add 0 blocks on either side of the center column, row index 1 we add 1 block on each side, until we get to the center.
After we get to the center, we want to decrement the number of blocks each time, which can be done by subtracting the row index from the total number of rows.
The code says it better, I think:
private static void Main()
{
while (true)
{
// Get grid size from user and keep it between 1 and the window width
var gridSize = GetIntFromUser("Enter the size of the grid: ");
gridSize = Math.Min(Console.WindowWidth - 1, Math.Max(1, gridSize));
var grid = new bool[gridSize, gridSize];
var center = (gridSize - 1) / 2;
// Populate our grid with a diamond pattern
for (int rowIndex = 0; rowIndex < grid.GetLength(0); rowIndex++)
{
// Determine number of blocks to fill based on current row
var fillAmount = (rowIndex <= center)
? rowIndex : grid.GetUpperBound(0) - rowIndex;
for (int colIndex = 0; colIndex <= fillAmount; colIndex++)
{
grid[rowIndex, center - colIndex] = true;
grid[rowIndex, center + colIndex] = true;
}
}
// Display the final grid to the console
for (int row = 0; row < grid.GetLength(0); row++)
{
for (int col = 0; col < grid.GetLength(1); col++)
{
Console.Write(grid[row, col] ? "█" : " ");
}
Console.WriteLine();
}
}
}
Oh, and this is the helper function I used to get an integer from the user. It's pretty helpful to keep around because you don't have to do any validation in your main code:
private static int GetIntFromUser(string prompt)
{
int value;
// Write the prompt text and get input from user
Console.Write(prompt);
string input = Console.ReadLine();
// If input can't be converted to a double, keep trying
while (!int.TryParse(input, out value))
{
Console.Write($"'{input}' is not a valid number. Please try again: ");
input = Console.ReadLine();
}
// Input was successfully converted!
return value;
}
OUTPUT
I am building a minesweeper game in C#, and one of my functions keeps crashing the program. The erorr message is Process is terminated due to StackOverflowException. I tried to find where is the problem in my recursion, but I didn't find it. The problem only happen if Openk is called, so I am pretty sure the problem is there. I tried to find if there is an infinite recursion, but didn't find it. What could be the problem?
public void Openk(int row, int col)
{
if (sqrs[row, col].IsBlank() == true)
{
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
if (IsValid(row - i, col - j))
{
if (sqrs[row - i, col - j].IsMine() == false)
sqrs[row - i, col - j].Open();
Openk(row - i, col - j);
}
}
}
}
}
public bool IsValid(int row, int col)
{
if (row >= n || row <= 0 || col >= n || col <= 0)
return false;
return true;
}
Your problem is that you keep looping and executing Openk on the same middle square each time. Maybe it is because of the unguarded if statement. It goes like this in your code.
You pass the selected coordinates of a square, say [5,5].
Then you loop a row before 5 (4), row 5, and a row after 5 (6).
For each row, your loop a square before the selected column 5 (4), column 5, and and the next column of 5 (6).
In your code, only when you reach the middle square (i = 0, j = 0), you execute the checking of square if it is a mine, and if it is not mine, you pass the selected coordinates after subtracting with zeros (i = 0, j = 0), which effectively loops the same spot again!
.
public void Openk(int row, int col)
{
if (sqrs[row, col].IsBlank() == true)
{
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
// Only execute the following code when checking the middle square, where i = 0, and j = 0.
if (i == 0 && j == 0) // Notice the braces I added, the code has the same effect if you don't type them.
{
// This if statement has not effect.
if (IsValid(row - i, col - j)) // It is like if (true)
{
if (sqrs[row-i, col-j].IsMine() == false)
sqrs[row - i, col - j].Open();
Openk(row - i, col - j); // row - 0 = row, col - 0 = col, your looping infinitely here.
}
}
}
}
}
}
I'm relatively new to programming, and I'm currently trying to get to grips with C#. I've reached a point where I'm learning about 'for' loops. The problem I'm stuck on is to create the shape below using only 'for' loops and the character '*' only once.
*
***
*****
*******
*********
The code I've written is below:
for (int row = 0; row < 5; row++)
{
for (int column = 0; column + row < 4; column++)
{
Console.Write(" ");
}
for (int column = 0; column + row >= 4; column++)
{
if (column - row >= 5)
break;
Console.Write("*");
}
Console.WriteLine();
}
From what I can see, the second nested 'for' loop is being skipped until the first loop is complete.
Replacing the spaces with "O", this displays the following:
OOOO
OOO
OO
O
*********
**********
***********
************
*************
**************
Can anyone see where I'm going wrong?
The problem is in the loop condition for the second nested loop, column + row >= 4. Since column starts at zero, the first execution is basically the same as row >= 4 so for any row < 5 it won't print, which is exactly the conditions of the first loop. This means all the spaces are printed, and then a bunch of stars.
You also don't need the if-statement since that is basically a second loop condition. Something like for (int column = 0; column <= row * 2; column++) would make the most sense.
You also should probably re-evaluate the outer loop, since you only want 5 lines, and you will go through 10 lines as-is.
for (int row = 0; row < 5; row++)
{
for (int column = 0; column + row < 4; column++)
{
Console.Write(" ");
}
for (int column = 0; column <= row * 2; column++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int row = 0; row < 10; row++)
{
// This code will only get run for the first 3 rows
for (int column = 0; column + row < 4; column++)
{
Console.Write(" ");
}
// This code will only get run for rows 4 and greater
for (int column = 0; column + row >= 4; column++)
{
if (column - row >= 5)
break;
Console.Write("*");
}
Console.WriteLine();
}
What you need to do is ensure your are putting the correct number of spaces followed by the correct number of * for each line.
Hint: You probably only want 5 rows not 10
This works:
for (int row = 0; row < 5; row++)
{
for (int column = 0; column < 5 + row + 1; column++)
Console.Write(column < 5 - row ? " " : "*");
Console.WriteLine();
}
*
***
*****
*******
*********
Worked out a solution:
for (int row = 0; row < 5; row++)
{
for (int column = 0; column + row < 4; column++)
{
Console.Write(" ");
}
for (int column = 0; column + row >= 0; column++)
{
if (column == (row * 2) + 1)
break;
Console.Write("*");
}
Console.WriteLine();
}
x...'s solution is probably more appropriate, being clearer and without the need for an if statement and any breaks, but you all helped me reach the answer without too much hand holding, and I appreciate that.
Thanks :D
A simple logic can be like this.
for(i=0 to 4) //since five rows
for(j=0 to 2*i-1) // is in multiple of 1, 3, 5
print(*)
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.