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 trialHerman Vicens
12,540 PointsSame error here
Acording to the error message, I should be passing a 6 x 6 table, which is exactly what I get with the Console.Write statement in my screen. I don't know if there is a problen with the treeHouse site of there is something wrong that I can't see right now.
using System;
namespace Treehouse.CodeChallenges
{
public static class MathHelpers
{
public static int[,] BuildMultiplicationTable(int maxFactor)
{
int newFactor = maxFactor+1;
int[,] results = new int[10,10];
for (int x=0 ; x != newFactor ; x++)
{
for (int y=0 ; y != newFactor ; y++)
{
results[x,y] = x*y;
Console.Write(" results["+x+","+y+"] ->"+results[x,y]);
}
Console.WriteLine();
}
return results;
}
}
}
3 Answers
Herman Vicens
12,540 PointsAndren, Thanks a lot. I did not know I could use a variable in the actual initialization of the array. That opens a world of other possibilities. Great to know. However, after I use the newFactor variable to initialize the array, I am still getting the Bummer ! [5][5] should contain 25.....and if I add the +1 to the newFactor = maxFactor + 1 then I get the Bummer!The table should contain 6 rows and 6 columns. Either way the challenge complaints and I ran our of ideas. Its easy to deal with compiler errors but the challenge is another whole story because it involves things like, taste, preferences, etc.
andren
28,558 PointsIn this line:
int[,] results = new int[10,10];
You are hardcoding the size of the multidimensional array to be 10x10.
Even if you only fill up 6x6 the array is still technically 10x10, which is why the challenge complaints. Also this is not really just a nitpick of the challenge but an actual design flaw. Let's say the number 20 was passed to your method, what would happen then? Well the method would crash due to an "index is out of range" error.
If you make the size of the array dynamic in the same way your for
loop is (you can use the same variable in fact) then your code will work.
Like this:
using System;
namespace Treehouse.CodeChallenges
{
public static class MathHelpers
{
public static int[,] BuildMultiplicationTable(int maxFactor)
{
int newFactor = maxFactor+1;
int[,] results = new int[newFactor,newFactor];
for (int x=0 ; x != newFactor ; x++)
{
for (int y=0 ; y != newFactor ; y++)
{
results[x,y] = x*y;
Console.Write(" results["+x+","+y+"] ->"+results[x,y]);
}
Console.WriteLine();
}
return results;
}
}
}
Herman Vicens
12,540 PointsDone. Thanks a lot. Strange issue gone. I tried crazy stuff out of desperation in that challenge. It needed the reset. I appreciate your help. I learned good stuff from this one.
andren
28,558 Pointsandren
28,558 PointsThat's strange, I tested the code in my answer before I posted it and it passed the challenge fine. So it should work correctly.
Maybe try resetting the challenge and copy/pasting the code I posted? The challenges can be a bit buggy at times.