Home > Back-end >  Can I write a function that prints out the contents of a multidimensional array and returns it as a
Can I write a function that prints out the contents of a multidimensional array and returns it as a

Time:03-20

So I wrote a fonction called Afficher(int [,] m) which prints out in a table like manner the contents of a multidimensional array. The problem is that I would like the fonction to return a string instead of being just a void so that I can combine it to another string later on. Is there a possible way of doing this, maybe by using StringBuilder? Thanks.

        static void Afficher(int[,] m)
        {

            string lignePleine = new string('-', m.GetLength(1) * 6   1);

            for (int ligne = 0; ligne < m.GetLength(0);   ligne)
            {

                Console.WriteLine(lignePleine);
                Console.Write("|");
                for (int colonne = 0; colonne < m.GetLength(1);   colonne)
                {

                    {
                        Console.Write($"{m[ligne, colonne],3}  |");
                    }
                }
                Console.WriteLine();
            }

            Console.WriteLine(lignePleine);
            Console.WriteLine();


        }


        public int[,] CreationMatrice()
        {
            Random generateur = new Random();
            List<int> NewValeur = new();
            for (int i = 1; i <= (Trajet.NB_LIGNES * Trajet.NB_COLONNES); i  )
            {
                listeValeurs.Add(i);
            }
            int nombreValeur = listeValeurs.Count;
            for (int i = 0; i < nombreValeur; i  )
            {
                int indexRandom = generateur.Next(listeValeurs.Count);
                NewValeur.Add(listeValeurs[indexRandom]);
                listeValeurs.RemoveAt(indexRandom);
            }

            int index = 0;
            for (int ligne = 0; ligne < Trajet.NB_LIGNES; ligne  )
            {
                for (int colonne = 0; colonne < Trajet.NB_COLONNES; colonne  )
                {
                    matrice[ligne, colonne] = NewValeur[index];
                    index  ;
                }
            }
            return matrice;
        }

CodePudding user response:

You can, instead of printing the parts that compose the string, simply add them to an object and return it at the end:

static string Afficher(int[,] m)
{
    string lignePleine = new string('-', m.GetLength(1) * 6   1);
    var result = "";
    for (int ligne = 0; ligne < m.GetLength(0);   ligne)
    {         
        result  = lignePleine   "\n"   "|";

        for (int colonne = 0; colonne < m.GetLength(1);   colonne)
        {
            result  = $"{m[ligne, colonne],3}  |";
        }
        result  = "\n";
    }
    result  = lignePleine   '\n';
    return result;
}

https://onlinegdb.com/G9ErwOcE6

Above simpling adding the strings, or if you prefer, use String.Concat:

static string Afficher(int[,] m)
{
    string lignePleine = new string('-', m.GetLength(1) * 6   1);
    var result = "";
    for (int ligne = 0; ligne < m.GetLength(0);   ligne)
    {         
        result = string.Concat(result, lignePleine   "\n"   "|");

        for (int colonne = 0; colonne < m.GetLength(1);   colonne)
        {
            result = string.Concat(result, $"{m[ligne, colonne],3}  |");
        }
        result = string.Concat(result, "\n");
    }
    result = string.Concat(result, lignePleine   '\n');
    return result;
}

You can then call it, for example, using your other function:

public static void Main(string[] args)
{
    Console.WriteLine(Afficher(CreationMatrice()));
}

https://onlinegdb.com/bR2G_UYwb

These are only 2 ways of doing it, there are several, and it depends on target and final goal, here you can find more info on this matter:

Most efficient way to concatenate strings?

CodePudding user response:

Here it is with the StringBuilder, not much different than the other approaches:

static String Afficher(int[,] m) { string lignePleine = new string('-', m.GetLength(1) * 6 1);

StringBuilder sb = new StringBuilder();
for (int ligne = 0; ligne < m.GetLength(0);   ligne)
{
  sb.AppendLine(lignePleine);
  sb.Append("|");
  for (int colonne = 0; colonne < m.GetLength(1);   colonne)
  {
    sb.Append($"{m[ligne, colonne],3}  |");
  }
  sb.AppendLine();
}
sb.AppendLine(lignePleine);
sb.AppendLine();
return sb.ToString();

}

  • Related