Looking at your code, this for
(the first one in the while
loop):
for (i = l; i < n; ++i)
printf("%d ", a[k][i]);
will always be executed at least ones (because l<n
, coming from the while
's condition).
Then you can just do the following:
- always add the space in front of the number
- add a single
if
check just for this very firstfor
-loop (use somebool
flag).
For example, something like:
bool first = true;
while (k < m && l < n)
{
for (i = l; i < n; ++i)
{
if( ! first )
{
printf(" %d", a[k][i]);
}
else
{
printf("%d", a[k][i]);
first = false;
}
}
// ....
}
This will be rather efficient and short solution - the if
is in just one loop and the flag will be true
just once (will avoid cache misses).