Getting average values from one Array to another Array [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I’m no programmer but I’m learning C# to build a foreign exchange trading system and Arrays are being a struggle…
The problem I have is the following…
I have a one dimensional Array with, let’s say, 100 elements in it.
Now I need to build another one dimensional array with a 10 elements rolling average based on the first Array.
Said in another way, I need to take the elements from the first Array starting in i = 0 up to i = 9 and average them and save the average in a new array. Than move one step forward and take i = 1 up to i = 10 from the original Array and average them and save the result in the new Array….and so forth….in Excel this would be extremely easy….
My need to have the data in Arrays is because later I will need to compare the last 10 elements rolling average with historical data….
Please, can anyone build a sample code that I can work with?
Many thanks
Paulo

Maybe something like this could work... Did this on my mac in sublime text so you'll still have to work with. Should get the point though.
public class Foo
{
List<int> main = new List<int>(100);
List<int> rollingAverages = new List<int>(100);
public void Add(int score)
{
main.Add(score);
if(main.Count > 10)
{
int rollingAverage = AverageLast10();
rollingAverages.Add(rollingAverage);
}
}
public int AverageLast10()
{
int sum = 0;
for(int i = main.Count - 10; i < 10; i++)
{
sum += main[i];
}
return sum / 10;
}
}
Somewhere else in the code
Foo foo = new Foo();
foo.Add(94);
foo.Add(94);
...
yadda yadda yadda

Related

I need to subtract value from the array [closed]

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 1 year ago.
Improve this question
I need to substract given sum from array of elements. I.e. if given value is 15 and array starts with [10, 20,...] I need to substract 10 from the first elements (resulting with 0 for it) and the rest continue to subsequent elements - so second would be decreased by 5 (15 - 10), and the rest stay untouched.
int[] myNum = { 10, 20, 30, 40 };
if given value is 15 then I need to subtract value from the array. The new array will be
int[] myNum=[0,5,30,40]
I can easily create new array with all elements of the array decreased by the given number with basic for loop, but I don't know how to change that number based on how much I already substracted.
Your best bet is a for loop.
This will allow you to perform an action (I.E subtraction) on each 'element' of an array.
By using a return statement, you can return the new array that has had each element modified
EDIT As per #AlexeiLevenkov's comment, I have updated my answer to keep a count of the remaining subtraction.
Using this to test :
using System;
public class Program
{
public static void Main()
{
int[] array = new int[]{5,10,15,20,25,30,35};
array=SubtractArray(array,25);
Console.WriteLine("Output is:");
foreach(int v in array){
Console.WriteLine(v+", ");
}
}
public static int[] SubtractArray(int[] array , int subtraction){
for(int i=0; i< array.Length;i++){
if(subtraction>0){
int newValue=array[i]-subtraction;
if(newValue<1){
newValue=0;
subtraction=subtraction-array[i];
}
array[i]=newValue;
}
}
return array;
}
}
I don't know wath you said but I think it that :
The Arrays work 0,1,2,3,4,5,6
if I made a arrays like that
int[] _int = {10, 50, 30};
for go to the fist (10) and substract I need to do that
Sorry for my english I'm french
_int[0] -= 10;
becose it will select automatic the first (0)

Algorithm to generate all combinations of a specific size from a single set [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I am looking for solution in c# to generate combinations on given list of characters or word to perform dictionary attack on zip files. because we lost passwords file for those zip. Advantage is that we know possible words on it. Dictionary should contain all the combos of words that I choose. And all characters/words are small case only.
Example: Let say we have a set of chars:
Set A = {A,B,C}
A,B,C =3
AA,AB,AC
BA,BB,BC
CA,CB,CC =9
AAA,AAB,AAC,ABA,ABB,ABC,ACA,ACB,ACC
BAA,BAB,BAC,BBA,BBB,BBC,BCA,BCB,BCC
CAA,CAB,CAC,CBA,CBB,CBC,CCA,CCB,CCA = 27
TOTAL POSIBLE COMBINATION 39
from the list of words a single word/character may repeat maximum of 4 times. If any such alogrithm/logic available please suggest.
Here is a C# implementation using recursion:
static char[] A={'a','b','c'};
static int N = 3;
static void foo(string s)
{
if (s.Length == N)
{
Console.WriteLine(s);
return;
}
for (int i = 0; i < A.Length; i++)
{
string t = s;
t += A[i];
foo(t);
}
}
Demo
If you want to retrieve the values later, store the strings in a global array before returning from function foo().

Knuth's algorithm for solving MasterMind with 5 guesses [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I read knuth's algorithm in wikipedia and I wonder about the 3rd step. If I understand correct for each option (even if not removed in step 2), we calculate how many possible guesses would have removed for every ffedback. for that step we take minimum. after, we find the maximum on the minimums and take the code the max belong to it. that will be our second guess. Im not sure what can be done next because,each time we dont change the max on the minimums (or what is the minimum). In addition, how can I implement this step in c# for example. How can I procceed and how can I implement that step? What does the minimum on the feedbacks really mean?
You need a constant collection of all possible outcomes, a collection of the remaining alternatives, and a method that can compute the outcome given a guess and a solution.
First, model the relevant domain objects:
public class Outcome
{
public int White { get; set; }
public int Black { get; set; }
}
public class Combination
{
// however you like to model this
}
Then, create the method that checks the guess against the secret:
public static Outcome Check(Combination guess, Combination solution)
{
// your implementation
}
Now the algorithm is as follows:
Outcome[] outcomes = new[] { new Outcome { White = 0, Black = 0 },
new Outcome { White = 1, Black = 0 },
// ... all other possibilities
};
// assume we have some list of combinations
int min = Integer.MaxValue;
Combination minCombination = null;
foreach (var guess in combinations)
{
int max = 0;
foreach (var outcome in outcomes)
{
var count = 0;
foreach (var solution in combinations)
{
if (Check(guess, solution) == outcome)
count++;
}
if (count > max)
max = count;
}
if (max < min)
{
min = max;
minCombination = guess;
}
}
At the end of the loop, minCombination is your next guess.
EDIT I messed up min and max in the first version, that is fixed now. The inner count gives the number of remaining options, provided the chosen combination and the assumed outcome. We want the maximum over the outcomes (the worst possible result for the chosen combination is the one that leaves most options remaining). After that we want the minimum over the combinations (the best possible combination is the one that leaves least options remaining in the worst case).
If you like Linq, you could also write the algorithm as
combinations.MaxBy(guess =>
outcomes.Min(outcome =>
combinations.Count(solution =>
Check(guess, solution) == outcome)));
in which I used MaxBy from the MoreLinq project.

Grid of numbers that randomizes on click [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
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;
}

Inversing integers and printing it in console [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array of integers, which have to be converted into its inverses, so that, my program reads a series of integers from a user, fills an array with it, and then print it's inverses in a writeline. I had an idea to put inversed integers into double array, but still I don't know how to inverse (so that it looks like that - 1/N) it.
Finally, inversed integers should be printed in WriteLines.
You can use double inverse = 1.0 / number
If I understand correctly, this should work:
int arrayOfIntegers[] = <Your Array of Integers>;
foreach (input in arrayOfIntegers){
Console.WriteLine(1.0 / (double)in);
}
for(int i = 0; i < numbers.Length(); i++)
{
Console.WriteLine("1 / " + numbers[i].ToString());
}
or the foreach version:
foreach(int i in numbers)
{
Console.WriteLine("1 / " + i.ToString());
}
or to get cute:
foreach(int i in numbers)
{
Console.WriteLine("1 / {0} = {1}", i, 1.0 / i);
}
Of course this is a very basic exercise, I would like to introduce LINQ to you:
var output = yourArray.Select(x=> {
float f = 1f/x;
Console.Write(((decimal)f) + " ");
return f;
}).ToArray();
//This way you can still store the array while print all the inversed elements

Categories