Write a program which appears to display a vertical bar character ‘|’ moving lef
ID: 3862729 • Letter: W
Question
Write a program which appears to display a vertical bar character ‘|’ moving left to right in 79 positions across the screen, then back right to left across the screen. (Hint: If you have just displayed a ‘|’, the cursor will be just to the right of it. To ‘move’ the character one space to the right, you must display the characters Backspace (8),’ ‘ (a space, to erase the old ‘|’), and a new ‘|’. If you display the ‘|’ in the last, or 80th, position in the screen, the cursor will go to the next line and you won’t be able to get it back to the previous line (with what you know).
Explanation / Answer
#include <stdio.h>
#include <time.h>
// to introduce the delay
void delay(int seconds)
{
time_t t = time(NULL);
while (difftime(time(NULL), t) < seconds) ;
}
int main()
{
int i=0;
for(; i< 10; i++) {
printf(" |");
fflush (stdout); // to force the output to stdout
delay(1);
}
printf(" ");
for(i=0; i< 10; i++) {
printf("| ");
fflush (stdout);
delay(1);
}
printf(" ");
}
Save it as main.c. You will find the expected output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.