Home > Software engineering >  How can I find the average of an integer variable that is changed 5 times from a while loop nested i
How can I find the average of an integer variable that is changed 5 times from a while loop nested i

Time:02-16

Sorry, it was hard to phrase that question.

My console "program" uses a while loop to randomly generate a number between 1 & 6 (like a dice). The goal is to generate (roll) the number 6. It also has a variable inside it called attempts to calculate how many attempts it took to generate (roll) the number 6.

I want to run this console "program" 5 times (I used a for loop) and then calculate the average of the attempts variable from each iteration of the for loop.

My question is, how can I extract the attempts variable from each iteration and then calculate the average of the attempts.

This is the code I have so far:

// creates the random # generator

Random numberGen = new Random();

// Runs the while loop 5 times

for (int i = 0; i < 5; i  ) {

//Declares the roll and attempts variables to start at 0

    int roll = 0;
    int attempts = 0;

//Runs the number generator to create a number between 1-6. Prints the output.

    while (roll != 6) {
        roll = numberGen.Next(1, 7);
        Console.WriteLine("You rolled: "   roll);
        attempts  ;
}
//Prints the # of attempts after each iteration of the for loop.

Console.WriteLine("Total attempts: "   attempts);

//I was trying to use an if statement to transfer the attempt variable into a different variable after each iteration, however this does not work, my brain is fried at this point.

int attempt1, attempt2, attempt3, attempt4, attempt5;
    if (i == 0) {
        int attempts = attempt1;
    }
    else if (i == 1) {
        int attempts = attempt2;
    }
    else if (i == 3) {
        int attempts = attempt3;
    }
    else if (i == 4) {
        int attempts = attempt4;
    }
    else {
        int attempts = attempt5;
    }


} 


Console.ReadKey();

CodePudding user response:

Here you go:

Random numberGen = new Random();

// Runs the while loop 5 times

List<int> attemptsList = new List<int>();

for (int i = 0; i < 5; i  )
{

    //Declares the roll and attempts variables to start at 0

    int roll = 0;
    int attempts = 0;

    //Runs the number generator to create a number between 1-6. Prints the output.

    while (roll != 6)
    {
        roll = numberGen.Next(1, 7);
        Console.WriteLine("You rolled: "   roll);
        attempts  ;
    }
    //Prints the # of attempts after each iteration of the for loop.

    Console.WriteLine("Total attempts: "   attempts);
    attemptsList.Add(attempts);

}

var avgAttempts = attemptsList.Average();

Console.WriteLine("Average attempts: "   avgAttempts);

CodePudding user response:

To build on mathis1337's answer, you can do the same thing without a list by summing each term of the arithmetic mean:

    Random numberGen = new Random();

    var mean = 0f;
    const int count = 5;

    for (int i = 0; i < count; i  )
    {

        //Declares the roll and attempts variables to start at 0

        int roll = 0;
        int attempts = 0;

        //Runs the number generator to create a number between 1-6. Prints the output.

        while (roll != 6)
        {
            roll = numberGen.Next(1, 7);
            Console.WriteLine($"You rolled: {roll}");
            attempts  ;
        }
        //Prints the # of attempts after each iteration of the for loop.

        Console.WriteLine($"Total attempts: {attempts}" );
        mean  = ((float)attempts) / count;

    }

    Console.WriteLine($"Average attempts: {mean}");

This is effectively what MPelletier was suggesting in their answer as well

CodePudding user response:

An average is the sum divided by the count. Declare a variable to accumulate the sum, then divide by 5. Mind your data types!

  •  Tags:  
  • c#
  • Related