0

I'm now learning C# and making a couple of challenges, i managed to pass easily most of them but sometimes there are things i don't understand (i'm a python dev basically)...

Create a function that takes an integer and outputs an n x n square solely consisting of the integer n.

E.G : SquarePatch(3) ➞ [ [3, 3, 3], [3, 3, 3], [3, 3, 3] ] 

So i went trough docs about multidimentionnal arrays, jagged arrays.But i get an error (kind of errors i get the most while learning c#, i never had that kind of problems in python. It's about TYPES convertion !) I mean i often have problems of types.

So here's my code :

public class Challenge { public static int[,] SquarePatch(int n) { int[ ][,] jaggedArray = new int[n][,]; for (int i=0;i<n;i++) { for(int j=0;j<n;j++) { for(int k=0;k<n;k++) { return jaggedArray[i][j, k]=n; } } } } } 

What is actually very boring is that in that kind of challenges i don't know how to make equivalent to python "print tests" ! So i don't even know what's going on till the end...

And i get this error :

Cannot implicitly convert type int to int[,] 
4
  • "return jaggedArray[i][j, k]=n;" What is this supposed to do? Assign something and return the entire array? That's not how C# works. Anyway why use a jagged array in the first place and not a multi-dimensional one which defines fix dimensions? Commented Nov 22, 2022 at 12:45
  • Hello, i'm a beginner, i started learning c# yesterday night. Yes i though i could assign the n value and return the entire array. When i look at the challenge example, the output looks like a jagged array to me, but i might be wrong. Commented Nov 22, 2022 at 12:51
  • Just an asside: you have exactly two dimensions but three nested loops. I suppose you just need two loops, as correctly shown in the answers. Commented Nov 22, 2022 at 12:53
  • If i could fin a way to make prints to verify what i am doing it would be much more easy ! But yes i saw that i was not doing it right. Commented Nov 22, 2022 at 12:57

2 Answers 2

1

n as in the error message, cannot be converted to int[,]. It is an int. You don't need a jagged array but simply an array of n by n, hence arr[n,n].

public static int[,] SquarePatch(int n, int v) { int[,] myArray = new int[n,n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { myArray[i,j] = v; } } return myArray; } 

Here n is the size for row and cols and v is the value to initialize all members to.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to add int v as an argument, n will be the same everywhere, as number of colums, rows and values to fill inside, but thanks it works great !
@GarbezFrançois, yes you are right, I missed that you wanted the same n value in array's members.
1

Well, you should return 2d array: int[,]:

public static int[,] SquarePatch(int n) { // Do not forget to validate input values (n) in public methods if (n < 0) throw new ArgumentOutOfRange(nameof(n)); // We want to return 2d array, here it is int[,] result = new int[n, n]; // All we should do now is to loop over 2d array and fill it with items for (int r = 0; r < result.GetLength(0); ++r) for (int c = 0; c < result.GetLength(1); ++c) result[r, c] = n; return result; } 

You can change your challenge and return jagged array int[][] (array of arrays):

public static int[][] SquarePatch(int n) { // Do not forget to validate input values (n) in public methods if (n < 0) throw new ArgumentOutOfRange(nameof(n)); // now we want to return array of size n (lines) of arrays: int[][] result = new int[n][]; // We loop over lines for (int r = 0; r < result.Length; ++r) { // We create line after line int[] line = new int[n]; // assign each line to result result[r] = line; // and fill each line with required items for (int c = 0; c < line.Length; ++c) line[c] = n; } return result; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.