Correction méthode ajout tableau
Tạo vào: 31 tháng 12, 2024
Tạo vào: 31 tháng 12, 2024
patch this code pls
using System;
namespace ProjetInformatique
{
public class Mofus
{
public static string[][] add(string[][] tableau, string input = "")
{
Console.WriteLine("Quel mot voulez-vous ajouter ?");
input = Console.ReadLine();
int n = input.Length;
text// Parcours du tableau for (int i = 0; i < tableau.Length; i++) { // Si la ligne est vide (null), on l’initialise if (tableau[i] == null) { // Si on se trouve à l’indice correspondant à la longueur du mot // on met directement le nouveau mot if (i == n) { tableau[i] = new string[] { input }; break; } else { // Sinon, on fait quelque chose par défaut (ex. "v") // Ce bloc dépend de votre logique métier tableau[i] = new string[] { "v" }; } } else { // Si on se trouve à l’indice où ajouter le mot // on agrandit le tableau existant à cet emplacement if (i == n) { string[] temp = new string[tableau[i].Length + 1]; for (int j = 0; j < tableau[i].Length; j++) { temp[j] = tableau[i][j]; } temp[temp.Length - 1] = input; tableau[i] = temp; break; } } } return tableau; } }
}
using System;
namespace ProjetInformatique
{
public class Mofus
{
public static string[][] add(string[][] tableau, string input = "")
{
Console.WriteLine("Quel mot voulez-vous ajouter ?");
input = Console.ReadLine();
int n = input.Length;
text// Parcours du tableau for (int i = 0; i < tableau.Length; i++) { // Si la ligne est vide (null), on l’initialise if (tableau[i] == null) { // Si on se trouve à l’indice correspondant à la longueur du mot // on met directement le nouveau mot if (i == n) { tableau[i] = new string[] { input }; break; } else { // Sinon, on fait quelque chose par défaut (ex. "v") // Ce bloc dépend de votre logique métier tableau[i] = new string[] { "v" }; } } else { // Si on se trouve à l’indice où ajouter le mot // on agrandit le tableau existant à cet emplacement if (i == n) { string[] temp = new string[tableau[i].Length + 1]; for (int j = 0; j < tableau[i].Length; j++) { temp[j] = tableau[i][j]; } temp[temp.Length - 1] = input; tableau[i] = temp; break; } } } return tableau; } }
}
i want full patch for add pls
Below is a “patched” version of your add method that correctly places a word at the row matching its length, expanding the top-level array if necessary and appending to the existing strings in that row. It avoids unnecessary iteration over the entire array (only touches the relevant index).
csharpusing System; namespace ProjetInformatique { public class Mofus { /// <summary> /// Adds a word into a jagged string array (tableau) at the row /// corresponding to the word’s length. If that row doesn't exist yet, /// the method expands the top-level array and initializes that row. /// </summary> /// <param name="tableau">Jagged array of strings.</param> /// <param name="input">Optionally provided word. If empty, method prompts user.</param> /// <returns>The modified jagged array.</returns> public static string[][] add(string[][] tableau, string input = "") { // Prompt the user if no input was given if (string.IsNullOrWhiteSpace(input)) { Console.WriteLine("Quel mot voulez-vous ajouter ?"); input = Console.ReadLine() ?? ""; } // If user entered an empty string, just return if (string.IsNullOrEmpty(input)) { Console.WriteLine("Aucun mot saisi. Fin de l’opération."); return tableau; } // The row index for storing this word is the word's length int n = input.Length; // Ensure the top-level array is large enough to hold index n if (n >= tableau.Length) { // Resize to length = n+1 so that index n is valid Array.Resize(ref tableau, n + 1); } // If the row at index n is null, initialize it with an empty array if (tableau[n] == null) { tableau[n] = new string[0]; } // We want to append this new word at the end of row n string[] currentRow = tableau[n]; Array.Resize(ref currentRow, currentRow.Length + 1); currentRow[currentRow.Length - 1] = input; // Assign the expanded row back tableau[n] = currentRow; return tableau; } } }
tableau
, we directly compute the “target row” with n = input.Length
and work only with tableau[n]
.n < tableau.Length
. If not, we resize so n
is valid.tableau[n]
is null
, we set it to a new, empty string array.input
at the end.This approach is typically more efficient and cleaner than manually looping over each row and checking conditions.