Answer to Question #349741 in C# for Vish

Question #349741

Problem Statement: Find the average of an array using for and for-each loop

Given a positive integers array find the average of the numbers in that array using for-each loop

This exercise contains a class named NumberAverage with the following methods:

 +FindAverage(int[]) : String  
     - Should take positive integer array as input and return a String with the average calculated
     - The average of the array should be calculated by using a for-each loop 
     - Should send error message if all value in array are not positive
     - Should send error message if array is empty
1
Expert's answer
2022-06-10T18:34:08-0400
    class NumberAverage
    {
        public static string FindAverage(int[] massive)
        {
            int SumOfNumbersInArray = 0;
            if(massive.Length == 0)
            {
                return "Error. Massive length must have at least 1 number";
            }

            foreach(int i in massive)
            {
                if (i < 0)
                {
                    return "Error. Massive must have only positive numbers";
                }
                SumOfNumbersInArray += i;
            }

            return $"{SumOfNumbersInArray / massive.Length}";
        }
    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS