Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How make this with LINQ?
public uint[,] pole;
public uint selColor;
for (int y = 0; y < cellsamount; y++)
{
for (int x = 0; x < cellsamount; x++)
{
if (pole[x, y] == selColor)
pole[x, y] = 0;
}
}
Is this possible or better to leave it as is?
Here's how you'd do it (but I don't recommend it, it's not very sexy). Better keep your current solution.
pole = pole.Select(i =>
i.Select(j => j == selectColor ? 0 : j).ToArray()
).ToArray();
You could do something like this:
var selectedIndices =
from x in Enumerable.Range(0, cellsamount)
from y in Enumerable.Range(0, cellsamount)
where pole[x, y] == selColor
select new { x, y };
foreach (var index in selectedIndices.ToArray())
pole[index.x, index.y] = 0;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
I know there's a bunch of related questions but I only have one line returning from this method and can't see why the return line would be unreachable:
public List<int> GetMiddleLaneForAI(Room r)
{
int laneCount = GetLaneCount(true);
List<int> matches = new List<int>();
for(int x = 0; 0 < 5; x++)
{
if(aiCardFrames[x].deployedCard == null)
{
if (laneCount < 3)
{
matches.Add(x);
} else
{
if (x > 0 && x < 4)
{
if (aiCardFrames[x - 1] != null && aiCardFrames[x - 1] != null) matches.Add(x);
}
}
}
}
return matches.Count > 0 ? matches : null; // unreachable code detected
}
Look closely at this line:
for(int x = 0; 0 < 5; x++)
When does this for loop exit? ;-)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
int arraySum (int [] a, int n)
{
int sum = 0;
n = a.size();
for (int i = 1; i < n; i++)
sum += a[i];
return sum;
}
I want to convert this code from iterative to recursive.
C# Version:
int arraySum ( int [] a, int sum = 0, int i = 0 ) /*i = 0, technically means this code is logically different from yours, however it will count every element and is just a default :)*/
{
if( i < a.Length )
return arraySum( a, sum + a[i], ++i );
return sum;
}
You need:
1- Recursive definition like: sum(n) = n + sum(n-1)
2- You need to specify where should you stop so the recursion does not last forever.
for example: if (n == 0) return 0;
based on this you can code at any language.
C++ Example:
int arraySum (int a[], int n)
{
if(n==1)
return a[n-1];
else
return a[n-1] + arraySum (a, n-1);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Does anyone have any idea why does this piece of C# code return x = 0 and y = 0 (ocassionally) :
public void NewPos()
{
int x = 0;
int y = 0;
while (lstPosition.Where(z => z.Position.X == x && z.Position.Y == y).Count() != 0) {
x = new Random().Next(4, 20) * 10;
y = new Random().Next(4, 20) * 10;
}
NewPos.X = x;
NewPos.Y = y;
Console.WriteLine(x + " - " + y );
}
You're not ever getting inside the while loop, although we can't tell what lstPosition is set to with the code you've provided. The where clause must be returning an empty set.
There's no way Random.Next(int, int) is returning zero in this situation.
Presumbably, you want to initalize x and y to a non-zero value.
You, probably, want something like this:
// Do not recreate random
// (otherwise you're going to have a badly skewed values);
// static instance is the simplest but not thread safe solution
private static Random s_Generator = new Random();
public void NewPos() {
// Just Any, not Where + Count
if (lstPosition.Any(z => z.Position.X == x && z.Position.Y == y)) {
// If we have such a point, resample it
NewPos.X = s_Generator.Next(4, 20) * 10;
NewPos.Y = s_Generator.Next(4, 20) * 10;
// Debug purpose only
Console.WriteLine(x + " - " + y );
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
if (i%2==0)
{
Console.WriteLine(i);
}
}
}
}
It's start from 0 but you can't see.Try this:
for (int i = 0; i < 1000; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
if (i % 100 == 0) Console.ReadKey();
}
Press enter to see next numbers.That will show 100 numbers at once,you can change it if you want to display less number..
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example if I were to have
Textbox1.Text = "aaaabbbccDdff";
How would I convert that to a letter and numeral representation of the letters based on how many of them are in a row, ie having it turn into
Textbox2.Text = "a4b3c2Ddf2";
Try with this:
string s = Textbox1.Text; //"aaaabbbccfffff";
string r = "";
int count = 0;
char currChar = s[0];
for(int i = 0; i < s.Length + 1; i++)
{
if(i >= s.Length || currChar != s[i])
{
r += currChar + count.ToString();
count = 1;
if(i < s.Length)
currChar = s[i];
}
else count++;
}
Textbox2.Text = r;
For the Linq fanatics:
var r = string.Join("", s.GroupBy(c=>c).Select(x=>x.Key+x.Count().ToString()));