In: Computer Science
Develop a C# console application that will implement one method that will return an integer value, one void method that will calculate an average, and one void overload for the calculate method that will compute a total. Please read the requirements below carefully. In Main: You will need four variables: an integer value to hold a random value, a double value to hold an average, a double value to hold a total, and a double value to hold an input entry. Using a for loop that will call a get random method 20 times. In the loop, you will pass a seed value to the get random method to use with the random generator and it will return a random value to be added to (accumulate) to the local integer variable in Main. In the getRandommethod, you will generate a random value between 1 and 100 (see p241, section 7.9) and return that random value to Main. The random value will be generated to get random using a Random class object. Once the loop has completed the 20 method calls you will call a calculate method which returns no value and to which you will pass the total of the random values, the average variable by reference, and the literal value of 20. In the calculate method you will compute an average by dividing the total of the random number passed in by the literal value 20 that was passed into the calculate method. After the calculation method executes you will display the average to the console. Following the writeline statement noted above, you will implement a second for loop that will process 5 iterations. Within this for loop you will prompt the user to enter a double value from the console and then assign that input to the double variable declared to hold the input entry. Also within the loop you will call an overload of the void calculate method and will pass two arguments: the entry taken from the console and the variable to hold the total by reference. The overload of the void calculate method will receive the double entry value and the byref double variable that will receive the total. In the overloaded calculate method you will add the value passed in to the total variable. After the overloaded calculate method executes you will display the total to the console. and I want the screenshot of the output too.
using System;
namespace week7
{
class Program
{
static void Main(string[] args)
{
int randomNumber;//hold random
double average = 0;//hold average
double total = 0;//hold total
double manualEntry = 0; //input entry
for(int i = 0; i < 20; i++)//20 times
{
randomNumber = getRandom();//by reference
total = total + randomNumber;//by reference
}
calculate(total, ref average, 20);
Console.WriteLine("The average of the 20 random numbers is {0}", average);
total = 0;
Console.WriteLine();//adds space
for (int i = 0; i < 5; i++)
{
Console.Write("Enter a double value ");
manualEntry = Convert.ToDouble(Console.ReadLine());
calculate(manualEntry, ref total);
}
Console.WriteLine("The total is {0}",total);
}
static int getRandom()
{
Random randomGenerator = new Random();
return randomGenerator.Next(1,101);
}
//pass the total of the random values, the average variable by reference, and the literal value of 20.
// the entry taken from the console and the variable to hold the total by reference.
private static void calculate(double consoleInput, ref double total)
{
total += consoleInput;
}
//pass the total of the random values, the average variable by reference, and the literal value of 20.
private static void calculate(double total, ref double average, double denominator)
{
average = total / denominator;
}
}
}