C# infinite Loops [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 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#.

Related

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.

What happened on my `Enumerable.Range(,)`? [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 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.

What relevance is 1.307 to the series 1 + 1/2 + 1/3 + 1/4... + 1/n [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 4 years ago.
Improve this question
I'm currently working my way through the Nakov book, Fundamentals of Computer Programming in C#. In Chapter 4 question 12 states:
Write a program that calculates the sum (with the precision of 0.001) of the following sequence: 1 + 1/2 - 1/3 + 1/4 - 1/5 + …
It seemed to me to be a relatively straightforward question. The series is a diminishing fraction that does not have an asymptote. Stopping the loop at a certain point due to diminished changes in value meets the precision requirements AFAIC. However, the solution given in both the Hungarian and English versions of the book makes reference to an obscure (to me) value of 1.307. As follows:
Accumulate the sum of the sequence in a variable inside a while-loop (see the chapter "Loops"). At each step compare the old sum with the new sum. If the difference between the two sums Math.Abs(current_sum – old_sum) is less than the required precision (0.001), the calculation should finish because the difference is constantly decreasing and the precision is constantly increasing at each step of the loop. The expected result is 1.307.
Can someone explain what this might mean?
Note that header contains "harmonic sequence" that has no limit.
But question body shows alternate sign sequence that converges towards value 2 - ln(2)
The expected result is 1.307.
I think they are simply saying what the result of the calculation is, so you can check your answer.
The sequence you've got
1 + 1/2 - 1/3 + 1/4 + ...
is the same as the Alternating Harmonic Series on Wikipedia, except with the signs from 1/2 onwards flipped:
1 - 1/2 + 1/3 - 1/4 + ... = ln 2
and the natural logarithm of 2, ln 2, = 0.693. Hence your 1.307 here = 2 - ln 2.

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.

Can you explain me what this method does? [closed]

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 8 years ago.
Improve this question
We found this method in a book.
From what I understand wants to check if a number is ordered ascending.
For example, the number 54321 (all figures have ordered ascending)
However, I do not understand how this method works ... why returns 0 or 1?
Can you explain to me in a simple way what happens in this method?
static int f(long n)
{
while(n>10)
{
if (n % 10 > n / 10 % 10) return 0;
n = n / 10;
}
return 1;
}
n % 10 gets you the digit in the unit's place and n / 10 % 10 gets you the digit in the ten's place.
The author is comparing these two digits and returning 0 if the digit in the unit's place is larger than the one in the ten's place.
If not, he is dividing n by 10 to discard the number in the unit's place. Now, the number that was in the ten's place is now in the unit's place and the one in the hundred's place is now in the ten's place and the previous steps are repeated.
If the number becomes less than or equal to 10 after you keep discarding the last number, if it hasn't returned 0, it will return 1, which suggests that all the digits in n are in descending order from left to right.

Categories

Resources