We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,278 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C# Average numbers?

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?

3
Contributors
9
Replies
1 Day
Discussion Span
5 Months Ago
Last Updated
10
Views
Question
Answered
tony75
Junior Poster
155 posts since Mar 2011
Reputation Points: 20
Solved Threads: 4
Skill Endorsements: 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.

tinstaafl
Nearly a Posting Virtuoso
1,330 posts since Jun 2010
Reputation Points: 355
Solved Threads: 234
Skill Endorsements: 14

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?

tony75
Junior Poster
155 posts since Mar 2011
Reputation Points: 20
Solved Threads: 4
Skill Endorsements: 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?

tony75
Junior Poster
155 posts since Mar 2011
Reputation Points: 20
Solved Threads: 4
Skill Endorsements: 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.

tinstaafl
Nearly a Posting Virtuoso
1,330 posts since Jun 2010
Reputation Points: 355
Solved Threads: 234
Skill Endorsements: 14

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

tony75
Junior Poster
155 posts since Mar 2011
Reputation Points: 20
Solved Threads: 4
Skill Endorsements: 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.

tinstaafl
Nearly a Posting Virtuoso
1,330 posts since Jun 2010
Reputation Points: 355
Solved Threads: 234
Skill Endorsements: 14
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.

ddanbe
Industrious Poster
4,294 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 723
Skill Endorsements: 26

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 -

tinstaafl
Nearly a Posting Virtuoso
1,330 posts since Jun 2010
Reputation Points: 355
Solved Threads: 234
Skill Endorsements: 14

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

Yes Sir,Thats I mean Thank you very much again

tony75
Junior Poster
155 posts since Mar 2011
Reputation Points: 20
Solved Threads: 4
Skill Endorsements: 0
Question Answered as of 5 Months Ago by tinstaafl and ddanbe

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0850 seconds using 2.74MB