1.11M Members

C# Average numbers?

 
0
 

Hi
double[] MyArray = new double[10];

I have 10 numbers and I get average of them.
But how can print out how many of the numbers that are smaller than average, and how many of the numbers that are larger than average?

 
0
 

If you include LINQ(using System.Linq;), it includes an 'Average' method for integer arrays to find the average of every element in the array. You can also extract specific elements that match specific criteria. This link gives a fairly good explanation.

 
0
 

Thanks Sir

I mean the users can specify an arbitrary number of integers. Then the program will calculate the average of the given numbers. At last, the program will print out how many of the numbers that are smaller than average, and how many of the numbers that are larger than average?
Actually I need to know how can print out how many of the numbers that are smaller than average, and how many of the numbers that are larger than average?

 
0
 

The code give me the minimum and maximam number

    int min = arrNumbers[0];
         int max = arrNumbers[0];
         int sum = 0;
         double average = 0;
         for (int i = 0; i < n; i++)
         {
            sum += arrNumbers[i];
            if (min > arrNumbers[i])
               min = arrNumbers[i];
            if (max < arrNumbers[i])
               max = arrNumbers[i];
         }

Bu I need to print out all nummber that are smaller and larger than average?

 
0
 

To figure out which information you want to print out, the link in my other message gives very easy to understand information with examples on how to program that.
To print out the information, to the screen, not to an actual printer. You can use a temporary pop-up called Message Box, which will display your information until the user clicks and OK button. You can also put a Text Box or a Label on your form in which you can display the information until you change it in your program. If you meant an actual printer you could use the Printer Dialog, which is in your toolbox. It will depend on what your requirements are. Information on how to use these is linked to each one.

 
0
 

Thanks
But I need the information print out in my textbox not message box?
Have you any idea about coden?

 
0
 

There's a link in my last message about Text Boxes. They are fairly straight forward basically TextBox1.Text=AnyString. Like any string variable if you want to add to the string, TextBox1.Text = TextBox1.Text + AnyString.

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] MyArray = new double[]{10,9,5,77,45,23,12,5,88,49};
            double wrecked = MyArray.Average();
            int lowerthanAv = 0, higherthanAv = 0;
            for (int i = 0; i < MyArray.Length; i++)
            {
                if (MyArray[i] < wrecked)
                    lowerthanAv++;
                else
                    higherthanAv++; // I do not take equal than av into acount here
            }
            Console.WriteLine("The average is: {0}", wrecked);
            Console.WriteLine("Number lower than average: {0}", lowerthanAv);
            Console.WriteLine("Number higher than average: {0}", higherthanAv);
            Console.ReadLine();

        }
    }
}

I think the above code does what you like to do.

 
0
 

Here you go sorry for the confusion I think this gives you what you want.

            int[] ArrNumbers = new int[] { 10, 9, 5, 77, 45, 23, 12, 5, 88, 49 };
            int min = ArrNumbers.Min();
            int max = ArrNumbers.Max();
            double average = ArrNumbers.Average();
            string higher = "", lower = "", equal = "";
            for (int i = 0; i < ArrNumbers.Length - 1; i++)
            {
                if (ArrNumbers[i] < average)
                {
                    lower = lower + Convert.ToString(ArrNumbers[i]) + " ";
                }
                else
                {
                    if (ArrNumbers[i] > average)
                    {
                        higher = higher + Convert.ToString(ArrNumbers[i]) + " ";
                    }
                    else
                    {
                        equal = equal + Convert.ToString(ArrNumbers[i]) + " ";
                    }
                }
            }
            textBox1.Lines = new string[] { "Minimum - " + min, "Maximum - " + max, "Average - " + average, "Lower - " + lower, "Higher - " + higher, "Equal - " + equal };

the output in the textbox is:

Minimum - 5
Maximum - 88
Average - 32.3
Lower - 10 9 5 23 12 5
Higher - 77 45 88
Equal -

 
0
 

"I think the above code does what you like to do."

Yes Sir,Thats I mean Thank you very much again

Question Answered as of 1 Year Ago by tinstaafl and ddanbe
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: