On Sat, 18 Jul 2020 19:12:25 +0000, T. Ment wrote:
> So I started coding some output to see what's going on. After much trial
> and error, I finally got it. Here's the code:
Whoops no attachment, hit the wrong button. Try again:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <conio.h>
char *data;
int new, old; /* position */
int count, cycle;
int lsize, rsize, size;
void
show (void)
{
int x;
for (x = 0; x < size; x++) {
printf (" %c ", data[x]);
}
printf ("\n");
for (x = 0; x < size; x++) {
if (x == new)
printf (" %2d+", x);
else if (x == old)
printf (" %2d-", x);
else
printf (" ", x);
}
printf (" %2d %2d\n\n", cycle, count);
}
void
main (int argc, char **argv)
{
if (argc != 3) {
bogus:
printf ("bogus input\n");
exit (1);
}
lsize = strlen (argv[1]);
rsize = strlen (argv[2]);
size = lsize + rsize;
if (size > 16)
goto bogus;
data = calloc (size + 1, 1);
strcpy (data, argv[1]);
strcpy (data + lsize, argv[2]);
clrscr ();
count = size;
cycle = 0;
while (count) {
new = cycle;
data[size] = data[new]; /* use extra space to begin cycle */
for (;;) {
if (new < rsize) /* demarcates final blocks */
old = new + lsize; /* right moves left by left size */
else
old = new - rsize; /* left moves right by right size */
if (old == cycle) /* end of cycle */
break;
data[new] = data[old]; /* copy to new position */
data[old] = ' '; /* erase old */
show ();
new = old; /* erased old becomes next new */
--count;
}
data[new] = data[size]; /* use extra space to close cycle */
show ();
if (--count) /* not done yet, start next cycle */
++cycle;
}
exit (0);
}