The code Should write in C language with unix gcccompiler. Computer systems use
ID: 3614626 • Letter: T
Question
The code Should write in C language with unix gcccompiler. Computer systems use checksums in many situations to do errorchecking. A brief discussion on checksums will be presented in class. you will beimplementing one method of computing checksums for a file containing integers. Command Line Details: The executable version of your programfor Part (a) must be named p4a. The program will be executed by a command line of thefollowing form: p4a inputfile outputfile In the command line, the first argument (inputfile) specifiesthe name of the input file and the second argument (outputfile) specifies the name of the output filethat your program must generate. The input file and the output file are both text files. The input filecontains integers, with each line containing exactly four integers. The output file must have the samenumber of lines as the input file. Each line of the output file must contain five integers; the first fourof these integers are the same as those in the corresponding line of the input file. The fifth integer isthe checksum for the four integers on that line. Each of the integers (including the checksum) needs only2 bytes; that is, it fits in a variable of type short. The method for computing the checksum is explainedbelow. Computing the Checksum: Imagine that the 16 bits of a shortinteger are numbered 0 through 15 from right to left. Suppose the four integers on a line ofthe input file are stored in variables v1, v2, v3 and v4 (of type short). Let the variable checksum (also oftype short) store the checksum of the four integers. The 16 bits of checksum are determined asfollows. 1. The four bits in positions 15, 14, 13 and 12 of checksummust be the corresponding bits of v1. 2. The four bits in positions 11, 10, 9 and 8 of checksum mustbe the corresponding bits of v2. 3. The four bits in positions 7, 6, 5 and 4 of checksum mustbe the corresponding bits of v3. 4. The four bits in positions 3, 2, 1 and 0 of checksum mustbe the corresponding bits of v4.Assumptions: You can make the following assumptions about theinput file. (a) The input file will have at least one line. (b) Each line of the input file will have exactly fourintegers, each of which needs only 16 bits. Errors to be detected: You need to check only for the usualcommand line errors (wrong number of parameters on the command line, the input or the outputfile can’t be opened). In such a case, your program must output a suitable error message to stderr andstop.
Goal: To write a program that computes checksums for a file containing integers. Note: Checksums are used to detect errors. (They are used in hardware as well as software components of a computer system.) Command line: % p4a infile outfile Notes about the input file: • The input file contains at least one line. Errors to be detected: Usual command line errors (wrong number of command line arguments, one of the specified files can’t be opened).
• Each line contains exactly four integers. • Each integer fits in 16 bits (i.e., in a variableof type short). Notes about the output file: • The number of lines in the output file is thesame as that of the input file. • Each line of the output file contains exactlyfive integers. The first four integers are the same as those in the corresponding line of the input file; the last integer is the checksum. • The checksum is also 16 bits long (i.e., can be stored in a variable of type short). Example of checksum computation: Suppose a line of the input file has -14052 23228 57 -31819 Let the above values be read into variables v1, v2, v3 and v4 (of type short) respectively.
v1: 1100 1001 0001 1100 v2: 0101 1010 1011 1100 v3: 0000 0000 0011 1001 v4: 1000 0011 1011 0101 checksum: 1100 1010 0011 0101 So, the line written to output file is -14052 23228 57 -31819 -13771 Programming Suggestions: • Use format "%hd" to read and print variables of type short. (There is no need to use strtok.) • Use suitable masks and bitwise operations to construct the checksum.
The code Should write in C language with unix gcccompiler. Computer systems use checksums in many situations to do errorchecking. A brief discussion on checksums will be presented in class. you will beimplementing one method of computing checksums for a file containing integers. Command Line Details: The executable version of your programfor Part (a) must be named p4a. The program will be executed by a command line of thefollowing form: p4a inputfile outputfile In the command line, the first argument (inputfile) specifiesthe name of the input file and the second argument (outputfile) specifies the name of the output filethat your program must generate. The input file and the output file are both text files. The input filecontains integers, with each line containing exactly four integers. The output file must have the samenumber of lines as the input file. Each line of the output file must contain five integers; the first fourof these integers are the same as those in the corresponding line of the input file. The fifth integer isthe checksum for the four integers on that line. Each of the integers (including the checksum) needs only2 bytes; that is, it fits in a variable of type short. The method for computing the checksum is explainedbelow. Computing the Checksum: Imagine that the 16 bits of a shortinteger are numbered 0 through 15 from right to left. Suppose the four integers on a line ofthe input file are stored in variables v1, v2, v3 and v4 (of type short). Let the variable checksum (also oftype short) store the checksum of the four integers. The 16 bits of checksum are determined asfollows. 1. The four bits in positions 15, 14, 13 and 12 of checksummust be the corresponding bits of v1. 2. The four bits in positions 11, 10, 9 and 8 of checksum mustbe the corresponding bits of v2. 3. The four bits in positions 7, 6, 5 and 4 of checksum mustbe the corresponding bits of v3. 4. The four bits in positions 3, 2, 1 and 0 of checksum mustbe the corresponding bits of v4.
Assumptions: You can make the following assumptions about theinput file. (a) The input file will have at least one line. (b) Each line of the input file will have exactly fourintegers, each of which needs only 16 bits. Errors to be detected: You need to check only for the usualcommand line errors (wrong number of parameters on the command line, the input or the outputfile can’t be opened). In such a case, your program must output a suitable error message to stderr andstop.
Goal: To write a program that computes checksums for a file containing integers. Note: Checksums are used to detect errors. (They are used in hardware as well as software components of a computer system.) Command line: % p4a infile outfile Notes about the input file: • The input file contains at least one line. Errors to be detected: Usual command line errors (wrong number of command line arguments, one of the specified files can’t be opened). Goal: To write a program that computes checksums for a file containing integers. Note: Checksums are used to detect errors. (They are used in hardware as well as software components of a computer system.) Command line: % p4a infile outfile Notes about the input file: • The input file contains at least one line. Errors to be detected: Usual command line errors (wrong number of command line arguments, one of the specified files can’t be opened). Errors to be detected: Usual command line errors (wrong number of command line arguments, one of the specified files can’t be opened).
• Each line contains exactly four integers. • Each integer fits in 16 bits (i.e., in a variableof type short). Notes about the output file: • The number of lines in the output file is thesame as that of the input file. • Each line of the output file contains exactlyfive integers. The first four integers are the same as those in the corresponding line of the input file; the last integer is the checksum. • The checksum is also 16 bits long (i.e., can be stored in a variable of type short). Example of checksum computation: Suppose a line of the input file has -14052 23228 57 -31819 Let the above values be read into variables v1, v2, v3 and v4 (of type short) respectively.
v1: 1100 1001 0001 1100 v2: 0101 1010 1011 1100 v3: 0000 0000 0011 1001 v4: 1000 0011 1011 0101 checksum: 1100 1010 0011 0101 So, the line written to output file is -14052 23228 57 -31819 -13771 Programming Suggestions: • Use format "%hd" to read and print variables of type short. (There is no need to use strtok.) • Use suitable masks and bitwise operations to construct the checksum. • Each line contains exactly four integers. • Each integer fits in 16 bits (i.e., in a variableof type short). Notes about the output file: • The number of lines in the output file is thesame as that of the input file. • Each line of the output file contains exactlyfive integers. The first four integers are the same as those in the corresponding line of the input file; the last integer is the checksum. • The checksum is also 16 bits long (i.e., can be stored in a variable of type short). Example of checksum computation: Suppose a line of the input file has -14052 23228 57 -31819 Let the above values be read into variables v1, v2, v3 and v4 (of type short) respectively.
v1: 1100 1001 0001 1100 v2: 0101 1010 1011 1100 v3: 0000 0000 0011 1001 v4: 1000 0011 1011 0101 checksum: 1100 1010 0011 0101 So, the line written to output file is -14052 23228 57 -31819 -13771 Programming Suggestions: • Use format "%hd" to read and print variables of type short. (There is no need to use strtok.) • Use suitable masks and bitwise operations to construct the checksum.
Explanation / Answer
please rate - thanks #include int main(int argc, char * argv[]) {short v1,v2,v3,v4,csum,ck1,ck2,ck3,ck4; FILE *input, *output; input = fopen(argv[1],"r"); if(input == NULL) { printf("Error opening input file! "); return 0; } output= fopen(argv[2],"w"); if(output == NULL) { printf("Error opening output file! "); return 0; } while(fscanf(input,"%hd%hd%hd%hd",&v1,&v2,&v3,&v4)>0) {ck1=v1&0xF000; ck2=v2&0xF00; ck3=v3&0xF0; ck4=v4&0xF; csum=ck1|ck2|ck3|ck4; fprintf(output,"%hd %hd %hd %hd %hd ",v1,v2,v3,v4,csum); } fclose(input); fclose(output); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.