I'm new to c#.

How do I add five numbers to each other and indentify the highest and lowest value together with the average value of this five numbers?


Please give me something to begin with (or a whole example of code to study).

Have a nice day.

Thank you.

Hi mate...the easiest way would be to make an array with your values...

then declare another variable that will then hold the max number out of the numbers in your array.

for example....

//initilize your array of numbers...
int[] myArray = {23, 58, 20, 19, 38, 12};

//now make your new variable to hold the max number...

int maxNum = myArray.Max();

//maxNum will be given the value 58..

Sorry did not read your question properly....

you can do the above but instead of

myArray.Max();

//do

myArray.Min();

as for the average of the numbers in the Array just do

//declare a variable to hold the average

double average = myArray.Average;

//need to use a variable declared as a double for the average...

as for the adding the numbers together... use a simple for loop..

//declare variable called total outside of loop give it value 0
int total = 0;

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

hope that helps mate....

Sorry did not read your question properly....

you can do the above but instead of

myArray.Max();

//do

myArray.Min();

as for the average of the numbers in the Array just do

//declare a variable to hold the average

double average = myArray.Average;

//need to use a variable declared as a double for the average...

as for the adding the numbers together... use a simple for loop..

//declare variable called total outside of loop give it value 0
int total = 0;

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

hope that helps mate....

Thank you for your time.

This my code so far:

private static void ReadInt(int nr1, int nr2, int nr3, int nr4, int nr5)
        {

            int Number = 0;
            int counter = 0; 
            
            Console.WriteLine("Write five numbers:");

            for (int i = 1; i <= 5; i++)
            {

                //Five numbers                
                Number += int.Parse(Console.ReadLine()); //adding each number                                              
                counter++; //Counter            }
            double avg = Number / 5;
            Console.WriteLine("Average is {0}:", avg);
            
        }

I need to return average, highest value and lowest value of the five numbers.

Edited 5 Years Ago by Peter1: n/a

Class TestScoresOfFiveNumbers

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

namespace experiment
{
    class TestScoresOfFiveNumbers
    {
        //field
        private int _score1;
        private int _score2;
        private int _score3;
        private int _score4;
        private int _score5;

        //Construct
        public TestScoresOfFiveNumbers()
        {

        }

        public double Average
        {
            get
            {
                return (Score1 + Score2 + Score3 + Score4 + Score5) / 5;
            }
        }

        public int Highest 
        { 
            get
            {
                return value; //wrong
            }

        }
        public int Lowest { 
        
         get
            {
                return value; //wrong
            }
        
        } 

        //Prop
        public int Score1 {
            get
            {
                return _score1;
            }
            set
            {
                _score1 = value;
            }

        }

        public int Score2 {
            get
            {
                return _score2;
            }
            set
            {
                _score2 = value;
            }
        }
        public int Score3 {
            get
            {
                return _score3;
            }
            set
            {
                _score3 = value;
            }
        }

        public int Score4
        {
            get
            {
                return _score4;
            }
            set
            {
                _score4 = value;
            }
        }

        public int Score5
        {
            get
            {
                return _score5;
            }
            set
            {
                _score5 = value;
            }
        }

       
        
    }
}

Program

As seen before.

I want to get five numbers, process them, return max and min and average.

Edited 5 Years Ago by Peter1: n/a

your best bet would be to do some reading on arrays mate...its by far the easiest way to go about it...they look daunting at first..but they are fairly simple....

you can do it the way you are doing it above...but a bit further in...it would be easier to do it this way...

//declare array with space for 5 nums
int[] num = new int[5];

            for (int i = 0; i < num.Length; i++)
            {

                Console.Write("Enter Number");
                num[i] = Convert.ToInt32(Console.ReadLine());
            }
            int total = 0;
            //for loop to add each number in array
            for (int i = 0; i < num.Length; i++)
            {
                total += num[i];
            }

            Console.WriteLine("Total is {0}",total);//output total of numbers in array
             //output max num in array
            Console.WriteLine("Max num is {0}",num.Max());
             //output min num in array
            Console.WriteLine("Min num is {0}",num.Min());
           //output average of nums in array
            Console.WriteLine("Average of numbers is {0}",num.Average());
            Console.ReadLine()

try that mate...step through it using F11 and you should see whats going on...

oh right im with you...do you have to do it like that...is it for an assignment or something?

Thank you for your help. I will read on Arrays and try your suggestion.

No assignment, just trying to learn c#.

Have a nice day.

Perhaps you want something like this:

class Program
    {
        static void Main(string[] args)
        {
            int max = 0;           
            int[] Numbers = { 2, 158, 33, 42, 38, 1001, 3 };

            max = Max(Numbers);
            Console.WriteLine("An maximum of the numbers is {0}", max);
            Console.ReadLine();
        }

        static int Max(int[] N)
        {
            int maximum = int.MinValue; // set maximum to the lowest value
            for (int i = 0; i < N.Length; i++)
            {
                // set maximum if a higher value is found in array
                if (N[i] > maximum) maximum = N[i];
            }
            return maximum;
        }
    }

Do the exact opposite for minimum.
And an average of integers is most likely to be of type double.

Perhaps you want something like this:

class Program
    {
        static void Main(string[] args)
        {
            int max = 0;           
            int[] Numbers = { 2, 158, 33, 42, 38, 1001, 3 };

            max = Max(Numbers);
            Console.WriteLine("An maximum of the numbers is {0}", max);
            Console.ReadLine();
        }

        static int Max(int[] N)
        {
            int maximum = int.MinValue; // set maximum to the lowest value
            for (int i = 0; i < N.Length; i++)
            {
                // set maximum if a higher value is found in array
                if (N[i] > maximum) maximum = N[i];
            }
            return maximum;
        }
    }

Do the exact opposite for minimum.
And an average of integers is most likely to be of type double.

Thanks for the help.

If I don't want to use arrays, but classes and objects (see my code posted earlier in this thread), can you please give me some advice?

Well, I hoped my answer would put you on the way.
Let the outcome of the properties of your class depend on some private functions in your class. Happy computing :)

Well, I hoped my answer would put you on the way.
Let the outcome of the properties of your class depend on some private functions in your class. Happy computing :)

Thank you for your answer.

Is it possible for you to show me an example?

How do I let the outcome of the properties in my class depend of some private functions?

My code right now.

I want to combine and move it to the class TestScores.

static void Main(string[] args)
        {
            int max = 0;
            int min = 0;
            double avg = 0;
            int[] Numbers = { 1, 2, 3, 4, 5 };

            max = Max(Numbers);
            min = Min(Numbers);
            avg = Numbers.Average();
            Console.WriteLine("An maximum of the numbers is {0}", max);
            Console.WriteLine("An minium of the numbers is {0}", min);
            Console.WriteLine("An average of the numbers is {0}", avg);
            Console.ReadLine();
        }

        static int Max(int[] N)
        {
            int maximum = int.MinValue; // set maximum to the lowest value
            for (int i = 0; i < N.Length; i++)
            {
                // set maximum if a higher value is found in array
                if (N[i] > maximum) maximum = N[i];
            }
            return maximum;
        }

        static int Min(int[] N)
        {
            int minimum = int.MaxValue; // set minimum to the lowest value
            for (int i = 0; i < N.Length; i++)
            {
                // set minimum if a lower value is found in array
                if (N[i] < minimum) minimum = N[i];
            }
            return minimum;
        }

Well what about this:

class Program
    {
        static void Main(string[] args)
        {
            int max = 0;
            TestScores test = new TestScores();

            max = test.Highest;
            Console.WriteLine("The maximum of the numbers is {0}", max);
            Console.ReadLine();
        }

        class TestScores
        {
            private int[] Numbers = { 2, 158, 33, 42, 38, 1001, 3 };


            public TestScores()
            {
            }

            public int Highest         
            {             
                get            
                {
                    return Max(Numbers);//value; //wrong            
                }         
            }

            private int Max(int[] N)
            {
                int maximum = int.MinValue; // set maximum to the lowest value
                for (int i = 0; i < N.Length; i++)
                {
                    // set maximum if a higher value is found in array
                    if (N[i] > maximum) maximum = N[i];
                }
                return maximum;
            }
        }
    }

Well what about this:

class Program
    {
        static void Main(string[] args)
        {
            int max = 0;
            TestScores test = new TestScores();

            max = test.Highest;
            Console.WriteLine("The maximum of the numbers is {0}", max);
            Console.ReadLine();
        }

        class TestScores
        {
            private int[] Numbers = { 2, 158, 33, 42, 38, 1001, 3 };


            public TestScores()
            {
            }

            public int Highest         
            {             
                get            
                {
                    return Max(Numbers);//value; //wrong            
                }         
            }

            private int Max(int[] N)
            {
                int maximum = int.MinValue; // set maximum to the lowest value
                for (int i = 0; i < N.Length; i++)
                {
                    // set maximum if a higher value is found in array
                    if (N[i] > maximum) maximum = N[i];
                }
                return maximum;
            }
        }
    }

Thank you very much. It helped me a lot.

If I want to use, ReadInt (see below) to capture user input and store them into _score1, _score2 and _score3, is that possilbe?

My code so far:

Program.cs

class Program
    {
        static void Main(string[] args)
        {
            int max = 0;
            int min = 0;
            double avg = 0;
            TestScores testscores = new TestScores();

            max = testscores.Highest;
            min = testscores.Lowest;
            avg = testscores.Average;
            Console.WriteLine("The maximum of the numbers is {0}", max);
            Console.WriteLine("An minimum of the numbers is {0}", min);
            Console.WriteLine("An average of the numbers is {0}", avg);
            Console.ReadLine();
        }


        private static void ReadInt(int score1, int score2, int score3)
        {

            int[] score = new int[3];

            Console.WriteLine("Enter three numbers:");

            for (int i = 0; i < score.Length; i++)
            {
                score[i] = Convert.ToInt32(Console.ReadLine());


            }
            int total = 0;
            
            //for loop to add each number in array
            for (int i = 0; i < score.Length; i++)
            {
                total += score[i];
            }


            
        }
    }

class Testscores

class TestScores
    {
        private int[] Numbers = { 5, 15, 20, 40, 80 };

        //fields
        private int _score1;
        private int _score2;
        private int _score3;

        public int Score1
        {
            get
            {
                return _score1;
            }
            set
            {
                _score1 = value;
            }

        }
        public int Score2
        {
            get
            {
                return _score2;
            }
            set
            {
                _score2 = value;
            }
        }
        public int Score3
        {
            get
            {
                return _score3;
            }
            set
            {
                _score3 = value;
            }
        }
        
            public TestScores()
            {
                //empty
            }

            public int Highest         
            {             
                get            
                {
                    return Max(Numbers);//value;            
                }         
            }

            public int Lowest
            {
                get
                {
                    return Min(Numbers);//value;            
                }
            }

            public double Average
            {
                get
                {
                    return Numbers.Average();
                }
            }

            private int Max(int[] N)
            {
                int maximum = int.MinValue; // set maximum to the lowest value
                for (int i = 0; i < N.Length; i++)
                {
                    // set maximum if a higher value is found in array
                    if (N[i] > maximum) maximum = N[i];
                }
                return maximum;
            }

            static int Min(int[] N)
            {
                int minimum = int.MaxValue; // set minimum to the lowest value
                for (int i = 0; i < N.Length; i++)
                {
                    // set minimum if a lower value is found in array
                    if (N[i] < minimum) minimum = N[i];
                }
                return minimum;
            }
        }

Edited 5 Years Ago by Peter1: n/a

Whenever you encounter a situation where you have to use var1, var2, var3 etc., consider using an array. Do do not use score1, score2 etc. but put them in an array.

I'm new to c#.

How do I add five numbers to each other and indentify the highest and lowest value together with the average value of this five numbers?


Please give me something to begin with (or a whole example of code to study).

Have a nice day.

Thank you.

public void AskForFiveIntegers()
{
    try
    {
        int[] array = new int[5];

        for (int i = 1; i <= 5; i++)
        {
            Console.Write("input value ({0}): ", i.ToString());
            array[i - 1] = Int32.Parse(Console.ReadLine());
        }

        int min = array[0],
            max = array[0];

        for (int i = 0; i < array.Length; i++)
        {
            if (min > array[i])
            {
                min = array[i];
            }

            if (max < array[i])
            {
                max = array[i];
            }
        }

        IEnumerable<int> enumeration = array;

        double avg = enumeration.Average();

        Console.WriteLine("Min: {0}\r\nMax: {1}\r\nAvg: {2}", min, max, avg);
    }
    catch (FormatException fe)
    {
        Console.WriteLine("..invalid input");
    }
}

Edited 5 Years Ago by iconoclazt: n/a

Comments
Well done.
This article has been dead for over six months. Start a new discussion instead.