0

I have an empty 2D array called stockGains which I am trying to append with values of gains (the value of nextPrice/currentPrice) but I cannot seem to find an appropriate operator or method in C# to be able to append to my 2D array and complete my loop? Currently, I have incorrectly used .Add to give an example. The 2D array stockGains should be dynamically sized.

double[,] stockGains = {};
   
foreach (double currentPrice in pricesMonth1AsList){

    // divide the nextPrice by currentPrice
    double gains = ((currentPrice+1)/currentPrice);

    // append the result to stockGains
    stockGains.Add(gains);

    // do this on repeat for every day of the month until the end of pricesMonth1Aslist
} 



I have stored historical price data to be iterated over as a List<double> called pricesMonth1AsList which has each currentPrice stored as doubles sequentially from day 1 to day 30 (you can consider the index position of the list to be (day of the month - 1). nextPrice is the value of price the day after (which i have represented as currentPrice+1 in index position) example of the sample data is here:


20.35, 30.5, 40.2, 50 represents Day 1 [0], Day 2 [1], Day 3 [2], Day 4 [3]
 etc

For example the first gain to be calculated would be 30.5/20.35 and then this value is to be stored as the first value in index 0 of the 2D array, the second value would be 40.2/20.35, the third would be 50/20.35 also stored in index 0 of stockGains until the last day of the month. Then repeats the process again but from day 2 onwards i.e 40.2/30.5, 50/30.5 etc gains will be stored in index 1 of stockGains.

The end goal is to find the single maximum gains value in the 2D array at the end.


Stefan
  • 17,448
  • 11
  • 60
  • 79
loco
  • 321
  • 1
  • 4
  • 18
  • Does this answer your question? [Adding values to a C# array](https://stackoverflow.com/questions/202813/adding-values-to-a-c-sharp-array) – Charlieface Mar 20 '21 at 21:06
  • Hi, did any of this helped you? – Stefan Mar 21 '21 at 13:14
  • To address the dimensions of the 2 dimensional array, you would address it as: (stockGains[0,0] for the first value, then, stockGains[0,1] for the second value of the first stockGains index. For each subsequent index it would be [1,0],[1,1],...etc. But you have to copy the arrays back and forward to add them. A tuple might be a better way to handle the values. – nocturns2 Mar 21 '21 at 20:36

1 Answers1

2

I am not sure if this answers your question; and I adjusted your loop to be able to access the previous element, that's why I am using for.

I believe this will be easier with a Tuple<Tx,Ty> (this has my preference)

Create a List<> out of it, and you'll have an Add:

var pricesMonth1AsList = new List<double>()
{
    0,1,2,3,4,5
};

//init
var list2D = new List<Tuple<double,double>>();
for (int i = 1; i < pricesMonth1AsList.Count; i++) {
    //calculate your values

    //since I did not understand the calculation,
    //here's just a dummy one
    var currentValue = pricesMonth1AsList[i];
    var previousValue = pricesMonth1AsList[i-1];
    
    double a = previousValue/currentValue;
    double b = i;

    //and add it as such
    list2D.Add(new Tuple<double,double>(a,b));
}

foreach(var item in list2D)
{
    Console.WriteLine($"{item.Item1} - {item.Item2}");  
}

Output:

0 - 1
0.5 - 2
0.6666666666666666 - 3
0.75 - 4
0.8 - 5
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • Hi, since it's unclear to me what calculation you ant to do, I'll leave it up to you. I provided a way to access the previous items in your array and included a way to modify a 2D array with a List of Tuple's. I hope it helps. If not, I'll delete my answer. – Stefan Mar 21 '21 at 20:02
  • Hi Stefan, thanks for your response though I believe you misunderstand and your data set is not the best example either as 1/0 will throw an error. To make it clearer using your example pricesMonth1AsList data the process within the for loop should be: Taking day 3 as an example this would be: 2/1 ----> 2 (append to stockGains) 3/1 ----> 3 (append to stockGains) 4/1 ----> 4 (append to stockGains) etc etc. 

Basically I want to divide the next days price by the day before to calculate the possible gain for every price on every day in the month. – loco Mar 21 '21 at 20:03
  • 1
    Hi Stefan, i've not had a chance to try your method yet. Please see my latest comment and i will incorporate your suggestions this evening. Thank you. – loco Mar 21 '21 at 20:04