C# C# Collections Arrays Jagged Arrays

Calvin Secrest
Calvin Secrest
PHP Development Techdegree Student 20,873 Points

I'm confused with it's multiplication table

Return a multiplication table consisting of a 2D multidimensional array. The table should contain all of the products of integers from 0 to maxFactor. For example, if maxFactor is 3 the resulting multiplication table should contain the following: 0 0 0 0 0 1 2 3 0 2 4 6 0 3 6 9

Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors. Restart Preview Get Help Recheck work Math.cs

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[,] BuildMultiplicationTable(int maxFactor)
        {
            for (int i = 0; i <  array.Length; i++)
        {
            System.Console.Write("maxFactor({0}): ", i);
        }


        }
    }
}

7 Answers

Philip Graf
Philip Graf
14,531 Points

Hi Calvin,

there were multiple errors in your code. First, the challenge tells you to use a jagged array(int[][]), not a multidimensional(int[,]) one. Also, you have to declare and return an array instead of printing the results to the Console.

Well, you can have a look at this code, which should work for the challenge:

namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] arr = new int[maxFactor + 1][];

            for (int x = 0; x <= maxFactor; x++)
            {
                arr[x] = new int[maxFactor + 1];

                for (int y = 0; y <= maxFactor; y++)
                {
                    arr[x][y] = x * y;
                }
            }

            return arr;
        }
    }
}

I would recommend you to study the structure instead of just copy&paste. If you get stuck, you can review the videos, and of course feel free to ask me!

Best Regards, Philip

Justin Blankinship
Justin Blankinship
5,084 Points
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[,] BuildMultiplicationTable(int maxFactor)
        {
            int[,] arr = new int[maxFactor + 1, maxFactor +1];

            for (int x = 0; x <= maxFactor; x++){
                for (int y = 0; y <= maxFactor; y++)
                {
                    arr[x, y] = x * y;
                }
            }

            return arr;
        }
    }
}

I am just happy! Here is my solution:

namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] result = new int[maxFactor+1][];

            for(int i=0; i <= maxFactor;i++)
            {
             result[i] = new int[maxFactor+1];
              for(int j = 0; j <= maxFactor; j++) 
              {
                result[i][j] = i*j;
              }   
            }
            return result;    
        }
    }
}

@CalvinSecrest did you ever complete this challenge?

I'm having trouble how did you get it to compile Calvin Secrest

Question, is the maxFactor + 1 added inside the [] because we count from zero?

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
20,984 Points

Dear Cavin

using System.Collections.Generic;
using System;
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
         public static List <int> GetPowersOf2 (int UpperLimit)
        {

            var PowersOf2List = new List <int> ();

            for (int index = 0; index < UpperLimit + 1; index++)
            {
                PowersOf2List.Add ( (int) Math.Pow (2, index) );
            }
            return PowersOf2List;
        }
    }
}