Wriju's BLOG

.NET, Cloud and everything

Building Simple Math Problems for Jounior School

My daughter studies in junior school and we often face issues getting her enough problem for simple addition and subtractions. So I decided to make an algorithm to generate them. Conditions I needed to follow,

1. It needs to be two digit

2. For addition it needs to be no carry forward, means addition needs to be within 10.

3. For Subtraction no negative calculations. a > b

What I end up doing is below,

var arr1 = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var arr2 = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Then, based on the above condition I created a sort of 2 dimensional array.

class Couple
{
    public int num1 { get; set; }
    public int num2 { get; set; }
}

But, I needed to check if the two vertical number is going to be more than 10 (for addition). So I ran a loop to cover all the elements,

var numList = new List<Couple>();

foreach (var _num1 in arr1)
{
    foreach (var _num2 in arr2)
    {
        var checkVal = _num1 + _num2;
        if(checkVal < 10)
        {
            numList.Add(new Couple() { num1=  _num1, num2 = _num2 } );
        }
    }
}

Then using Random I picked the object from the collection (List<Couple>), based on the variable loop to define the number of problems.

var formattedString = "";
for (int i = 0; i < loop; i++)
{
    int r1 = rnd.Next(numList.Count);
    int r2 = rnd.Next(numList.Count);
               
    formattedString += String.Format("  {0} {1}", numList[r1].num1, numList[r2].num1) + "\r\n";
    formattedString += String.Format("+ {0} {1}", numList[r1].num2, numList[r2].num2) + "\r\n—–\r\n\r\n\r\n";
}

I know this is not a complex thought process, but to me it was a solution to what I was looking for. Also I believe in making a solution simple.

Namoskar!!!