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

Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
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.
Question #1 Please specify he output of the following code (2.4) # import the pandas library...
Question #1 Please specify he output of the following code (2.4) # import the pandas library and aliasing as pd import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(8, 4), index = ['a','b','c','d','e','f','g','h'], columns = ['A', 'B', 'C', 'D']) # for getting values with a boolean array print df.loc['a']>0 Question #2 Please specify the output of the following code (2.2) import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],columns=['one', 'two',...
Submit a well-commented C program.   Create code. That will build a random 8x8 2D array of...
Submit a well-commented C program.   Create code. That will build a random 8x8 2D array of small-case characters. Demonstrate how it functions by printing out the result on your screen. Add a function will print out a single line of the array. If the user enters a value between 0 and 7 to select to print out a single row of the original 2D array.                void printLine(char *,int);    // This is the prototype declaration             void printLine(char myArr[][])     //...
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 use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT