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',...
Please, write this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
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...
Please, write code in c++. Using iostream library A chessboard pattern is a pattern that satisfies...
Please, write code in c++. Using iostream library A chessboard pattern is a pattern that satisfies the following conditions: • The pattern has a rectangular shape. • The pattern contains only the characters '.' (a dot) and 'X' (an uppercase letter X). • No two symbols that are horizontally or vertically adjacent are the same. • The symbol in the lower left corner of the pattern is '.' (a dot). You are given two numbers. N is a number of...
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT