Help me nail the final post to the fence of this algorithm

In summary, the conversation discusses a method for formatting console output in the form of a table. The method involves calculating the widths of the columns and using padding to make the strings equal in length. The flaw causing the incorrect output is due to the fact that the first row needs more padding to make it a multiple of the column width. The person suggests using a string of length 80 and inserting the substrings and padding in the correct places.
  • #1
SlurrerOfSpeech
141
11
I'm writing a class to format console output and the trickiest method in this class so far is the one for printing a table. I'm having it act like an HTML table in how text fits

Code:
        public static void PrintTable ( string[][] cells )
        {
            // Outputs content in cells matrix like 

            // =========================================
            //  cells[0][0]   | cells[0][1]
            // -----------------------------------------
            //  cells[1][0]   | cells[1][1]
            // -----------------------------------------
            //  cells[2][0]   | cells[2][1]
            //                .
            //                .
            // -----------------------------------------
            //  cells[n-1][0] | cells[n-1][1]
            //  ========================================

            // Each cell must be able to hold at least 1 character inside 
            // 1 space of padding on the left and right

            int linelen = OutputFormatter.OutputMaxLength; // copy of width (in characters) of lines being formatted
            // Calculate widths of columns 1 and 2 (excluding the single space of padding on the 
            // left and right side of each):
            int w1 = Math.Min(cells.Max(c => c[0].Length), linelen - 6);
            int w2 = linelen - w1 - 5;
            OutputFormatter.PrintChars('-'); // prints a line of equals signs for the top border of the table
           
            // print out rows:
            foreach ( string[] row in cells )
            {
                // Make the strings corresponding to the first and second column have equal
                // length by adding whitespace to the end of the shorter one:
                String padding = new String(' ', Math.Abs(row[0].Length - row[1].Length));
                if ( row[0].Length > row[1].Length ) row[1] += padding;
                else row[0] += padding;

                // Print out content of row
                int i = 0, j = 0, n = row[0].Length - Math.Max(w1,w2);
                for ( ; i < n && j < n; i += w1, j += w2 )
                {
                    Console.WriteLine(" {0} | {1} ",
                                       row[0].Substring(i,w1),
                                       row[1].Substring(j,w2));
                }
                Console.WriteLine(" {0} | {1} ",
                                   row[0].Substring(i,row[0].Length - i),
                                   row[1].Substring(j,row[1].Length - j));                                OutputFormatter.PrintChars('-'); // prints a line of dashes to separate rows
            }

        }
It's almost correct except for the fence post

Code:
                Console.WriteLine(" {0} | {1} ",
                                   row[0].Substring(i,row[0].Length - i),
                                   row[1].Substring(j,row[1].Length - j));

For example, when I test it with the string

Code:
        private static readonly string[][] _instructs = new string[][] {
                                                                         new string[] {"CompareLastTwo","Shows difference between friends lists of last two Facebook data files in repository"},
                                                                         new string[] {"AddFriendList <DataFolderPath>", "blah blah blah"}
                                                                       };

here's what it ends up looking like:

IbJKBvR.png


Where's the flaw that is causing that? I'm trying to make this as compact, elegant, efficient and readable as possible, but it's proving difficult.
 

Attachments

  • upload_2015-11-24_7-19-39.png
    upload_2015-11-24_7-19-39.png
    1.2 KB · Views: 382
Technology news on Phys.org
  • #2
The width of row[0].Substring(i,w1) is w1.
The width of row[0].Substring(i,row[0].Length - i) can be less than w1.
 
  • #3
When I did something similar way back when, I ended up by creating a string of length 80 (the width of the console), filling it with spaces, inserting the substrings (sprintf()) and at the end inserting the "fence post" at the correct place. Then printing the complete string.
 
  • #4
Do you realize that the row(0) string needs more padding? In needs to be padded out to a length that is a multiple of w1.
 

1. How does this algorithm work?

The algorithm uses a set of logical and mathematical operations to solve a specific problem or perform a task. It follows a step-by-step process to analyze input data and produce an output.

2. What is the purpose of this algorithm?

The purpose of this algorithm is to provide a systematic and efficient approach to solving a problem or completing a task. It can be used in various fields such as mathematics, computer science, and engineering to streamline processes and improve results.

3. How accurate is this algorithm?

The accuracy of an algorithm depends on its design and implementation. A well-designed and properly implemented algorithm can produce highly accurate results. However, it is important to regularly test and refine the algorithm to ensure its accuracy.

4. Can this algorithm be applied to different situations?

Yes, the same algorithm can be applied to different situations as long as the problem being solved or task being performed is similar. However, some adjustments may need to be made depending on the specific variables and data involved.

5. What are the advantages of using this algorithm?

Using an algorithm can save time and effort by providing a systematic approach to solving a problem. It can also improve accuracy and consistency in results. Additionally, algorithms can be used to handle complex tasks and process large amounts of data more efficiently.

Similar threads

  • Programming and Computer Science
Replies
4
Views
908
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
605
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
872
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
33
Views
6K
Back
Top