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 writing C# program in Visual Studio 2010, to build a windows application.
I have created a class named Store. And inside Store class, I have created a method named StoreLength(Store ob)
public int StoreLength(Store ob)
{
int i = 0, L = 0;
while (i < 100)
{
if (ob.a[i] != null)
L += 1;
}
return L;
Now from other class I have created an object of Store class. And using that object I am trying to use the StoreLength method.
private void buttonEqual_Click(object sender, EventArgs e)
{
int l = ob.StoreLength(ob);
DisplayUnit.Text = Convert.ToString(l);
}
Now when I am running the program, everythign is working properly but as soon as I am clicking on buttonEqual, the window freezes. I think there is some problem with in buttonEqual1_Click mothod, or in StoreLength method.
Please help.
It's because the while loop's condition is always satisfied.
Every time it evaluates i, it'll always be less than 100 because you set it to 0 and its value does not change.
The typical approach would be to increment i by 1 on every iteration:
while (i < 100)
{
i++;
if (ob.a[i] != null)
L += 1;
}
The reason this causes it to freeze is because your UI is 'waiting' (in a roundabout way) for this while loop to complete.
Also, consider using a for loop for scenarios like this. It suggests the declaration, initialisation, condition and increment all on the one line for readability and consistency sake:
for(int i = 0; i < 100; i++)
{
if (ob.a[i] != null)
L += 1;
}
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 2 years ago.
Improve this question
I'm a begginer in c# and I'm trying to solve problems and exercises, what I have to do is write a method that compares the jump height of a jumper to the height of the hurdles, if the jump height is equal to or greater than the biggest hurdle, the method should return true. The heights of the hurdles are imputed in the form of an array, and the jump height is an integer.
The code I wrote was:
public static bool hurdleJump(int[] hurdles, int jumpHeight)
{
int height= 0;
for(int i = 0; i == hurdles.Length; i++)
{
if(hurdles[i] > height)
height = hurdles[i];
}
if(jumpHeight >= height)
{
return true;
}
else
{
return false;
}
}
I used a for loop to find the biggest value inside of the array and then compared that to the int, but this code doesn't work with some arrays and I have no idea why, what am I missing?
Your loop is not running because of the condition;
for(int i = 0; i == hurdles.Length; i++)
You must use:
for(int i = 0; i < hurdles.Length; i++)
This should do it, much simple than using a loop:
public static bool hurdleJump(int[] hurdles, int jumpHeight)
{
return jumpHeight >= hurdles.Max();
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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
I'm making a dice of the Royal Game of Ur in C#. It needs 4 4-sided pyramids that have 2 out of 4 peaks colored white. This means that a single dice gives 50/50 chance of being either 0 or 1.
Now look at this code and tell me why it sometimes gives me 5 and 6.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ButtonScript : MonoBehaviour {
public Button rollButton;
public int result;
void Start()
{
rollButton.onClick.AddListener(onClick);
}
void resultReset()
{
Debug.Log("Setting result from " + result + " to 0");
result = 0;
}
public int Calculate()
{
for (int i = 0; i < 4; i++)
{
int num = Random.Range(0,2); // Either 1 or 0.
result = result + num; // num is added to total result
if (result > 4)
{
Debug.Log("Rolling " + result + " not possible!!");
}
}
return result;
}
void onClick()
{
resultReset();
int iRolled = Calculate();
Debug.Log("I rolled " + iRolled); // Sometimes gives 5+ and skips the for loop (like, goes 1-2 times and gives out an impossible number)
}
}
I can't reproduce this, but my best guess would be that the onClick method is tied to two different objects that are both being triggered simultaneously? Or maybe the event handler is added to a single object multiple times somehow. Either might explain why it is being fired twice. You might have a race conditions where both objects reset the [shared] result and then both start adding to it at the same time. Try running your loop against non-shared local variables instead, like this:
public int Calculate()
{
int thisRoll = 0; // only accessible from inside the method
for (int i = 0; i < 4; i++)
{
int num = Random.Range(0,2); // Either 1 or 0.
thisRoll = thisRoll + num; // num is added to total result
if (thisRoll > 4)
{
Debug.Log("Rolling " + thisRoll + " not possible!!");
}
}
return thisRoll;
}
void onClick()
{
//resultReset(); // not necessary anymore
int iRolled = Calculate();
Debug.Log("I rolled " + iRolled); // Sometimes gives 5+ and skips the for loop (like, goes 1-2 times and gives out an impossible number)
// set result here if you actually need it in addition to iRolled:
result = iRolled;
}
At least one of OnClick and Calculate is being called twice.
One time is accounted for by the call to AddListener when Unity calls Start() on ButtonScript. Any additional calls on a click would create a race condition that could produce unexpected output.
Make sure that neither of those methods are included in the button's list of On Click events in the inspector before you run the scene.
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
So I currently have the problem that I am unable to increment my variables. Using 'i++' in my code seems to do nothing and using any other form of incrementing a value does nothing too.
static void Main(string[] args)
{
Console.WriteLine("Enter a number to see its times tables");
int number = Convert.ToInt16(Console.ReadLine());
int i;
for (i = 10; i == 10; ++i)
{
int output;
output = i * number;
Console.WriteLine(output);
}
Console.ReadKey();
}
All I am trying to do is create a basic loop but it is just stuck in an infinite loop, setting my variable 'i' to 10 causes the loop to work like its supposed to but it only executes once. The loop part becomes irrelevant.
Try
for (i = 0; i <= 10; i++)
Your condition means that the loop executed an incorrect number of times.
By incorrect number looking at your post and various attempts this means anything from zero to 'a positive' (but incorrect) number of times.
An if statement is a compound statement. In your case your if statement is composed of three sub-statements - an initialization, a test and an increment.
When the if statement is executed these three statements are executed in order.
If the test returns false the loop is not entered so when the condition is 'i==10' and the initialization is 'i=1' the loop is not executed at all. When the initialization is 'i=10' then the loop executes once.
Sub-statements 2 and 3 are executed after each execution of the content of the loop and as long as statement 2 resolves to true the loop will continue being executed.
In our example using a condition 'i <= 10' would cause the code within the loop to be executed 11 times. 'i<10' would cause it to be run ten times. Care has to be taken when choosing sub-statement 2 (i.e. the loop condition). You have to make sure that the initial value in statement 1 and the increment in statement 3 (or alternatively a change to the value made within the body of the loop itself) guarantee that the condition will eventually fail.
If not then code has to be added to the loop to ensure that the loop will exit.
This will loop indefinitely
for (int x=0; x==0; ) {
}
This will NOT loop indefinitely
for (int x=0; x==0; ) {
x++;
}
A test and break out of a loop could be done like shown in the following.
for (int x=0; x==0; ) {
if (x==0) {
break;
}
}
If you do not ensure that the loop is guaranteed to terminate it could potentially never exit.
By Using
(i = 0; i < 10; i++)
The range of i will be from 0 to 9
By using
(i = 0; i <= 10; i++)
The range of i will be from 0 to 10
By using
(i = 1; i <= 10; i++)
The range of i will be from 1 to 10
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 7 years ago.
Improve this question
I am working on a MinHeap implementation for school and I have encountered a problem. The code typically works well but sometimes generates an argument out of range exception in my heapify method. I have tried to isolate the problem but I am a terrible debugger.
Here is my code for the function:
private void Heapify(int i)
{
int least;
int leftchild = 2 * (i + 1) - 1;
int rightchild = 2 * (i + 1);
if (leftchild < heap.Count && (heap[rightchild].CompareTo(heap[i]) < 0))
{
least = 1;
}
else
{
least = i;
}
if (rightchild < heap.Count && (heap[rightchild].CompareTo(heap[least]) < 0))
{
least = rightchild;
}
if (least != i)
{
T temp = heap[i];
heap[i] = heap[least];
heap[least] = temp;
this.Heapify(least);
}
Out of range exceptions are generally easy to track down. Basically, you have to make sure that whenever you're accessing an item in an array via indexes, said array's count / length is greater than the index. In other words, ensure that in every call to heap[#index], #index < heap.Count (either by straight checking it or by your method's logic)
if (leftchild < heap.Count && (heap[rightchild].CompareTo(heap[i]) < 0))
If rightchild >= heap.Count, this will give you an exception.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I made the algorithm below for bubblesort exercise at school.
//Bubble Sort (My code)
static void _BubbleSort(int[] a)
{
for (int i = 0; i < a.Length - 1; i++)
{
for (int j = 0; j < a.Length - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
swap(a, j);
}
}
}
}
But when I check the internet I see different algorithm below.
The algorithm I found in internet uses different "for" loop as follows. Mine is nested for loops but the code below is not nested.
public void BubbleSort(int[] b)
{
for (int pass = 1; pass < b.Length; pass++) // passes
for (int i = 0; i < b.Length - 1; i++) // one pass
if (b[i] > b[i + 1]) // one comparison
Swap(b, i); // one swap
}
What I want to ask is my code is an example of bad programming or not? or my brain is working different than you computer science guys? I am arts student by the way if you wonder.
What I want to ask is my code is an example of bad programming or not?
Your code almost is identical. There is a difference in that you are (correctly) using curly brackets to explicitly state code blocks, while the other example isn't. One problem is your loop bounds check.
#Sriram also pointed to the fact that your loop uses a.Length - 1 - i while the latter simply checks for b.Length - 1, which isn't actually necessary on your part and would cause the loop to prematurely end. Use the latter approach from the second example.
Other than the fact you shouldn't use _ at the beginning of your method (this is simply a naming convertion), your algorithm is identical.