Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write in C Programming for my understanding You are being asked to implement a timed quiz which stores the questions and answers in different files
Write in C Programming for my understanding
You are being asked to implement a timed quiz which stores the questions and answers in different files named quest.txt and ans.txt (Note: these are standard text files to allow you to edit the questions and answers in a text editor; them being text files does not alter how you use the read system call to read the data. The quiz should display a question and wait 20 seconds for the user to enter an answer; if the user enters an answer within the time limit, it should display whether the answer was correct. If the user failed to enter an answer within the time limit, the program should inform the user the question has expired; in either case, the program should reset the timer and display the next question. This program should be implemented using interval timers, signals, and any other necessary system calls. "All of the includes for the project go here those includes must support signals, interval timers, read, open, and close. You will also need support for error numbers "/ \#include \#include Problem 1 - complete the list of include files \#define BUF_SIZE 1024 int timedOut; // this integer represents a C Boolean value that is set to "true" if the SIGALRM is received /" The PrintS function takes a constant c-string and outputs it to STDOUT using write feel free to use string functions like strcmp, strcat, etc to make this one work. PrintS returns 0 on failure and 1 on success "/ int PrintS(const char *str) \{ Problem 2 - implement PrintS using write ) / the Printint function takes a constant int and outputs it to STDOUT using write feel free to use of a function like sprint to convert an integer to a string Printint returns 0 on failure and 1 on success Note: you will need to convert the int to a c-string to make it readable \% int Printint(const int val) \} / implement the signal handler below ... use the provided prototype it must handle two signals, SIGINT and SIGALRM if the signal handler receives SIGINT prompt the user if they want to exit or not on yes, exit successfully, on no, just return from the handler on SIGALRM, set the timedOut flag to 1% void signalHandler(int sig) \{ I* Problem 4 - implement the signalHandler * / " when writing the prompt for SIGINT and reading the reply use system calls ... please also remember to read more than 1 character when reading the response since that response has at least two characters in it. Yes the responses of ' Y ' and ' n ' involve multiple characters \% \} " This function reads a line of text from a file we are simulating another functions behavior here .. the basic idea is to read the file character by character til we find the in or ir (newline and carriage return characters) Go ahead and tack the null character on here for simplicity Return a 0 if no characters were read, return a 1 otherwise "/ int readline(int fd, char "line) \{ I Problem 5 - implement readLine as described above */ " Remember to use read to do this .... This is probably best accomplished by reading in 1 character at a time and then adding them to the array you passed in as line*/ 3 " This function reads a question answer pairing from the provided pair of file descriptors it returns 0 if the files are empty and 1 if if successfully reads the pairing notice it calls the custom function readLine since we need to read only one line at a time*/ 1 Problem 5 - implement readLine as described above * I Remember to use read to do this .... This is probably best accomplished by reading in 1 character at a time and then adding them to the array you passed in as line* } " This function reads a question answer pairing from the provided pair of file descriptors it returns 0 if the files are empty and 1 if if successfully reads the pairing notice it calls the custom function readLine since we need to read only one line at a time*/ int readQA(int questFd, int ansFd, char "quest, char "ans) \{ if ( readLine(questFd, quest )=0 ) return 0 ; if (readLine(ansFd, ans) ==0 ) return 0 ; return 1; \} int main(int argc, char * argv[]) \{ int numRead =0; int numWrite =0; int question =1; int correct =0 char buf[BUF_SIZE]; char quest[BUF_SIZE]; char ans[BUF_SIZE]; int questFd, ansFd; /" declare structures for signals and timers. you will need to turn the timer on and off during this program ... turning the timer on sets the timer's value to 20 secs turning it off sets the timer's value to 0 secs you can do this with one or two struct itimervals I am using two, so the questions are designed to support two* I* Problem 6 a- declare the structures for the signals and timers / I* initialize structures for signals and timers make certain to actually set a mask this time add both SIGINT and SIGALRM to the mask* l Problem 6b - initialize the structure for the signal handlers *I 1 " you get 20 seconds a question ... this timer has an interval of 0 and a value of 20% l Problem 6c - initialize the structure for the 20sec timer / I" you also need to disable the timer when testing this is done by setting interval and value to 0 I suggest creating another struct itimerval \% 1 Problem 6d initialize the structure for the 0 sec timer, ignore this prompt if you chose to use only one struct itimerval */ l Problem 6c initialize the structure for the 20sec timer / / you also need to disable the timer when testing this is done by setting interval and value to 0 I suggest creating another struct itimerval \% l Problem 6d initialize the structure for the 0sec timer, ignore this prompt if you chose to use only one struct itimerval * * set up the signal handlers * l Problem 7 - associate the SIGINT and SIGALRM with their signal handler / / open the question and answer files .. these are simple text files called quest.txt and ans.txt.\% I Problem 8 - open the files quest.txt and ans.txt * I" this loop handles the Q/A stuff I have included some of it to make your life simpler I have also left some comments to assist you as well read the first question, answer pairing prior to entering the loop \% readQA(questFd, ansFd, quest, ans); while (1) \{ /" output the current question */ PrintS("\#"); Printint(question); PrintS(" "); PrintS(quest); PrintS("? "); / we will set the timedOut flag to 0, since it hasn't yet % timedOut =0; I Problem 8 - open the files quest.txt and ans.txt // /" this loop handles the Q/A stuff I have included some of it to make your life simpler I have also left some comments to assist you as well read the first question, answer pairing prior to entering the loop \% readQA(questFd, ansFd, quest, ans); while (1) \{ / output the current question " PrintS("\#"); Printint(question); PrintS(" "); PrintS(quest); PrintS("? "); / " we will set the timedOut flag to 0 , since it hasn't yet % timedOut =0; I now set the interval timer prior to reading in the user's response % I Problem 9 - set the interval timer to go off in 15 seconds / / read in the user's response from STDIN, use read instead of scanf % I Problem 10 - read a buffer from STDIN */ " make certain to store the return value of your read in the correct location, it is used in the provided code % if (numRead ==0 ) break; / disable the timer here I Problem 11 - disable the time by setting the timer's values to 0 seconds *I /" check the answer buf[numRead-1] =0 if ( strncmp ( buf,ans )==0) \{ correct++; PrintS("lncorrectln"); \} else \{ PrintS(ans); PrintS(ans); PrintS("lnwrongln"); \} / read the next question .. break if there are no more questions if (readQA(questFd, ansFd, quest, ans) == 0) break; question++; \} PrintS("final score is ")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started