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.)
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 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 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()
{
}
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
Desired output:
1
121
12321
1234321
123454321
Code:
for (r = 1; num >= r; r++)
{
for (sp = num - r; sp >= 1; sp--)
Console.Write(" ");
for (c = 1; c <= r; c++)
Console.Write(c);
for (x = r - 1; x >= 1; x--)
Console.Write(x);
Console.Write("\n");
}
I'm trying this in c# I got the code in net. But I need to how it works exactly. Please some one explain how nested loops are works with Clear Explanation. Any help must be appreciated.
The outer loop iterates over the lines to print. The first inner loop prints the desired number of spaces in order to have the non-whitespace characters appear center aligned. The second inner loop prints the left half of the row, and finally the third inner loop prints the right half of the row.
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
I'm making a game that involves finding and clicking on a number (0-9) in a grid that randomizes each time you click on the correct one.
I want to get it so that when you click on the correct number, the grid randomizes again.
How would you do this?
Here's what it would kind of look like in the end:
I assume you're rendering an array of integers in order:
for (int i = 0; i < arrayOfNumbers.Length; i++ ) {
// rendering here
render(arrayOfNumbers[i]);
}
If thats the case.. just randomize the array after a successful click.. somewhat like this:
var rnd = new System.Random();
var arrayOfNumbers = Enumerable.Range(1, 9).OrderBy(r => rnd.Next()).ToArray();
Then you can just re-render (or let your game loop continue to render the array). Since the array has changed, your rendering will too.
Every time you detect a click on the correct number (I hope you know how to do this) you simply randomize the array of numbers you're displaying in your grid:
//Fisher-Yates algorithm
Random generator = new System.Random();
int len = array.Length;
while (len > 1)
{
len--;
int k = generator.Next(len + 1);
int temp = array[k];
array[k] = array[len];
array[len] = temp;
}
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??