Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Collections Arrays Jagged Arrays

Calvin Secrest
Calvin Secrest
24,815 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

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

Calvin Secrest
Calvin Secrest
24,815 Points

Thanks for you assistance

Elfar Oliver
Elfar Oliver
3,924 Points

I tried reading through and maybe understood a bit, but could you explain a few things for me? If you're still on here 5.5 years later that is

Justin Blankinship
Justin Blankinship
5,087 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?

Calvin Secrest
Calvin Secrest
24,815 Points

I did thanks for checking on my issued

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?

Correct

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 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;
        }
    }
}