Question

In: Computer Science

please submit the C code( no third party library). the C code will create output to...

please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.

Solutions

Expert Solution

The following code is written in C language in which I have considered if any keyboard hit occurs in 1 second time period then we will write 1 in the output file but skip the remaining second. This is well explained in the comments.

// Library imported
#include<stdio.h> 
#include<string.h> 
#include<time.h> 
//Time delay function for 1 second delay
int delay(int number_of_seconds) 
{  
    clock_t start_time = clock(); 
    // 1000*number_of_seconds converts second to milliseconds
    // while loop will work for 1 second time period
    while (clock() < start_time + 1000 * number_of_seconds) 
        {
        // kbhit is used to check keyboard hits
                if(kbhit())
                {
                        char ch='\0';
                        ch=_getche();
                        //once keyboard button is pressed we take it as input
                        //  and store it in temporary varaible
                        // _getche is used to avoid pressing enter after some character
                        printf("\r\n");
                        // Just to make console screen proper previous line used
                        return 1;
                        // if any keyboard hits in 1 second peroid we will return 1
                }
                }
                return 0;
                // if no keyboard hit in 1 second peroid we will return 0
} 
  
int main( ) 
{ 

        FILE *filePointer ; 
        // declaring filePointer

        filePointer = fopen("output.txt", "w") ; 
        // opening file named output.txt it will always be empty in begining
        if ( filePointer == NULL ) 
        { 
           // if some exception occurs
                printf( "Failed to open" ) ; 
        } 
        else
        { 
                int i; 
                // 60 seconds loop start
        for (i = 0; i < 60; i++) { 
        printf("%d iteration\r\n",i+1);
        // printing every iteration so you can keep track on time 
        int s=delay(1);
                if(s==0){
        fputs("0", filePointer) ; 
        // write 0 in file if delay fn returns 0
        }
        else{
                fputs("1", filePointer) ; 
            // write 1 in file if delay fn returns 1
                }
         } 
                fclose(filePointer) ; 
                // close the file
                printf("End of the Code") ; 
                //end of the code
        } 
        return 0;                
} 

You can also change some part of code to make different implementations.  

If any doubts feel free to ask

Thanks


Related Solutions

C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
provide a C code (only C please) that gives the output below: ************************************ *         Menu HW...
provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1. Creating polynomials * * 2. Adding polynomials * * 3. Multiplying polynomials. * * 4. Displaying polynomials * * 5. Clearing polynomials. * * 6. Quit. * *********************************** Select the option (1 through 6): 7 You should not be in this class! ************************************* *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1. Creating polynomials...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
Please do this in C++ only. Please Show your code and your output too. Would you...
Please do this in C++ only. Please Show your code and your output too. Would you focus on number 2 in the Required functions:? Please use all the variables in the program. The assignment problem: You are asked to develop an application that prints a bank’s customers' names, IDs, and accounts balances in different ways. In this program, you need to read a file of customer’s data into different arrays, pass these arrays to functions as (array parameter), calculate the...
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
Third-party access has become a big problem as the security of the third-party plays a critical...
Third-party access has become a big problem as the security of the third-party plays a critical role in ensuring the security of the primary. How can this situation be handled best? Can A third-party can be both an internal and external threat.
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the...
Please follow the instructions and solve it by C++. Thank you! What to Submit Submit the following: 1) Your .cpp file of the solution. 2) For what n value (approximately) does your computer stop producing output? Why is this? Enter your answer in the 'Comments' field when you submit the file.   So far in our study of recursion we identified a few common recursive number sequences, such as the Fibonacci numbers. Number sequences like this are not recursive algorithms themselves...
Please create a Java Eclipse code and show output and explanation. MUST USE EXCEPTION! Step 1....
Please create a Java Eclipse code and show output and explanation. MUST USE EXCEPTION! Step 1. Ask users how much money was spent on an orange. Step 2. Then ask the user how many oranges they purchased Step 3. Lastly calculate the price for each orange purchased.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT