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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I think my loop have some problem. First i consider (i=0 and i>1) but i have no idea how to write. Any one can help me?
logik i want is
//start
=>if i=0 copy from txtbox1;
=>after that, compare i=2 and i=3 see whether are same. if same then copy from txtbox;
=>i++ until the last, every 1,2,3,4... will show differend string;
//end
public void OnMasterColumnChanged(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderMasterColumnChangedEventArgs e)
{
for (int i = 0; i < e.MasterRecord.DetailCount; i++)
{
if (i == 0)
{
e.MasterRecord.GetDetailRecord(i).YourPONo = TxtBox1.Text;
}
else if (i > 1)
{
if (e.MasterRecord.GetDetailRecord(i).YourPONo == e.MasterRecord.GetDetailRecord(i - 1).YourPONo)
{
e.MasterRecord.GetDetailRecord(i).YourPONo = TxtBox1.Text;
}
}
}
}
I think that you want that:
public void OnMasterColumnChanged(BCE.AutoCount.Invoicing.Sales.SalesOrder.SalesOrderMasterColumnChangedEventArgs e)
{
if (e.MasterRecord.GetDetailRecord.Count == 0)
return;
e.MasterRecord.GetDetailRecord(0).YourPONo = TxtBox1.Text;
if (e.MasterRecord.GetDetailRecord.Count < 3)
return;
for (int i = 2; i < e.MasterRecord.DetailCount; i++)
{
if (e.MasterRecord.GetDetailRecord(i).YourPONo == e.MasterRecord.GetDetailRecord(i - 1).YourPONo)
{
e.MasterRecord.GetDetailRecord(i).YourPONo = TxtBox1.Text;
}
}
}
IF GetDetailRecord method returns the different values for different inputs then You are passing the different values below for the GetDetailRecord and checking the equal condition.
if (e.MasterRecord.GetDetailRecord(i).YourPONo == e.MasterRecord.GetDetailRecord(i - 1).YourPONo)
At one place you are passing GetDetailRecord(i) and checking with the GetDetailRecord(i - 1)
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 6 years ago.
Improve this question
string text1 = "abcde";
string text2 = "fgchi";
I want to check if the 2 strings have the same characters at the same index and if they do then let the place where they are the same be printed.
for (int i = 0; i < text1.Length; i++)
if (text1[i] == text2[i])
Console.WriteLine("Character {0} at index {1}", text1[i], i);
Considering your strings have the same length.
Edit: if I should not give answers to trivial tasks such as this and instead encourage the user to find it by himself, then please point out that to me. I'm new around. [I suppose it's obvious, so i'm just not gonna do it, and adjust]
Maybe something like the following code helps. And it shouldn´t be important how long each string is with that. Maybe the string.Format isn´t needed.
private string charMatch(string str_a, string str_b)
{
int char_a = str_a.Count();
int char_b = str_b.Count();
int runs = 0;
StringBuilder sb = new StringBuilder();
if (char_a <= char_b) { runs = char_a; }
else { runs = char_b; }
for (int i = 0; i < runs; i++)
{
if (str_a[i] == str_b[i])
{
sb.Append(string.Format("Match found at {0} \n", i));
}
}
return sb.ToString();
}
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
For example:
What is the disadvantage/advantage of using this block:
for(int i = 0; i < 10; i++)
{
if(i < 9){}
else Console.Writeline(i);
}
As opposed to this block:
for(int i = 0; i < 10; i++)
{
if(i < 9)
{
continue;
}
Console.Writeline(i);
}
Considering their output is exactly the same.
Empty blocks are weird, so in this case, you would just invert the condition:
for (int i = 0; i < 10; i++)
{
if (i >= 9)
Console.Writeline(i);
}
But in the general case, using continue or break is a good way to prevent your code from being indented too many levels. For some discussion on that topic, see this question on Programmers SE.
I always do
// ...
if(!(i < 9))
Console.Writeline(i);
// ...
to prevent an empty if statement.
In your code, there is little difference between the two, with the first it doesn't reach the else statement, and the second it skips over the rest in the loop.
The only reason I find to chose one over the other is legibility. I personally use both forms, opting for one or another based more on the length of the block after the if. For example, if it contains a few lines only I just use the plain if:
for (int i = 0; i < 10; i++)
{
if (i >= 9)
Console.Writeline(i);
}
(example taken from poke answer).
But if the work is considerable, I would invert the condition and rely on continue instead:
for (int i = 0; i < 10; i++)
{
if (i < 9) continue;
Console.Writeline(i);
Console.Writeline("this");
Console.Writeline("that");
Console.Writeline("blablabla");
Console.Writeline("hello");
Console.Writeline("world");
Console.Writeline(i + 1);
Console.Writeline(i + 2);
}
Doing so avoids the indentation on a much bigger block. Doing on a single or very few lines is generally easy to read, but on a longer block can become more difficult to follow.
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.
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;
}