Click here to Skip to main content
14,272,096 members
Rate this:
Please Sign up or sign in to vote.
See more:
//You only have to update the 2 here and the rest of the code will function based on the size you set this to
double[] myLottoNums = new double[6];

double sum = 0;
double mean = 0;
double upperVariable = 0;


Console.WriteLine("Please enter numbers: ");

for (int i = 0; i < myLottoNums.Count(); i++)
{
    Console.Write("Number {0}: ", i + 1);
    myLottoNums[i] = double.Parse(Console.ReadLine());
    sum = myLottoNums[i] + sum ;

    if (myLottoNums[i] < 10)
    {
        Console.WriteLine("Sorry that is too low");
    }


}

//Calculate the mean after totalling up the sum
mean = sum / myLottoNums.Count();


//Loop back through our numbers to calculate the upper of each and total it to the upperVariable.
foreach (double d in myLottoNums)
{
    Console.WriteLine("  {0}", d);
    //Console.WriteLine(d.ToString() + " - " + mean.ToString() + " = (" + (d - mean).ToString() + ")");
    // Calculate the value of d by squaring it, then adding it to upperVariable which initializes at 0 but once the first calculation is made, it keeps adding
    upperVariable = (d - mean) * (d - mean) + upperVariable;
}

Console.WriteLine("Thank you. The numbers are: ");

foreach (double x in myLottoNums)
{
    Console.WriteLine("{0}", x);
}
int divisor = myLottoNums.Count() - 1;

Console.WriteLine("The sum is {0}", sum);
Console.WriteLine("The mean is {0}", mean);
Console.WriteLine("The standard deviation is {0}", upperVariable/(divisor));

Console.ReadLine();



Hey guys question on this logic. I am trying to make my program do the following.
if a number is entered thats below 10 then make the program ask for the first number again. Right now if I give a number below ten it will display that the number is too low but it wont loop again but instead it will go on to the second value.

Any suggestions that can lead me in the right direction?

Thank you.
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

There are some other problems with the loop as well. Try this:
// arrays have Length property, use that instead of LINQ Count()
for (int i = 0; i < myLottoNums.Length; ) // only increment i for valid numbers
{
    Console.Write("Number {0}: ", i + 1);
    var number = double.Parse(Console.ReadLine());  // read to a variable
	
	if (number >= 10)
	{
		// assign to the array and move to next number only if this one is ok
		myLottoNums[i++] = number; 
		sum += number;  // only update the sum with valid numbers!
	}
	else
    {
        Console.WriteLine("Sorry that is too low");
    }
}
   
Comments
Maciej Los 7-Jan-15 18:09pm
   
+5!
iniesta08 7-Jan-15 18:47pm
   
This is an awesome solution. But quick question or comment.

I thought that whenever there was a for loop, there needs to be the i++. You left that blank. How does the computer know what you intend on doing?
Tomas Takac 8-Jan-15 3:27am
   
As you can see it works! ;-) The for statement is very flexible. Per MSDN[^] the iterator part can contain zero or more commands. Most people use for statement only to loop trough sequence of numbers, array idexes mostly. But it is more universal than that. For example reading all lines from a text reader:

for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())

So in your words "computer" doesn't know/care what you are doing, you need to tell it via your code. In this case you don't want to increment i after each iteration.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100