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 8 years ago.
Improve this question
I am checking for the index to be in range, but it's throwing a System.IndexOutOfRangeException... Theoretically the following should work:
for (int b = 1; b <= p.Length-2; a++)
but I still get a
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at scred.Program.Main(String[] args) in c:\Users\\Documents\Visual Studio
2013\Projects\CodeJam\Store Credit - Small (C-Sharp)\Program.cs:line 29
on run
Code: https://gist.github.com/mypalsminecraft/9498980 (who needs Pastebin?)
It seems like that you have done a copy&paste error:
for (int a = 1; a <= p.Length-2; a++)
{
bool done = false;
for (int b = 1; b <= p.Length-2; a++)
{
should be
for (int a = 1; a <= p.Length-2; a++)
{
bool done = false;
for (int b = 1; b <= p.Length-2; b++)
{
instead. Notice the b++ instead of the a++.
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 3 years ago.
Improve this question
Ran into the notation:
for (var i = 0; i < count; i++) { }
on this tutorial. I gather it is equivalent to:
for(var i = 0; i < count; i++) { }
What is this notation? Why use it? Does it perform better?
It's an HTML parsing error. < is supposed to show up as <. You're right - it's supposed to be i < count.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
When attempting to use the following bit of code to answer a question Box Blur on the website CodeFights.com, I receive the following message which has me stumped:
file.cs on line 2: error CS0029: Cannot implicitly convert type `int' to `int[][]'
I'm confident that this is something simple that I'm missing but, my searching for an answer to this has not returned any success.
Any ideas as to what simple mistake I am committing?
int[][] boxBlur(int[][] matrix) {
int[][] result = new int[matrix.Length-2][matrix[0].Length-2];
for (int i =1; i < matrix.Length-1; i++)
for (int j = 1; j < matrix[0].Length-1; j++)
{
int sum=0;
for(int k=i-1; k<=i+1; k++)
{
for(int m=j-1;m<=j+1;m++)
{
sum+=matrix[k][m];
}
}
result[i-1][j-1]=sum/9;
}
return result;
}
Wrong call array. Try this(or something like):
int[][] result = new int[matrix.Length - 2][];
for(int i=0; i< matrix.Length - 2; i++)
{
result[i] = new int[matrix[0].Length - 2];
}
int[][] is an array of arrays. You can't initialize the size of the arrays within it when you're initializing it.
If you want a two-dimensional array, use int[,].
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 years ago.
Improve this question
I have problem with for loop in C#.
I have the following code, in main method:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for(int i=0;i>20;i++)
{
if(i%2==0)
{
Console.WriteLine("{0},", i);
}
}
Console.ReadLine();
}
}
}
The for loop does not execute. Why?
Change condition, > to <:
...
for (int i = 0; i < 20; i++) // i < 20, not i > 20
...
In your original code you assign int i = 0 then check i > 20 get false and do not enter the loop at all
The following statement in your code:
for (int i = 0; i > 20; i++)
First assigns 0 to i then evaluates the condition i > 20 which is wrong, so the for block can not be executed.
Other people have pointed out the fact that the test should actually be "i < 20" rather than "i > 20) but just to make one other point: if all you're trying to do is write out all the even numbers, you can actually increment "i" by 2 every time - that way you never have to test to see if it's even (just write "i += 2" instead of "i++").
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 8 years ago.
Improve this question
I don't understand how to do this im new to programming but im sure its simple. help the code I have so far is:
I need the asterisks to be separating the numbers
for (int i = 0; i < 101; i++)
Console.WriteLine(i);
Console.WriteLine("********");
but the asterisks is only showing at the bottom
You should put the body of the loop into a scope:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
Console.WriteLine("********");
}
Otherwise only the following next command is interpreted as the body of the loop, equivalent to this:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
}
Console.WriteLine("********");
The way you have it is only doing the for loop on the first line. You need to put both lines into a 'block' using curly braces:
for (int i = 0; i < 101; i++)
{
Console.WriteLine(i);
Console.WriteLine("********");
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
I am new to C#, and need a little help, i know in PHP that i can to something like this
$SelectedEvent="event selected";
if (i > 0)
{
$SelectedEvent = "event";
}
It replace a string easy, but in c# that is not working ok, here is example code i have that is not working
string SelectedEvent = "event selected";
if (i > 0)
{
SelectedEvent = "event";
}
This is not working like in PHP, i can not override the varible?
Here is my example code
EDITED
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
string SelectedEvent = "event";
}
}
You should not redeclare the variable twice but only modify its value:
for (int i = 0; i < Model.Items.Count; i++)
{
string SelectedEvent = "event selected";
if (i > 1)
{
SelectedEvent = "event";
}
// here you can use the SelectedEventVariable
// its value will be "event selected" on the first and second
// iterations and "event" on subsequent
}