The following code is for printing the elements of a matrix in spiral order. The program works fine. The problem, however, is that the online compiler against which I\'m checkin
You can set a boolean variable isFirst
to true
before your printing out any stuff, and test it before each printf
statement. If isFirst
, do not print a space but set isFirst
to false
; else print a single space. After that, continue with printing your number without a space.
Alternative: Instead of printing your results immediately, create a results
array. Store your results in there, and when done, print out the results in a tight loop. You can print the first number without a leading space, then loop over the remainder and print them with a leading space.