I have an array set up to hold this information, namely:
1. The outcome possibility (2-12),
2. The number of times that outcome occurs and
3. What percentage of total does it occur
I have the basic functionality of the program completed. I am, however, having trouble with sorting. I am to create a sort button that will change the way the array is sorted, by either outcome possibility or percentage of occurrance. Although the specifications state that I will use an array, I'm wondering if I need 2 arrays. Could someone steer me in the right direction?
Code follows in two files, Dice.cs class and Default.aspx.cs:
Dice.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for Dice
/// </summary>
public class Dice
{
private Random random = new Random();
public int count;
public Dice()
{
}
public int Roll()
{
int die1 = random.Next(1, 7);
int die2 = random.Next(1, 7);
return die1 + die2;
}
public int[] TossResults(int number)
{
int[] result = {0,0,0,0,0,0,0,0,0,0,0,0,0};
for (int i = 0; i < number; i++)
result[Roll()]++;
return result;
}
public int CountRolls (int number)
{
for (int i = 0; i < number; i++)
count++;
return count;
}
}
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
int numRolls;
int[] diceThrows;
int numCount;
double pct;
Dice dice = new Dice();
protected void Page_Load(object sender, EventArgs e)
{
txtRolls.Focus();
cmdPercent.Enabled = false;
cmdPossible.Enabled = false;
}
public void setDiceArray()
{
numRolls = int.Parse(txtRolls.Text);
diceThrows = dice.TossResults(numRolls);
numCount = dice.CountRolls(numRolls);
txtDiceOutcome.Text = "Outcome\tFrequency\tPercentage\n";
for (int i = 2; i <= 12; i++)
{
double diceThrow = diceThrows[i];
pct = (diceThrow / (numRolls * .01));
txtDiceOutcome.Text += i + "\t" + diceThrows[i] + "\t\t" +
pct.ToString("#0.00") + " %\n";
}
}
protected void cmdCalc_Click(object sender, EventArgs e)
{
setDiceArray();
cmdCalc.Enabled = false;
txtRolls.Enabled = false;
cmdPossible.Enabled = true;
cmdPercent.Enabled = true;
}
protected void txtClear_Click(object sender, EventArgs e)
{
txtDiceOutcome.Text = "";
txtRolls.Text = "";
txtRolls.Focus();
cmdCalc.Enabled = true;
txtRolls.Enabled = true;
cmdPercent.Enabled = false;
cmdPossible.Enabled = false;
}
protected void cmdPossible_Click(object sender, EventArgs e)
{
cmdPossible.Enabled = false;
cmdPercent.Enabled = true;
}
protected void cmdPercent_Click(object sender, EventArgs e)
{
cmdPercent.Enabled = false;
cmdPossible.Enabled = true;
}
}
I have read and tried the array.sort, but since I only have one item stored in the array I'll never be able sort any other way. I feel like I need a second array, but am am a little stuck. Any assistance in the right direction is very much appreciated. Thanks!


Ask A New Question
Reply





MultiQuote






|