I
I
Ivan Vyrov2018-06-19 19:07:22
C++ / C#
Ivan Vyrov, 2018-06-19 19:07:22

How to implement a method that would return the sum of a List?

Good afternoon!
Please tell me in which direction to dig to implement the following task:
There is a class:

internal class DataDevice
{
  public int Rate { get; set; }
  public int[] Indication { get; set; }
  public double[] Price { get; set; }
}
internal DataDevice(int rate, int[] indication, double[] price)
{
  Rate = rate;
  Indication = indication;
  Price = price;
}

You need to write a method that would return the sum of all List entries, for example:
List<DataDevice> s = new List<DataDevice> { 
new DataDevice(2, new int[2] { 1025, 1145 }, new double[2] { 4.05, 84.05 }) 
new DataDevice(2, new int[2] { 1126, 1214 }, new double[2] { 4.05, 84.05 }) 
new DataDevice(2, new int[2] { 1265, 1351 }, new double[2] { 4.05, 84.05 }) 
};
Double summ = s.TotalAmount();

TotalAmount() will have to execute line by line the sum of all List entries (according to a certain formula) and get 18286.3 at the output
Question:
Tell me what material to read on this topic.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2018-06-19
@kttotto

Judging by your comments, I can assume such a decision

public double TotalAmount(List<DataDevice> data)
{
  double sum = 0;;
  for(var i = 1; i < data.Count; data++)
  {
    sum  += (data[i].Indication[0] - data[i-1].Indication[0]) * data[i].Price[0] + (data[i].Indication[1] - data[i-1].Indication[1]) * data[i].Price[1];
    tempValue.Add(sumItem);
  }
  return sum;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question