Freigeben über


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!!!

Comments

  • Anonymous
    February 16, 2016
    Thx for this fun idea! Here is a solution with just one loop: var arr = new[] { 1, 2, 3, 4, 5, 6, 7, 8 }; for(int x=0;x<count;x++) {   var d1a = arr[rnd.Next(7)]+1;   var d2a = arr[rnd.Next(8-d1a)];   var d1b = arr[rnd.Next(7)]+1;   var d2b = arr[rnd.Next(8-d1b)];   formattedString += String.Format("  {0} {1}", d1a, d1b) + "rn";   formattedString += String.Format("+ {0} {1}", d2a, d2b) + "rn-----rnrnrn";   Console.WriteLine(formattedString); }