Hello, my current assignment states this: "Modify the console application you created in HW1 in the following manner. Instead of using five Console.ReadLine() statements to read the user’s input into the five variables, use one Console.ReadLine() statement that’s inside a loop that executes five times. The rest of your code works like HW1 part 1 and 2 (i.e. compute the average and the variance of the five integers). Please note, to get all 70 points you MUST accurately compute both the mean and the variance. Use the following equation for the variance."

The last assignment from HW1 was pretty simple, we just had to heave the user input 5 variables, and it figured out the average and variance.

Here is what I have come up with so far:

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

namespace Assignment_1_ITM_225
{
    class Program
    {
        static void Main(string[] args)
        {
            int Integer1 = 0;
            int Integer2 = 0;
            int Integer3 = 0;
            int Integer4 = 0;
            int Integer5 = 0;
            int intValue = 0;
            int Integers = 0;
            float Average = 0;
            double Variance = 0;

            Console.WriteLine("Please enter 5 integers between 10 and 50: ");
            if (intValue < 10 || intValue > 50)
            {
                Console.WriteLine("{0} is not an integer between 10 and 50", intValue);
            }
            while (Integers < 5)
            {

            }
            Console.ReadKey();
            Integer1 = Convert.ToInt32(Console.ReadLine());

            Integer2 = Convert.ToInt32(Console.ReadLine());
            Integer3 = Convert.ToInt32(Console.ReadLine());
            Integer4 = Convert.ToInt32(Console.ReadLine());
            Integer5 = Convert.ToInt32(Console.ReadLine());

            Average = ((Integer1 + Integer2 + Integer3 + Integer4 + Integer5) / 5.0f);
            Console.WriteLine("The average of the integers is {0}", Average);
            Console.ReadKey();

            Variance = ((Integer1 - Average) * (Integer1 - Average) + (Integer2 - Average) * (Integer2 - Average) + (Integer3 - Average) * (Integer3 - Average) + (Integer4 - Average) * (Integer4 - Average) + (Integer5 - Average) * (Integer5 - Average)) / 4;
            Console.WriteLine("The variance of the integers is {0}", Variance);
            Console.ReadKey();





        }
    }
}

I'm just not sure what I'm supposed to do with the integers to make it into a loop, also, the if statement is a separate part of the assignment to make sure the variables are between 10 and 50. Any help would be greatly appreciated on how to make it so the integers are a loop.

IntValue is not defined. Define it.
Start your loop with while (Intvalue < ...)
Do a ReadLine.
Put your if statement next. Test Intvalue as you did.
Augment Intvalue only if between 10 and 50.
Are 10 and 50 inclusive or not?
Succes!

I would suggest:

Use an array to accept your inputs(int[] inputs = new int[5];).

A for loop with intValue accepting the Console.ReadLine().

Use a while loop inside the for loop to validate the input(while (intValue < 10 || intValue > 50)). Don't forget to re-initialize intValue after each successful input.

When you have the inputs, another for loop to calculate the variance will look nicer than a long run on line(Variance += (inputs[i]-Average)*(inputs[i]-Average);) then a simple Variance /= 4;, after the loop is finished. You could also use this loop to display your inputs.

Edited 3 Years Ago by tinstaafl

Thanks guys for the help, When you say define intValue what exactly do you mean by that? Also, what do you mean by augment intvalue? Thank you, I'm really new to programming and my teacher doesn't offer much help.

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

namespace Assignment_1_ITM_225
{
    class Program
    {
        static void Main(string[] args)
        {            
            int intValue = 0;
            float Average = 0;
            double Variance = 0;

            Console.WriteLine("Please enter 5 integers between 10 and 50: ");

            while (intValue > 5)
            {
                Console.ReadLine();
            }
            if (intValue < 10 || intValue > 50)
            {
                Console.WriteLine("{0} is not an integer between 10 and 50", intValue);
            }

            Console.ReadKey();
            intValue = Convert.ToInt32(Console.ReadLine());

            Average = ((Integer1 + Integer2 + Integer3 + Integer4 + Integer5) / 5.0f);
            Console.WriteLine("The average of the integers is {0}", Average);
            Console.ReadKey();

            Variance = ((Integer1 - Average) * (Integer1 - Average) + (Integer2 - Average) * (Integer2 - Average) + (Integer3 - Average) * (Integer3 - Average) + (Integer4 - Average) * (Integer4 - Average) + (Integer5 - Average) * (Integer5 - Average)) / 4;
            Console.WriteLine("The variance of the integers is {0}", Variance);
            Console.ReadKey();

        }
    }
}

That's what I have so far, I'm not sure how to make a loop that will let the user input 5 variables. and then how to make it so I can do the variance/average later.

Well, you defined intValue on line 13 of your most recent code! You defined the variable intValue, you gave it a name(intValue), a type(integer) and a value(0).
Line 19 should be: while (intValue < 5)
Put your if statement IN the while loop.
Do you know what an array is? It would certainly make things easier if you did.

I know what it is, I just don't know how to make one in C#. Thanks for the help. I'll post my progress in a few.

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

namespace Assignment_1_ITM_225
{
    class Program
    {
        static void Main(string[] args)
        {            
            int intValue = 0;
            float Average = 0;
            double Variance = 0;

            Console.WriteLine("Please enter 5 integers between 10 and 50: ");

            while (intValue < 5)
            { 
                if (intValue < 10 || intValue > 50)
            {
                Console.WriteLine("{0} is not an integer between 10 and 50", intValue);
            }
                Console.ReadLine();
            }
            Console.ReadKey();
            intValue = Convert.ToInt32(Console.ReadLine());



            //Average = ((Integer1 + Integer2 + Integer3 + Integer4 + Integer5) / 5.0f);
            //Console.WriteLine("The average of the integers is {0}", Average);
            //Console.ReadKey();

            //Variance = ((Integer1 - Average) * (Integer1 - Average) + (Integer2 - Average) * (Integer2 - Average) + (Integer3 - Average) * (Integer3 - Average) + (Integer4 - Average) * (Integer4 - Average) + (Integer5 - Average) * (Integer5 - Average)) / 4;
            //Console.WriteLine("The variance of the integers is {0}", Variance);
            //Console.ReadKey();

        }
    }
}

So here is where I am, how do I get it so the average/variance will be all 5 inputs from the intValue?

try this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_1_ITM_225
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Inputs = new int[5];
            int intValue = 0;
            float Average = 0;
            double Variance = 0;
            for (int i = 0; i < Inputs.Length; i++)
            {
                while (intValue < 10 || intValue > 50)
                {
                    Console.Clear();
                    Console.WriteLine("Please enter {0} more integers between 10 and 50: ",Inputs.Length-i);
                    intValue = Convert.ToInt32(Console.ReadLine()); 
                }
                Inputs[i]=intValue;
                intValue = 0;
            }
            Average = (float)(Inputs.Sum() / 5.0f);
            Console.Clear();
            for (int i = 0; i < Inputs.Length; i++)
            {
                Console.WriteLine(Inputs[i]);
                Variance +=(Inputs[i]-Average)*(Inputs[i]-Average);
            }
            Console.WriteLine("The average of the integers is {0}\n", Average);
            Variance /= 4;
            Console.WriteLine("The variance of the integers is {0}\n", Variance);
            Console.ReadKey();
        }
    }
}

That worked perfectly. Thanks a lot. I'll try remaking it with different variables and stuff to see if I understand it.

castajiz2? Are you in Panic? DON'T!!! Everything always ends well, in the end . . .
Just answer 3 simple questions first.
Do you know wath an array is?
Do you know some statistical formula's?
What equation for calculating the variance was given to you?

Edited 3 Years Ago by ddanbe: omission

I know what an array is. I just didn't know how to make one in C#. I don't know many statistical formula's, and the equation was (Since it won't let me upload the image) [(1st int - average)^2 + (2nd int - average)^2 + (3rd int - average)^2 + (4th int - average)^2 + (5th in - average)^2] / 4

With your formula it is very hard to go without arrays.
If you may use them find some tutorial on the web and study a bit how they are declared etc. Example Here
Try to figure it out and change your 5 integers into an array. See ya.

When I run this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_1_ITM_225
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Inputs = new int[5];
            int intValue = 0;
            float Average = 0;
            double Variance = 0;
            for (int i = 0; i < Inputs.Length; i++)
            {
                while (intValue < 10 || intValue > 50)
                {
                    Console.Clear();
                    Console.WriteLine("Please enter {0} more integers between 10 and 50: ",Inputs.Length-i);
                    intValue = Convert.ToInt32(Console.ReadLine()); 
                }
                Inputs[i]=intValue;
                intValue = 0;
            }
            Average = (float)(Inputs.Sum() / 5.0f);
            Console.Clear();
            for (int i = 0; i < Inputs.Length; i++)
            {
                Console.WriteLine(Inputs[i]);
                Variance +=(Inputs[i]-Average)*(Inputs[i]-Average);
            }
            Console.WriteLine("The average of the integers is {0}\n", Average);
            Variance /= 4;
            Console.WriteLine("The variance of the integers is {0}\n", Variance);
            Console.ReadKey();
        }
    }
}

It doesn't work. It keeps saying input string (line 22) is not in the correct format. What do I do?

The input must be digits only. Try this:

 while (intValue < 10 || intValue > 50)
{
    Console.Clear();
    Console.WriteLine("Please enter {0} more integers between 10 and 50: ",Inputs.Length-i);
    if(!Int.TryParse(Console.ReadLine(),out intValue)       
        intValue = 0;
}

Now it won't proceed until the input is only digits.

Edited 3 Years Ago by tinstaafl

Now when I type in the 5 number between 10 and 15 and press enter it erases the numbers I typed in and keeps repeating.

Because of the Console.Clear(); I should remove the line.
Here is a version without the use of arrays.

using System;

namespace MeanVar
{
    class Program
    {
        static void Main(string[] args)
        {
            // look mama, no arrays!
            const float N = 5; //elements in sample
            float Mean = 0;
            double Variance = 0;
            int Sum = 0;
            int SumSquare = 0;
            int i = 0;
            int input = 0;

            while (i < N)
            {
                input = Convert.ToInt32(Console.ReadLine());
                if (input < 10 || input > 50)
                {
                    Console.WriteLine("{0} is not an integer between 10 and 50", input);
                }
                else
                {
                    Sum += input;
                    SumSquare += input * input;
                    i++;
                }
            }

            Mean = Sum / N;
            Variance = (SumSquare - Sum * Sum / N) / (N - 1);
            Console.WriteLine("The average of the integers is {0}", Mean);
            Console.WriteLine("The variance of the integers is {0}", Variance);
            Console.ReadKey();
        }
    }
}

My original code works fine, just pay attention, enter one integer at a time, not all 5 all at once.

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