Question

In: Computer Science

Write a function in C# that takes an array of double as the parameter, and return...

Write a function in C# that takes an array of double as the parameter, and return the average

Solutions

Expert Solution

For this we would need 2 methods first to calculate the sum,

public double Sum(params double[] arr)
            {
               double result = 0.0;

               for(int i = 0; i < arr.Length; i++)
               {
                  result += arr[i];
               }

               return result;
            }

and another one to calculate the average using the sum method listed above.

  public double Average(params double[] arr)
        {
            double sum = Sum(arr);
            double result = (double)sum / arr.Length;
            return result;
        }

The full code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public double Sum(params double[] arr)
            {
               double result = 0.0;

               for(int i = 0; i < arr.Length; i++)
               {
                  result += arr[i];
               }

               return result;
            }
        public double Average(params double[] arr)
        {
            double sum = Sum(arr);
            double result = (double)sum / arr.Length;
            return result;
        }
        public static void Main(string[] args)
        {
            double[] values={1.0,2.0,3.0,2.3};
            Console.WriteLine("Hello, world!");
            Console.WriteLine(values.Average());
        }
    }
}

Related Solutions

C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
(C++) Write a function that takes as an input parameter an integer that will represent the...
(C++) Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number...
} 1. write a function that takes a string as parameter, return true if it’s a...
} 1. write a function that takes a string as parameter, return true if it’s a valid variable name, false otherwise. You can use keyword module’s  iskeyword() to determine is a string is keyword. import keyword keyword.iskeyword(var)   #returns true or false }2. write a function that returns the length of a string (without using len() function) }3. write a function that counts number of vowels in a string }4. write a function that checks if a word is palindrome PYTHON PROGRAMMING
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and...
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and use as stack to convert the integer to a its corresponding binary representation. Hint: divide the integer by 2. 2. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a...
Array & Function - Can I pass a single parameter to a function that takes two...
Array & Function - Can I pass a single parameter to a function that takes two arguments: data passed from: arrayOne(4); to function: string function (array[], int)
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT