I have been working on this assignment for a couple days now. I have written all the code myself. I'm not looking to cheat or have someone do my work for me, but for the life of me, I can't get this to work correctly.
The problem I'm having is that when I try to average numbers in an array, instead of dividing by just the number of entries, it's dividing by the total allowed entries into the array.
For example. I enter 2 values into an array that can hold 100 values, instead of dividing by 2, it divides by 100.
How do I get it to divide by just the number of entries? Here is what I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void InputData(string[] player, int[] score, ref int numPlayer)
{
// input items up to the number of the array size
while (numPlayer < player.Length)
{
Console.Write("Enter player name (Q to quit): ");
player[numPlayer] = Console.ReadLine();
if ((player[numPlayer] == "Q") || (player[numPlayer] == "q"))
{
Console.WriteLine();
break;
}
else
{
Console.Write("Enter score for " + player[numPlayer] + ": ");
score[numPlayer] = Convert.ToInt32(Console.ReadLine());
numPlayer++;
}
}
}
static void DisplayPlayerData(string[] player, int[] score, int numPlayer)
{
Console.WriteLine("Name Score");
for (int i = 0; i < numPlayer; i++)
Console.WriteLine("{0, -16} {1, 8}", player[i], score[i]);
}
static double CalculateAverageScore(int[] score, ref int numPlayer)
{
double avgScore;
double total = 0;
for (int i = 0; i < numPlayer; i++)
{
total += score[i];
}
avgScore = total / score.Length;
Console.WriteLine("Average Score:" + avgScore);
return avgScore;
}
static void DisplayBelowAverage(string[] player, int[] score, int numPlayer)
{
double avgScore = CalculateAverageScore(score, ref numPlayer);
Console.WriteLine("Players who scored below average");
Console.WriteLine("Name Score");
for (int i = 0; i < numPlayer; i++)
{
if (score[i] < avgScore)
{
Console.WriteLine("{0, -16} {1}", player[i], score[i]);
}
}
}
static void Main(string[] args)
{
//Variables
string[] player = new string[100];
int[] score = new int[100];
int numPlayer = 0;
InputData(player, score, ref numPlayer);
DisplayPlayerData(player, score, numPlayer);
CalculateAverageScore(score, ref numPlayer);
DisplayBelowAverage(player, score, numPlayer);
Console.ReadLine();
}
}
}