/* File: inch4.c Draw an inch worm, inching across the screen. This time using a for loop. Now, draw the food pellet, too. Use functions to make our code more readable. */ #include #include void print_spaces(int n) ; void clear_screen(void) ; int main() { int i ; for ( i=0 ; i <= 20 ; i++ ) { clear_screen() ; print_spaces(i) ; printf("_/\\_") ; print_spaces(21-i) ; printf("o\n") ; usleep(350000) ; clear_screen() ; print_spaces(i) ; printf("_____") ; print_spaces(21-i-1) ; printf("o\n") ; usleep(350000) ; } // Draw the inch worm eating the pellet clear_screen() ; print_spaces(21) ; printf("_/\\_o\n") ; usleep(650000) ; clear_screen() ; print_spaces(21) ; printf("____o\n") ; usleep(450000) ; clear_screen() ; print_spaces(22) ; printf("_/\\o\n") ; usleep(450000) ; clear_screen() ; print_spaces(22) ; printf("___o_\n") ; usleep(450000) ; clear_screen() ; print_spaces(23) ; printf("_/o_\n") ; usleep(450000) ; clear_screen() ; print_spaces(24) ; printf("_o___\n") ; usleep(450000) ; clear_screen() ; print_spaces(25) ; printf("o/\\_\n") ; usleep(650000) ; clear_screen() ; print_spaces(25) ; printf("._____\n") ; usleep(350000) ; return 0 ; } /* function: print_spaces(n) Prints n spaces to the screen No return value */ void print_spaces(int n) { int j ; for ( j=1 ; j <= n ; j++ ) { printf(" ") ; } return ; } /* function: clear_screen() Use VT100 escape sequences to clear the screen. */ void clear_screen() { printf("\033[2J"); // clear screen printf("\033[H"); // go home return ; }