In: Computer Science
Can you solve this C program by using Function?
Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
#include <stdio.h>
#include <time.h>
void ring_bell(int n , int sec){
// n= no. of times bell will ring
//sec= delay in seconds between each ring
int miliseconds=1000 * sec;
// convert seconds to miliseconds
for(int i=1;i<=n;i++){
clock_t ct=clock();
//clock() method Returns the processor clock time used since the beginning of an implementation, clock_t is the return type of clock() method
printf("\a");
// \a escape sequence rings the computer bell
while(clock() < ct + miliseconds);
//this is adding the delay between each ring
//looping at one place until some specified time is passed
}
}
int main() {
ring_bell(10,2); // function call 10 rings with 2 sec delay
return 0;
}