0

I have a small program to roll two dices. So the user should enter a number to be the frequency number of the dice to be rolled when user click start, the program will show two different column with each fill with random number based on frequency which user inputted.

The program looks like this:

rolldice

After the random number showed up. The program will sum the random numbers (i filled the random numbers inside an array for each dices), I'm confuse, how can I sum two array from different method?

This is my code so far:

private void button1_Click(object sender, EventArgs e)
{
    firstDice();
    secondDice();
}
Random vran = new Random();
void firstDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0;i<x;i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox2.Text = temp;
    }
}
void secondDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0; i < x; i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox3.Text = temp;
    }
}

Notes: the array will sum for each value (i.e array[0] + array[0].. and so on).

5
  • 2
    Have each method return its array. button1_Click could then sum the arrays they return. Commented Oct 11, 2019 at 19:16
  • @EdPlunkett could you please show me how its done? since Im confuse on how to return each element of array/ Commented Oct 11, 2019 at 19:21
  • Return the array. int[] secondDice() { /* ...stuff...*/ return arr; } Commented Oct 11, 2019 at 19:22
  • 1
    I have no idea what that means, sorry. Commented Oct 11, 2019 at 19:26
  • 1
    Im sorry that was a stupid question tho, I have figured it out the solution. Thanks! Commented Oct 11, 2019 at 19:31

4 Answers 4

1

You will want to return an array from each of your firstDice and secondDice methods so that you can sum the two of them in your button1_Click method.

private void button1_Click(object sender, EventArgs e)
{
    int[] d1 = firstDice();
    int[] d2 = secondDice();
    // Assuming d1 and d2 are always of the same length
    int[] sum = new int[d1.Length];
    for (int i = 0; i < sum.Length; i++)
    {
        sum[i] = d1[i] + d2[i];
    }
}
Random vran = new Random();
int[] firstDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0;i<x;i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox2.Text = temp;
    }
    return arr;
}
int[] secondDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0; i < x; i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox3.Text = temp;
    }
    return arr;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here's your code corrected to do what you wanted. First note that both firstDice() and secondDice() now return an array of integers. Then in button1_Click a for loop iterates through each array element and sums them up, the result is put back in array1.

private void button1_Click(object sender, EventArgs e)
{
     int[] array1 = firstDice();
     int[] array2 = secondDice();
     for (int i = 0; i < array1.Length; i++)
     {
         array1[i] += array2[i];
     }
}
Random vran = new Random();
int[] firstDice()
{
     string temp = null;
     int x = Convert.ToInt32(textBox1.Text);
     int[] arr = new int[x];
     for (int i = 0;i<x;i++)
     {
          int a = vran.Next(1, 6);
          arr[i] = a;
          temp += a + " ";
          textBox2.Text = temp;
     }
     return arr;
}
int[] secondDice()
{
     string temp = null;
     int x = Convert.ToInt32(textBox1.Text);
     int[] arr = new int[x];
     for (int i = 0; i < x; i++)
     {
         int a = vran.Next(1, 6);
         arr[i] = a;
         temp += a + " ";
         textBox3.Text = temp;
     }
     return arr;
}

Comments

0

Just declare private variables under the class, and use them like this :

private int firstDiceSum = 0;
private int secondDiceSum = 0;
private int totalSum = 0;

private void button1_Click(object sender, EventArgs e)
{
    firstDice();
    secondDice();

    // Sum
    totalSum = firstDiceSum + secondDiceSum;

    //Show it to the user 
    textBox4.Text = totalSum.ToString();
}

Random vran = new Random();

void firstDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0;i<x;i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox2.Text = temp;

        //add this 
        firstDiceSum += a;
    }
}

void secondDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0; i < x; i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox3.Text = temp;

        //add this 
        secondDiceSum += a;
    }
}

Comments

0

You can simplify your code using only 1 method for generating the dice rolls. You can use use string.Join to concatenate the array and Enumerable.Zip to sum. Also note that the max value of Random.Next is exclusive, so if you want 6 to be included, it must be 7:

private void button1_Click(object sender, EventArgs e)
{
    int frequency = Convert.ToInt32(textBox1.Text);
    var firstDice = RollDice(textBox2,frequency);
    var secondDice = RollDice(textBox3,frequency);
    textBox4.Text = string.Join(" ",firstDice.Zip(secondDice, (x,y)=> x+y));
}
Random vran = new Random();
int[] RollDice(TextBox display, int frequency)
{    
    int[] arr = new int[frequency];
    for (int i = 0; i<frequency; i++)
    {
        int a = vran.Next(1, 7); //If you want 6 inclusive you should change to 7
        arr[i] = a;        
    }
    display.Text = string.Join(" ", arr);
    return arr;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.