Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im tring to make a programm that calculates average of all the posive numbers in the array, but it cant calulate the average.

<pre>  double count = 0;
            int i = 0;
            double[] arr1 = new double[20];

            for (i = 0; i < 20; i++)
            {

                Console.Write("Enter a number: ");
                var number = (Console.ReadLine());
                var total = number.Count(s => s > 0);
                if (number == "0") break;
                arr1[i] = int.Parse(number);


                if (arr1[i] > 0)
                {
                    count++;

                }



                Console.WriteLine($"Average of all positive numbers: {number/count}");
                Console.ReadKey();


What I have tried:

I have tried
Convert.ToInt32;
Posted
Updated 21-Oct-21 0:04am
v3
Comments
Richard MacCutchan 21-Oct-21 4:38am    
You are trying to mix integer and double values in the same variables. and at the end you are tryin g to divide a string by a number, which will never work.
George Swan 21-Oct-21 4:51am    
Here are some tips. Use a while (i<20) loop. Validate the user input with uint.TryParse. If that fails, inform the user and go round the loop again without increasing i. If the number is 0, break out of the loop. Else set arr1[i] = number and increase i. After the loop, check for an early exit by i<20 and exit if true after informing the user. Otherwise calculate the average by Linq or summing arr1 and dividing by 20. Convert to a double before dividing. You do not actually need an array, you can just keep a running total within the loop.

An average is simple to calculate, there's no need for Linq.

Your code:

0) you never terminate the 'for loop (posting error ?).

Keep it simple:

0) declare a type Double running-total variable outside the 'for loop: update it with each new user-entered value.


1) fill the array with user supplied values: decide if the user must enter a value for each element of the array-size you define, or if the user can quit at any time.


1a) use Double.TryParse [^] to get valid floating-point values.

2) when you exit the 'for loop: double average = runningtotal / arr1.Length;
 
Share this answer
 
You could create a method that returns the average of all positive numbers.
e.g.
C#
private static double CalculatePositiveAverage(IEnumerable<double> numbers)
{
    return numbers
        .Where(x => x > 0)
        .Average();
}
You must use the System.Linq namespace for it.

You wonder what "IEnumerable" is?
This is one of the interfaces implemented by the List class.
Arrays also implement this interface.

The given example will allow you to do the calculation for any collection that implements this interface.

It is also recommended to have a look at What is LINQ[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900