Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Ok, this will be the first question for this project.

This is a Math quiz system. Always must generate different problems for each problem, but at the same time, I have to generate the same exam for each one (using the same seed in random class).

Many problems are different. There are times tables, addition and subtraction of signed numbers, comparing integers, comparing fractions, convert fraction to decimal, and others I can tell while I advance to explain you.

The problems must be between in a range and there mustn't be repeat problems.

I was using several jagged arrays. The code for me is awful because sometimes I don't even understand it.

For example: There are problems that using the same structure ( NUMBER1 OPERATOR NUMBER 2 = RESULT )

This is the pattern for it:

        int[][] arrayLimits1 = new int[][]
        {
            new int[] { 6, 10, 3, 10 }, new int[] { 6, 10, 3, 10 },
            new int[] { 6, 10, 3, 10 }, new int[] { 6, 10, 3, 10 },
            new int[] { 6, 10, 3, 10 }, new int[] { 6, 10, 3, 10 },
            new int[] { 6, 10, 3, 10 }, new int[] { 6, 10, 3, 10 },
            new int[] { 6, 10, 3, 10 }, new int[] { 6, 10, 3, 10 },
            new int[] { 15, 50, -95, -50 }, new int[] { -50, -15, -95, -50 }, new int[] { -50, -15, -95, -50 },
            new int[] { 15, 50, -95, -50 }, new int[] { -50, -15, -95, -50 }, new int[] { -50, -15, -95, -50 },
            new int[] { 6, 10, -10, -3 }, new int[] { -10, -6, 3, 10 }, new int[] { -10, -6, -10, -3 },
            new int[] { 6, 10, -10, -6, 1 }, new int[] { -10, -6, 6, 10, 1 }, new int[] { -10, -6, -10, -6, 1 }, 
            new int[] { 300, 900, 15, 90 }, new int[] { 300, 900, 25, 50 }, new int[] { 2000, 10000, 105, 900 }, new int[] { 12, 20, 25, 50, 1},
            new int[] { 10, 50, -50, -10 }, new int[] { -100, -50, -100, -50 }, new int[] { -10, 10, -10, 10 }, new int[] { -100, 0, -100, 0  }   
        };

        int[][] problems1 = GeneratePattern1(arrayLimits1);

The generate function, it receives the array. The first index in the array means min and max of NUMBER1, and others two for NUMBER2.

    private int[][] GeneratePattern1(int[][] arrayLimits)
    {
        int[][] sel = new int[arrayLimits.Length][];
        for (int i = 0; i < arrayLimits.Length; i++)
        {
            int[] temp;
            do
            {
                temp = Mathematics.GeneratePairOfIntegers(arrayLimits[i][0], arrayLimits[i][1], arrayLimits[i][2], arrayLimits[i][3]);

                if (arrayLimits[i].Length > 4)
                    temp[0] *= temp[1];

            } while (AuxiliaryMethods.Exists(sel, temp, i));

            sel[i] = temp;
        }

        return sel;
    }

    public static int[] GeneratePairOfIntegers(int li1, int ls1, int li2, int ls2)
    {
        int[] pair = new int[2];
        pair[0] = randomizer.Next(li1, ls1);
        pair[1] = randomizer.Next(li2, ls2);

        return pair;
    }

    public static bool Exists<T>(T[] array, T elem, int capacity)
    {
        for (int i = 0; i < capacity; i++)
        {
            if (array[i].Equals(elem))
                return true;
        }
        return false;
    }

All the array has Count=4, except some because has a number "1". This means that the problem is a division and I want to generate divisions with a remainder equals to zero.

I had to create another function for each type of problem. Last function was for integer problems but if I want doubles..

    private double[][] GeneratePattern2(double[][] arrayLimits)
    {
        double[][] sel = new double[arrayLimits.Length][];
        for (int i = 0; i < arrayLimits.Length; i++)
        {
            double[] temp;
            do
            {
                temp = Mathematics.GeneratePairOfDoubles(arrayLimits[i][0], arrayLimits[i][4], (int)arrayLimits[i][5],
                                                         arrayLimits[i][6], arrayLimits[i][7], (int)arrayLimits[i][5]);

                if (arrayLimits[i].Length > 4)
                    temp[0] = Convert.ToDouble(Convert.ToDecimal(temp[1]) * Convert.ToDecimal(temp[0]));

            } while (AuxiliaryMethods.Exists(sel, temp, i));

            sel[i] = temp;
        }

        return sel;
    }

    public static double[] GeneratePairOfDoubles(double li1, double ls1, int decimals1, double li2, double ls2, int decimals2)
    {
        double[] pair = new double[2];
        pair[0] = randomizer.NextDouble(li1, ls1, decimals1);
        pair[1] = randomizer.NextDouble(li2, ls2, decimals2);

        return pair;
    }

I think I'm being redundant. I need to generate the problems, how can I generate a class to store each one? Because I'm using several jagged arrays and I think that's bad.

9 x 9 = __ <-- Only an integer

49 + -89 = __ <-- Only an integer

0.75 (decimal) = _ (fraction) <-- Two integers

3/4 (fraction) = _ (decimal) <-- A double

share|improve this question
3  
You say your problem is that you want to work uniformly with those problems, but they are different in details. So you have different classes of problems. Start by creating classes for types of problems you have and figure out what you want to do with those problems in general and create an interface or abstract class for that. – Gabriel Ščerbák Aug 7 '11 at 0:22
3  
Gabriel is right about the interface/abstracts, identify the activities and data elements and design some contracts for starters. The other thing I wanted to say is, post smaller pieces of contiguous and specific code for review at a time. Often times cleaned up code makes available design improvements obvious, which I think is what you're more interested in anyways. – Jimmy Hoffa Aug 10 '11 at 12:29
Code is data and data is code. Use a Lisp. – Leonid Apr 20 '12 at 22:11

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.