%3Cp%20class%3D%22c1%22%3E%20Write%20a%20program%20to%20determine%20statistics%2
ID: 3553890 • Letter: #
Question
%3Cp%20class%3D%22c1%22%3E%20Write%20a%20program%20to%20determine%20statistics%20for%20a%0Avideo%20game%20tournament.%26nbsp%3BThe%20user%20will%20input%20names%20and%20scores%20of%0Aall%20tournament%20players.%26nbsp%3BThe%20program%20will%20calculate%20the%20average%0Ascore%20and%20display%20the%20players%20who%20scored%20below%0Aaverage.%26nbsp%3B%26nbsp%3B%3Cbr%20%2F%3E%0A%3Cbr%20%2F%3E%0AThe%20program%20will%20implement%20these%20functions%3A%3C%2Fp%3E%0A%3Cul%20class%3D%22c2%22%3E%0A%3Cli%3E%3Cbr%20%2F%3E%3C%2Fli%3E%0A%3Cli%3EMain()%3A%20Declares%20variables%20for%20the%20number%20of%20players%20and%0Aaverage%20score%2C%20and%20two%20arrays%20of%20size%20100%3A%20one%20to%20store%20player%0Anames%20and%20the%20other%20to%20store%20their%20respective%20scores.%26nbsp%3BCalls%0Athe%20following%20functions%20in%20sequence%2C%20passing%20necessary%20parameters%0Aby%20reference%3A%0A%3Cul%3E%0A%3Cli%3EInputData(%20)%3A%20Gets%20player%20names%20and%20scores%20from%20the%20user%20and%0Astores%20them%26nbsp%3Binto%20the%20two%20arrays%20for%20an%20unknown%20number%20of%0Aplayers%20up%20to%20100.%26nbsp%3B%3C%2Fli%3E%0A%3Cli%3EDisplayPlayerData()%3A%20Displays%20each%20player's%20name%20and%0Ascore.%26nbsp%3B%3C%2Fli%3E%0A%3Cli%3ECalculateAverageScore(%20)%3A%26nbsp%3BCalculates%20the%20average%20score%20and%0Areturns%20it%20by%20value.%26nbsp%3B%3C%2Fli%3E%0A%3Cli%3EDisplayBelowAverage(%20)%3A%26nbsp%3BDisplays%20the%20names%20and%20scores%20of%0Aplayers%20who%20scored%20below%20average.%26nbsp%3B%3C%2Fli%3E%0A%3C%2Ful%3E%0A%3C%2Fli%3E%0A%3C%2Ful%3E%0AExplanation / Answer
Create a C#(sharp) Console Application using Visual Studio and Save the project as gameTournament.
Open the file Program.cs and write the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace gameTournament
{
class Program
{
static void Main(string[] args)
{
//Declare arrays to hold the player names and player scores
string[] strPlayerName = new string[100];
double[] dblPlayerScore = new double[100];
double dblAverageScore = 0;
//Players count
int intPlayerCount = 0;
//call the InputData() method
InputData(ref strPlayerName, ref dblPlayerScore, ref intPlayerCount);
//call the DisplayPlayerData method
DisplayPlayerData(strPlayerName, dblPlayerScore, intPlayerCount);
//call the CalculateAverageScore to display the average score of all players entered
CalculateAverageScore(dblPlayerScore, intPlayerCount, ref dblAverageScore);
//call the DisplayBelowAverage to display all the users that have a score below the average score
DisplayBelowAverage(strPlayerName, dblPlayerScore, intPlayerCount, dblAverageScore);
Console.ReadLine();
}
static void InputData(ref string[] strPlayerName, ref double[] dblPlayerScore, ref int intPlayerCount)
{
//declare and initialize our test condition for the loop
bool isContinue = true;
//declare our test condition for our user input
bool isDouble;
//loop until the user has entered 100 players
do
{
Console.Write("Enter Player Name (Q to quit): ");
strPlayerName[intPlayerCount] = Console.ReadLine();
//here we check to see if the user asks to stop entering player data. If so, we flip the conditional flag, and continue to the end of the loop.
if (strPlayerName[intPlayerCount].ToLower() == "q")
{
isContinue = false;
continue;
}
//We are looping the entry for the score to validate the data. We do not want erronious characters present, so we parse the input to check if they are accepted as a double
do
{
Console.Write("Enter the score for {0}: ", strPlayerName[intPlayerCount]);
//try to parse input as a double. If it is acceptable, it returns a true (boolean) value
isDouble = double.TryParse(Console.ReadLine(), out dblPlayerScore[intPlayerCount]);
//if the attempted parse returned a false value, we display an error message and represent the entry for the score
if (!isDouble)
{
Console.Write("You have entered invalid data! ");
}
} while (!isDouble);
Console.WriteLine();
//increment the amount of players that were added
intPlayerCount++;
} while ((intPlayerCount <= 100) && (isContinue));
}
static void DisplayPlayerData(string[] strPlayerName, double[] dblPlayerScore, int intPlayerCount)
{
Console.WriteLine("Name Score");
//loop through each array element that holds data to display the players' names and scores.
for (int i = 0; i <= intPlayerCount - 1; i++)
{
Console.WriteLine("{0} {1}", strPlayerName[i], dblPlayerScore[i]);
}
}
static void CalculateAverageScore(double[] dblPlayerscore, int intPlayerCount, ref double dblAverageScore)
{
double dblSumOfScores = 0;
//loop through each element that holds data to accumulate the sum of all the player scores
for (int i = 0; i <= intPlayerCount - 1; i++)
{
dblSumOfScores += dblPlayerscore[i];
}
//calculate the average of all the scores by taking the sum and dividing it by the playercount
dblAverageScore = dblSumOfScores / Convert.ToDouble(intPlayerCount);
Console.Write(" Average Score: ");
Console.Write("{0} ", dblAverageScore);
}
static void DisplayBelowAverage(string[] strPlayerName, double[] dblPlayerScore, int intPlayerCount, double dblAverageScore)
{
Console.WriteLine(" Players who scored below average: ");
Console.WriteLine("Name Score");
for (int i = 0; i <= intPlayerCount - 1; i++)
{
if (dblPlayerScore[i] < dblAverageScore)
{
Console.WriteLine("{0} {1}", strPlayerName[i], dblPlayerScore[i]);
}
}
}
}
}
Press F5 to Run the Application.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.