What happened on my `Enumerable.Range(,)`? [closed] - c#

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
What happened on my Enumerable.Range(,)?
It produced a number 5030 which is out of range.
sellableItems is a small List<T>, I need to randomly pick 9 items from it.
Here is the code:
var targetIndexes = Enumerable.Range(i + 1, sellableItems.Count-1)
.OrderBy(x => random.Next())
.Take(9)
.ToArray();
for (var j = 0; j < targetIndexes.Length; j++)
{
...
}

The Enumerable.Range method takes in two parameters - a starting value and a count.
When you do: Enumerable.Range(i + 1, sellableItems.Count - 1), you are starting with the value 37 (i + 1) and a count of 4999 (sellableItems.Count - 1).
Since each iteration in the call to Range increments the previous value by one (except the first iteration, which uses the starting value), the range will be from 37 to 5035.

Related

C# infinite Loops [closed]

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 2 days ago.
Improve this question
Can someone help me understand why the loop below is infinite and how to make it finite?
int index = 1;
while (index != 10)
{
Console.WriteLine("Hello");
index += 2;
}
I tried putting it in Visual Studio but it did not run. I need to understand the logic. I am struggling with this course
It starts with index at 1 and increases by 2 (index += 2)
1
1 + 2
3
------:
-----:
3 + 2
5
------:
-----:
5 + 2
7
------:
-----:
7 + 2
9
------:
-----:
9 + 2
11
------:
-----:
11 + 2
13
Since you only stop at 10, it runs infinite.
(If you stop at >= 10, it will stop at 11)
Try using a debugger.
Visual studio will give you an error 'While' does not exist in the current context. Intellisense is your friend when learning a new language. Your error here is that While should be lower case while.
int index = 1;
while(index != 10)
{
Console.WriteLine("Hello");
index += 2;
}
Why is the loop infinite?
Set a breakpoint on the debugger to step through each line of the while loop. Hover over index to see it's new value upon each iteration. You'll see that your design only increments by odd numbers thus always satisfying your while condition index != 10.
There's many ways to make the loop finite. Start index at 0, or increment by one, or adjust your while condition. But I think the main thing from this question is that you need to brush up on c# syntax and VS intellesense\debugging. These tools will help you eleminate questions about the fundamentals of c#.

How is the exit condition "x=10" in this loop " for (x=0;x=10;x++)" always true? [closed]

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 months ago.
Improve this question
i have typed this code and ran it , and it runs infinitely , i couldn't understand why since it should have stopped once x gets to 10
i expected it to stop at 10 since it started from 0 and incremented by 1 until it gets to 10
for loops do not have an exit condition. They have a continue condition. When the middle condition is tested, the loop continues if it is true. x = 10 is an assignment expression. It assigns 10 to x, and the value of the assignment expression is the new value of x, so it is always 10 (or, if x is a _Bool, it is 1), which serves as “true” for the condition. You may have wanted x == 10, which is a comparison expression. But that is true only when x is 10, so the loop would never execute even the first iteration. You need to a condition to continue the loop, not to exit it, so you want x < 10.

Why for loop is acting like while loop in C# Arrays? [closed]

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 5 months ago.
Improve this question
this for loop inside arrays is working like while and printing only the second index.
what should i do to make it print only even indexes?
You have inverted the += operator in your for loop. You're saying i = +2 which is the same as i = 2
The correct code would be:
for (int i = 0; i < cars.Length; i += 2)
{
Console.WriteLine(cars[i]);
}

How to get the first value of a variable in C# without using array [closed]

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
Main()
{
int X=10;
X=20;
X=30;
Console.WriteLine ("X :"+X);//will print 30.
}
How to get the first value of X without using array.
I want the result as
X=10
Imagine you have a plate X and you put potatoes on it 10
int X=10;
then you remove the potatoes and put salad 20 on it
X=20;
then you remove the salad and put a steak 30 on it
X=30;
and now you ask yourself how do I serve potatoes to your guest. You guest will receive the steak because it is the last value that you put on the plate.
Console.WriteLine ("X :"+X);//will print 30.
I would suggest to use a List. It would be the equivalent of a plate with a memory.
List<int> x_es = new List<int>();
x_es.Add(10);
x_es.Add(20);
x_es.Add(30);
Now you can serve whatever you have placed already before on your plate
x_es[0];
If you really want to avoid array or List you need further plates/variables to store the content temporarily:
int X=10;
int mem_1 = X;
X=20;
int mem_2 = X;
X=30;
Console.WriteLine ("X :"+mem_1);//will print 10.

While loop for an index existing [closed]

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 days ago.
Improve this question
How could I write a while loop that said the following (pseudo code):
While the index of "blahblah" in string 1 exists
do this
while(string1.Contains("blahblah")) {
// do this
}
Pseudocode compiled to C# successfully. 0 errors, 0 warnings. Time taken: 0:00:01.860.
var string1 = "blahblah blahblah blahblah blahblah ";
int pos = -1;
while (0 >= (pos = string1.IndexOf("blahblah", pos + 1)))
{
// do this.
}

Categories

Resources