|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e8/solution/p5.c
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
/**
* Use this function to get a random integer between a and b inclusive.
* ex: R(0,10) -> a random number between 0 and 10
*/
int R(int a, int b)
{
int d = (a < b? b-a : a-b) + 1;
return a + (rand()%d);
}
/**
* make a loop to read a character using getch() and for each character read,
* print "Hello, world!" at a random location using the R() function defined
* above. The y value should range between 0 and LINES-1 and the x value between
* 0 and COLS-10. When a 'q' or 'Q' is read by getch() you should exit the
* loop and then the program.
*/
int main(void)
{
int ch;
initscr();
clear();
while((ch = getch()) != ERR) {
if (ch == 'q' || ch == 'Q') break;
mvprintw(R(0,LINES-1), R(0,COLS-10), "Hello, world!");
refresh();
}
endwin();
return 0;
}
|