In this question, you\'ll modify an array of integers to make each element the s
ID: 1810627 • Letter: I
Question
In this question, you'll modify an array of integers to make each element the sum of all the previous elements added to itself. Declare an array of 10 integers called 'array'. Fill it with the values 1 through 10, from the smallest index to the largest index. Next, make every element the sum of all the previous elements in the array added to itself. That is, array [0] is not changed. The element array[l] becomes array[l] + array[0]. The element array[2] becomes array[2]+array[l]+array[0]. Etc. Your code should be loop-based. The output below shows the array before and after modification (the output doesn't show all the values, your program should show them all). Provide that output that you obtained.Explanation / Answer
public static IEnumerable CumulativeSum(this IEnumerable sequence) { double sum = 0; foreach(var item in sequence) { sum += item; yield return sum; } } textBox_f.Text .Split(new char[]{','}) .Select(s => double.Parse(s)) .CumulativeSum() .ToArray(); public static double? MyParseDouble(this string s) { double d; if (double.TryParse(s, out d)) return d; return null; } public static IEnumerable CumulativeSum(this IEnumerable sequence) { double? sum = 0; foreach(var item in sequence) { sum += item; yield return sum; } } ... textBox_f.Text .Split(new char[]{','}) .Select(s => s.MyParseDouble()) .CumulativeSum() .ToArray();Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.