Nested for loop not executing - c#

Could anyone tell me why the nested for loop in the code below doesn't execute? I.e. The "Hello World" is not printed. The first loop is being executed.
for (int i = 0; i < data.Length; i++)
{// Loop through array
**for (int j = data.Length - 1; j < i; j--)**
{
// Loop backwards through array
**Console.WriteLine("Hello World");**
double subTotal = 0; //Keeps track of current subsequence's value
subTotal += data[j];
if (bbestTotal < subTotal)
{
bbestTotal = subTotal;
}
}
}

The loop is not executing because the loop condition
j < i
is false right at the beginning of the loop.
Since your loop advances j down, you should change the condition to
for (int j = data.Length - 1 ; j >= i ; j--)

The inner loop variable j is initialized with top value and it is greater then i so use j > i instead of j < i in loop condition part.
Change
for (int j = data.Length - 1; j < i; j--)
to
for (int j = data.Length - 1; j > i; j--)

The root cause of the problem is that the condition j < i for the 2nd for loop is always false for all values of i. So it never goes inside the body of the 2nd for loop.
This should fix the problem:
for (int j = data.Length - 1; j > i; j--)

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;
}
}
}
}

Difference between using brackets in for loop C# and not using it

The output of the following code is different from the ouput from the second code
can someone explain the problem?
Code 1:
for(int i = 1; i <= intInput; i++)
{
for(int j = 1; j<=i; j++)
{
Console.Write('+');
Console.WriteLine();
}
}
if intInput is 4 Ouput is:
+
+
+
+
Code 2:
for(int i = 1; i <= intInput; i++)
{
for(int j = 1; j<=i; j++)
Console.Write('+');
Console.WriteLine();
}
if intInput is 4 Ouput is:
+
++
+++
++++
I know how this line of codes works but i dont understand what difference the brackets make on both codes?
When you write;
for(int j = 1; j <= i; j++)
{
Console.Write('+');
Console.WriteLine();
}
Both Console line works until j loops out.
But when you write
for(int j = 1; j <= i; j++)
Console.Write('+');
Console.WriteLine();
Only first Console works until j loops out. That's why second one is equal to;
for(int j = 1; j<=i; j++)
{
Console.Write('+');
}
Console.WriteLine();
If there is one statement included in the loop, the curly brackes can be omitted. But using them is always a better approach.
Read: Why is it considered a bad practice to omit curly braces?
You second case effectively means:
for(int i = 1; i <= intInput; i++)
{
for(int j = 1; j<=i; j++)
{
Console.Write('+');
}
Console.WriteLine();
}
Indentation means nothing for compiler it is only for you
The loop has a scope. If you do not include the braces, only the first line is in the loop. If you have the braces, everything inside falls under the scope of the loop.
In this case, the first example write a "+" to the console as well as a new line every iteration of the inner loop.
The second case, the inner loop only executes the "+" writing on each inner iteration. The outer loop adds the new line.
If alter 1 with i in second loop then it will work same
for (int j = **i**; j <= i; j++)
Console.Write('+');
Console.WriteLine();

Output to the screen without moving to a new line

Console.WriteLine automatically move text on a new line and I have output on the console like:
1
2
3
4
5
..
But I need:
1234
5..
Code:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
Console.WriteLine(aField[i, j]);
Console.WriteLine();
}
If you don't want to write a line, don't use Console.Write*Line*. Just use Console.Write:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write(aField[i, j]);
}
Console.WriteLine();
}
(Note that your sample output only contains four characters, whereas your inner loop has 5 iterations. I'm hoping this is just a sample discrepancy, and nothing more complicated.)

Remove elements from array. index. C#. List<double>

I need your help. It's easy question, I think.
So. I work in C# with array.
var TinArr = new List<double>();
Next I have some elements in it. For example:
TinArr[0]=0.5;
TinArr[1]=0.6;
TinArr[2]=1.1;
TinArr[3]=1.5;
Ok. Then I have cycle for remove some of them:
for (var j = 0; j < TinArr.Count; j++)
{
if (TinArr[j] <= 1)
{
TinArr.RemoveAt(j);
}
}
As I understand, when I'll remove element with index "0", next element with index "1" after this action will have index "0". Maybe I'm wrong. How can I save index? Or maybe I should start cycle from the beginning? NEED YOU HELP! THANKS!
You could simply use the RemoveAll method:
TinArr.RemoveAll(x => x <= 1);
But if using a for-loop is a requirement, you should start from the end:
for (var j = TinArr.Count - 1; j >= 0; j--)
{
if (TinArr[j] <= 1)
{
TinArr.RemoveAt(j);
}
}
Do it in reverse rather, something like
for (var j =TinArr.Count - 1; j >= 0; j--)
{
if (TinArr[j] <= 1)
{
TinArr.RemoveAt(j);
}
}
You almost did it yourself, only missing post-decrement:
for (var j = 0; j < TinArr.Count; j++)
if (TinArr[j] <= 1)
TinArr.RemoveAt(j--);

Index out of range exception in 2D Array (C#)

char[,] map = new char[10, 20];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; i < map.GetLength(1); j++)
map[i, j] = '.';
}
I just simply want to make all the elements of map[i,j] to be a point , but always when I try to run it the compiler says: Index out of range exception. Maybe it's a stupid question but I had to ask it.
See the i in your j-loop
for (int j = 0; j < map.GetLength(1); j++)
You use i instead of j look at this:
char[,] map = new char[10, 20];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
map[i, j] = '.';
}
}

Categories

Resources