Hi, I think the question is not to FORM A MATRIX, but to PRINT the elements in the form of spiral matrix, where the problem of SEQUENTIAL OUTPUT comes...
-Vikram On 12/6/06, Gene <[EMAIL PROTECTED]> wrote: > > > > hijkl wrote: > > this question was asked by Google.. > > "Write a program of spiral matrix" > > ie. it takes inputs and puts in to matrix as a spiral..example. > > given : 3 X 4 matrix > > your input in this order : 1 5 8 9 10 7 4 8 0 2 3 6 > > will generate following matrix > > > > 1 5 8 9 > > 2 3 6 10 > > 0 8 4 7 > > > > and big O notation for this.. > > O(k) where k = mn is the number of inputs and the matrix is m x n. > > Let i0 and i1 be top and bottom rows of the unfilled portion; j0 and j1 > are left- and rightmost unfilled columns. Then just walk around the > edge of the unfilled space, updating as you go. You're done if there > are no unfilled rows or columns left. > > i0 = j0 = 0; i1 = m - 1; j1 = n - 1; > for (;;) { > if (i0 > i1) break; > for (i = i0++, j = j0; j <= j1; j++) a[i][j] = next_input(); > if (j0 > j1) break; > for (i = i0, j = j1--; i <= i1; i++) a[i][j] = next_input(); > if (i0 > i1) break; > for (i = i1--, j = j1; j >= j0; j--) a[i][j] = next_input(); > if (j0 > j1) break; > for (i = i1, j = j0++; i >= i0; i--) a[i][j] = next_input(); > } > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups-beta.google.com/group/algogeeks -~----------~----~----~----~------~----~------~--~---