1

This is my code:

 static void Main(string[] args) { Console.Write("How many tests would you like to do? 1 to 10: "); int tests = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); } 

can someone help me out with my code please? i have no idea what im doing

Thanks

5
  • organize all count2..count12 into a collection, say, int[] count - array Commented May 10, 2018 at 9:54
  • i'm curious at why you started at count2 My OCD went off all over the place Commented May 10, 2018 at 9:55
  • 2
    @TheGeneral because you can't score 0 or 1 on 2 regular D6 dice Commented May 10, 2018 at 9:57
  • Review of functional code is more appropriate on Code Review. Commented May 11, 2018 at 23:42
  • Re the edit; the purpose of Stack Overflow is not just to help the person asking, but also: to leave artefacts - code that can help other people in the future. Destroying the question makes that much harder, and is frankly disappointing. I'm tempted to revert the edit, but: the original remains available. Commented May 12, 2018 at 20:14

1 Answer 1

6

Make an array int[] counts = new int[13] and just use counts[total]++;; at the end, loop over it:

for(int i = 2 ; i <= 12 ; i++) // etc 

(note: you could use an array of 11 items and constantly handle the off-by-two, but... it probably isn't worth it)


Something like:

static void Main() { Console.WriteLine("Investigation 1"); Console.WriteLine("==============="); Console.WriteLine(); Console.Write("How many sets of tests? 1 to 10: "); int sets = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); Random r = new Random(); int[] counts = new int[13]; for (int ctr = 0; ctr < 36 * sets; ctr++) { int a = r.Next(1, 7), b = r.Next(1, 7), total = a + b; Console.WriteLine($"Roll {(ctr + 1)}: {a} + {b} = {total}"); counts[total]++; } Console.WriteLine("======================="); Console.WriteLine(); Console.WriteLine("Total Expected Actual"); Console.WriteLine("===== ======== ======"); for(int i = 2; i <= 12; i++) { var expected = sets * (6 - Math.Abs(7 - i)); Console.WriteLine($" {i} {expected} {counts[i]}"); } } 

For a histogram:

var maxCount = counts.Max(); // needs "using System.Linq;" at the top for (int i = 2; i <= 12; i++) { var width = ((Console.WindowWidth - 10) * counts[i]) / maxCount; // make it proportional Console.WriteLine($"{i}\t{new string('*', width)}"); } 
Sign up to request clarification or add additional context in comments.

5 Comments

Okay, I tried putting your code into my first FOR loop but when doing that my lower half of the code has stopped working and is now reading an error. It doesn't recognise "counts" in the current context. Am I doing something wrong?
And if i have it outside of the loop, the "total" becomes invaild and i cant use that :(
@John 2 secs... I'll hack it together#
AMAZING!! Thank you lots and lots, but... I have another request if you would be able to help? Is there a way to create a bar graph using "*"?
Thank you, worked well :) you just saved me hours of strain haha