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 7 years ago.
Improve this question
Currently I have multiple if-else loops in my code, such as:
if (a > 2 && a < 4)
{
// do something
}
else if (a > 4 && a < 6)
{
// do something
}
else if (a > 6 && a < 8)
{
// do something
}
... and so on.
is there a way to re-arrange this into a simpler FOR loop? or any other loop which can make the code more optimized?
for (int i = 2; ; i += 2)
{
if (a > i && a < i + 2)
{
//Do your thing
break;
}
}
You could use a switch if you wanted to check for exact values. However, since your conditions are based on mathematic expressions, you are likely better off sticking with what you've got. It may look cluttered, but it's likely already the most efficient way to do what you're trying to do.
if I understand correctly, all you need is put the values in an array as shown below and call the methods dynamically, is that what you are looking for?
var range = [3,5,7] //defined the range as >2 and <4 is equal to 3 and so on.
$.each(range, function(i, val){
window[PerformActionByVal + val]();//call the specific function
});
function PerformActionByVal3()
{
}
function PerformActionByVal5()
{
}
Related
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 5 years ago.
Improve this question
I am trying to find at what index a sine curve (which may start at any point along the curve) reaches its first maximum, and only the first. To do this, I am running a loop which compares one value to its previous. If one point is greater than its previous value, it is trending up, and similar for the opposite.
In c#, how do you detect when the variable has changed from trending up to trending down? In other words, how do you detect when the variable has changed. In LabVIEW, this can be done using a shift register. What is the equivalent in c#?
public static int FirstMaxIndex(int[] values)
{
bool up = false;
for (int i = 1; i < values.Length; i++)
if (values[i] < values[i - 1])
{
if (up) return i;
else up = false;
}
else if (values[i] > values[i - 1])
{
up = true;
}
return -1;
}
I didn't test this. This is only to give you an idea of how to solve this. (I wrote it as close as possible to what you wrote in a comment.)
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 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 6 years ago.
Improve this question
Getting a byte array like this [0,0,0,0,0,1,1,1,1,3,3,3,3,0,0,0,0,0]
Does anyone know how to detect a change from 1 to 3 efficiently in linq?
Why Linq? you could achieve this with simple loop.
int previous = array[0];
for(int i=1;i< array.Length;i++)
{
if(Math.Abs(array[i]- previous) > 1) // use appropriate jump
{
//logic
}
previous = array[i];
}
If you are looking for Linq solution, you could do this.
int previous = array[0];
array.FirstOrDefault(x=>
{
var retValue = Math.Abs(x- previous) > 1;
previous = x;
return retValue;
});
If you want to find all change-indexes in the most efficient way:
List<int> changeIndexes = new List<int>();
for(int i = 1; i < array.Length; i++)
if(array[i] != array[i-1])
changeIndexes.Add(i);
Linq is not the best tool when it comes to indexes.
If you want to find only the index where 1 changes to 3 modify the condition accordingly:
if(array[i] == 3 && array[i-1] == 1) ... // break the loop if you only want to find the first
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 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 9 years ago.
Improve this question
How to sort array without using sort method in C#
Visualization and Comparison of sorting algorithms in C#
example code
public IList BubbleSort(IList arrayToSort)
{
int n = arrayToSort.Count - 1;
for (int i = 0; i < n; i++)
{
for (int j = n; j > i; j--)
{
if (((IComparable)arrayToSort[j - 1]).CompareTo(arrayToSort[j]) > 0)
{
object temp = arrayToSort[j - 1];
arrayToSort[j - 1] = arrayToSort[j];
arrayToSort[j] = temp;
}
}
}
return arrayToSort;
}
above will surely helps you to understand the thing you want
By assigning value by value in alphabetical or whatever order.
Sorry, but I don't get your question. What is the actual problem you are trying to solve?
Use nested loop sorting methods such as bubble sort.
But why would you want to do such a thing?
Since the array implements IEnumerable<T>, you can use the OrderBy extention method on IEnumerable to sort it.
If your question is how to do it without using any built in functionality in the framework, I guess I will have to ask. Why??