Question: Find and fix the bugs in the following C# program using System; /* Many businesses need to convert data from one format to another. In

Find and fix the bugs in the following C# program

using System; /* Many businesses need to convert data from one format to another. In this case, data is streamed in sequence, but the data needs to be represented as a two-dimensional array. The idea is to convert the sequential data (vector) into a table (matrix), along the lines of a spreadsheet. Assume that a previous programmer wrote this code, but before debugging and delivering it, was transferred to a new department. Your task to finish the work. The code is almost correct, but compilation (syntax), coding (specification), or output (semantic) bugs exist. Carefully study the code, determine the problem, and fix the bug or bugs so that the output is as expected. Change as little code as possible to correct the problem. Place a comment on or before the fixed lines explaining what is wrong and how the fix resolves the issue--no comment(s), no points. */ namespace FFTB05 { class driver { // create a matrix with row size of 'width' from the data in 'vector' // pad any empty (uninitialized) elements with -1 static int[,] Transform(int width, int[] vector) { int rows = Math.Ceiling(vector.Length / width); int[,] matrix = new int[rows, width]; int row = 0; int col = 0; foreach (int n in vector) { matrix[row, col] = n; if (++col == width) { ++row; col = 0; } } int pad = rows * width - vector.Length; for (int i = 0; i < pad; ++i) matrix[row, col + i] = -1; return matrix; } static void Main(string[] args) { int[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int[,] table = Transform(3, data); Console.WriteLine("the new data in the table should be: "); Console.WriteLine("1 2 3 4 5 6 7 8 9 10 -1 -1 "); Console.WriteLine("the new data in a table is:"); for (int r = 0; r < table.GetLength(0); ++r) { for (int c = 0; c < table.GetLength(1); ++c) Console.Write(table[c, r] + " "); Console.WriteLine(); } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!