+ 1

Hey ! How to make 3 or more dimension array? String type ... / c#

suppose each dimension containe "a" and "b" string [ , , ] ab = {{"a","a","a"},{"a","a","b"},...,{"b","b","b"}} How to print the whole array?

28th Nov 2018, 7:31 PM
Mohammad Reza Nozadi
7 odpowiedzi
+ 2
string[ , , ] ab = { { { "a", "a", "a" }, { "a", "a", "b" }, { "b", "b", "a" }, { "b", "a", "b" }, { "b", "a", "a" } } }; int r1 = ab.GetLength(0); int r2 = ab.GetLength(1); int r3 = ab.GetLength(2); for (int i = 0; i < r1; i++) { for (int j = 0; j < r2; j++) { for(int k = 0;k < r3; k++) { Console.Write(string.Format("{0} ", ab[i, j, k])); } Console.WriteLine(); } }
29th Nov 2018, 3:20 AM
Afif Priya Wardana
Afif Priya Wardana - avatar
+ 1
The problem is in : public string nts (int a) You can't add parameters that different from the return type In this case you can't use int as a parameters Use this instead : public string nts (string a) And change the case to a string value like "0" and "1"
5th Dec 2018, 12:32 AM
Afif Priya Wardana
Afif Priya Wardana - avatar
0
Thank you Wardana ... thank you Matthews Is it possible to don't declare the whole array and make the program complete it ?( When the array continues regular like this) a,a,a a,a,b a,b,a a,b,b b,a,a b,a,b b,b,a b,b,b
29th Nov 2018, 7:50 AM
Mohammad Reza Nozadi
0
I wrote this (using Wardana code) What's the problem ? (it should print the array i mentioned at the previous post )
29th Nov 2018, 9:53 AM
Mohammad Reza Nozadi
0
public string nts(int a) { string b; switch (a) { case 0: b="a"; break; case 1: b="b"; break; } return b; } string[ , , ] ab = new string[2,2,2]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for(int k = 0;k < 2; k++) { ab[i, j, k] = {nts(i), nts(j), nts(k)}; Console.Write("{0}", ab[i, j, k]); } Console.WriteLine(); } }
29th Nov 2018, 9:54 AM
Mohammad Reza Nozadi
0
Thank u a lot Wardana I'll try it ...
5th Dec 2018, 7:36 PM
Mohammad Reza Nozadi
0
hi Wardana Thanks ...it worked ... https://code.sololearn.com/clu0099s4kCV/?ref=app
6th Dec 2018, 5:34 PM
Mohammad Reza Nozadi