Q:

Write a C# Sharp program to find the cumulative sum of an array of number

0

Write a C# Sharp program to find the cumulative sum of an array of number

A cumulative sum is a sequence of partial sums of a given sequence. For example, the cumulative sums of the sequence {x, y, z,...}, are x , x+y , x+y+z

Sample Output:

Orginal Array elements: 
1 3 4 5 6 7 
Cumulative sum of the said array elements:
1 4 8 13 19 26 
Orginal Array elements: 
1.2 -3 4.1 6 -5.47 
Cumulative sum of the said array elements:
1.2 -1.8 2.3 8.3 2.83

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;

namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] nums = {1, 3, 4, 5, 6, 7};
            Console.WriteLine("\nOrginal Array elements: ");
            foreach (var item in nums)
            {
                Console.Write(item.ToString()+" ");
            }
            Console.WriteLine("\nCumulative sum of the said array elements:");
            double[] result = test(nums);
            foreach (var item in result)
            {
                Console.Write(item.ToString()+" ");
            }
            double[] nums1 = { 1.2, -3, 4.1, 6, -5.47 };
            Console.WriteLine("\nOrginal Array elements: ");
            foreach (var item in nums1)
            {
                Console.Write(item.ToString() + " ");
            }
            Console.WriteLine("\nCumulative sum of the said array elements:");
            double[] result1 = test(nums1);
            foreach (var item in result1)
            {
                Console.Write(item.ToString() + " ");
            }
        }

        public static double[] test(double[] nums)
        {
            for (int i = 1; i < nums.Length; i++)
            {
                nums[i] = nums[i] + nums[i - 1];
            }

            return nums;
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now