Need help with 2D arrays and printout of values | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Need help with 2D arrays and printout of values

Create a static method void which uses Console.Write() to print the values of the 2nd array. Printout should values by commas and print each row of the array on its own row. Use “\n”

2nd Mar 2018, 11:32 PM
Luis
Luis - avatar
7 Answers
+ 7
Don't apologize. Nothing wrong with asking; we're here to help you out. Trust me, each and every one of us has been there, so we know how it goes. You'll get the hang of it in due time.
2nd Mar 2018, 11:58 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 6
You'll want to use nested loops. First loop will be your rows and second loop will be your cells. After the second loop, have it add the new line there. ::::: OUTPUT ::::::: 1,2 3,4 5,6 https://code.sololearn.com/cTFMtW887OC7/#cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Print2D(); } static void Print2D(){ int [,] num = new int [,] { {1,2}, {3,4}, {5,6} }; for(int i = 0; i < 3; ++i){ for(int j = 0; j < 2; ++j){ if(j == 1){ Console.Write(num[i,j]); } else { Console.Write(num[i,j] + ","); } } Console.WriteLine(); } } } }
2nd Mar 2018, 11:53 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 6
That's the one that'll iterate through the data in the row. It's grabbing the numbers in the array you created. Since we're working with a 3x2 array, it'll have 3 rows with 2 cells of data in each. i = row, j = cell. That's also why the first loop condition is i < 3 and second loop is j < 2. We check if j == 1 because that's the last cell in that row, so we don't want to put a comma behind it since nothing else comes after it on that row. If it isn't equal to 1, we add the comma behind it. Once that loop is done, in the bottom of the first loop you can just put Console.WriteLine() and it'll automatically do the "\n" for you. No need for Console.WriteLine("\n").
2nd Mar 2018, 11:57 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
This is what i have so far Public static void Print2D(double[] myArray){ int [,] num =new int [,] { {1,2}, {3,4}, {5,6}, }; Console.Write(num[1,0]);
2nd Mar 2018, 11:34 PM
Luis
Luis - avatar
+ 2
Indont understand your second for loop “for(int j =0; j<2;++j);
2nd Mar 2018, 11:52 PM
Luis
Luis - avatar
+ 2
Sorry im really new to coding
2nd Mar 2018, 11:54 PM
Luis
Luis - avatar
+ 2
Awesome Thanks! Do you happen to know a good comprehensive array video tutorial by any chance?
3rd Mar 2018, 12:00 AM
Luis
Luis - avatar