Question

Q3) Write a program to generate three random numbers between 1 and 9 and display them. The user has to enter their sum as soon as possible. These two steps will be repeated continuously. An alarm will be triggered every 5 seconds to display the numb answers so far. If the user did not give a correct answer in 10 seconds, the timer will jump to the main to give an error message Too slow response! and terminate the program. Moreover, the user may hit Cntrl+z to disable the timer (by blocking the signal) and take his time for the answer. Save your source code as hw2q3.c and include it in th Compile an Show the commands and the sample runs in the report. er of correct d run the program several times to show all features described in the question. 2I Page

in c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <unistd.h>

#include <poll.h>

#include <stdio.h>

#include <stdlib.h>

int main()

{

int total;

int randnumber()

{

int fnumber,snumber,tnumber;

fnumber = rand()%10;

snumber = rand()%10;

tnumber = rand()%10;

printf("First number: %d\nSecond Number: %d\nThird number%d\n", fnumber,snumber,tnumber);

printf("enter the total of 3 number in 10 seconds\n");

return fnumber+snumber+tnumber;

}

  

int answer=randnumber();

  

struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };

if( poll(&mypoll, 1, 10000) )

{

scanf("%d", &total);

if(total==answer){

printf("correct answer - %d\n", total);

}

else{

printf("you have entered incorrect answer, try again\n");

if( poll(&mypoll, 1, 5000) )

{

scanf("%d",&total);

if(total==answer){

printf("correct answer - %d\n", total);

}

else {

printf("You have tried two attempts and both got wrong.\ncorrect answer is %d",answer);

}

}

else{

printf("Too slow response\n");

}

}

}

else

{

printf("Too slow response\n");

}

return 0;

}

output:

cc version 4.6.3
   
First number: 3
Second Number: 6
Third number7
enter the total of 3 number in 10 seconds
 6
you have entered incorrect answer, try again
 16
correct answer - 16

output2

gcc version 4.6.3
   
First number: 3
Second Number: 6
Third number7
enter the total of 3 number in 10 seconds
Too slow response
 

output3:

gcc version 4.6.3
   
First number: 3
Second Number: 6
Third number7
enter the total of 3 number in 10 seconds
 16
correct answer - 16
 

output4:

First number: 3
Second Number: 6
Third number7
enter the total of 3 number in 10 seconds
 5
you have entered incorrect answer, try again
 5
You have tried two attempts and both got wrong.
correct answer is 16   
Add a comment
Know the answer?
Add Answer to:
in c Q3) Write a program to generate three random numbers between 1 and 9 and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 06) Write a C program to perform matrix operations based of threads. The program accepts from...

    06) Write a C program to perform matrix operations based of threads. The program accepts from the user a positive integer N. A menu is given to the user: 1. generate matrices: generates three NxN matrices A, B, C with random integer numbers between 0 and 9 2. add: matrix D- A+B+C 3. subtract: matrix D A-B-C 4. display matrices: A, B, C, D 5. display count of result values more than 8. 6. exit: terminate the program When the...

  • Questions: 1) Write a simple program to create three processes using fork() commands. Use any three...

    Questions: 1) Write a simple program to create three processes using fork() commands. Use any three of the six system calls to show how each child process executes new sub-programs using exec’s minions. Show the output of each of the child process with different data samples.   2) Write a simple program in your Linux environment using C++ to identify PID, PPID and GID for the processes created in Question 1 and display the real and effective user ID.   3) Modify...

  • Write a C program that should run on Linux platform using gcc compiler. You are required...

    Write a C program that should run on Linux platform using gcc compiler. You are required to simulate threads creation and termination behavior by using POSIX threads library. Input: In the main program, first take the value for total number of threads and then ask user to provide the arrival time and CPU time (i.e. running time) for each thread. Output: Simulate the behavior of threads arrival, working and termination at a specific time interval (i.e. 500ms). Requirements: i. Name...

  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Be sure that your program...

  • To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and...

    To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and vice versa. Inputs: Number in Decimal format (including special case of 0) Number in IEEE-754 format (including special cases) Output: Equivalent number in IEEE-754 format Equivalent number in Decimal Specification: The program converts a number based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are: Decimal to IEEE-754 conversion IEEE-754 to Decimal conversion Quit program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT