Question
Given a list of 30 whole numbers, each number representing the runs scored by a player in cricket per ball, write a C# program to find the following:
- Run rate, which is the runs per over. 6 balls make an over.
- Assuming that every 2 overs was played be a new batsman, calculate the batting average for each batsman. Batting average is the number of runs scored per ball played by that batsman.
Solution
//Calculate the run rate
public static double ranRateCalculator(List runs)
{
int totalRuns = 0;
foreach (int i in runs)
{
totalRuns += i;
}
//Total overs are the number of times the runs are entered in the list divided by 6 since 6 balls make an over.
int totalOvers = runs.Count / 6;
//runRate is the total runs per over
double ranRate = (double)totalRuns / totalOvers;
return runRate;
}
//Calculate the batting average for each batsman
public static List ranRateCalculator(List runs)
{
//Total overs are the number of times the runs are entered in the list divided by 6 since 6 balls make an over.
int totalOvers = (runs.Count)/6;
//total number of batsman are the total number of overs divided by 2 since the batsman change every 2 overs. If the total over is not exactly divisible by 2, then the number of batsman increase by one to play the remaining overs.
int batsmanNum = totalOvers / 2;
if (totalOvers%2 != 0)
batsmanNum = batsmanNum + 1;
//Store the average scores per batsman in a list
List avgPerBatsman = new List();
for (int I-0; i < batsmanNum; i++)
{
// find the runs scored by the first batsman, the second etc.
var totalRunsPerBatsman = runs.Skip(i*12).Take(12);
int total =0;
foreach (var n in totalRunsPerBatsman)
{
total += n;
}
double avg = (double) total / 12;
avgPerBatsman.Add(avg);
}
}
return avgPerBatsman;
//Calling the functions
public void Main()
{
List runs = new List{3,2,0,4,6,2,1,0,6,5,4,3,2,1,4,5,3,2,1,0,0,5,6,3,4,3,2,0,4,6};
// Finding the total run rate
double runRate = runRateCalculator(runs);
Console.Write("the run rate is {0}", runRate);
Console.ReadKey();
//Finding the run rate for each batsman
List avg = batsmanAvg(runs);
foreach (var v in avg)
{
Console.WriteLine("The batsman average score are : {0}", v);
}
Console.ReadKey();
}
No comments:
Post a Comment