Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In language C, do not need to use array Bonus Question [10]: A binary sequence (

ID: 3872022 • Letter: I

Question

In language C, do not need to use array

Bonus Question [10]: A binary sequence (consisting of only O's and 1's) which contains long runs of O's and of 1's can be efficiently compressed using run-length coding. This method represents the binary sequence as a sequence of integers containing the sizes of the runs of 0's and 1's in the binary sequence. More specifically, the integer sequence is determined as follows. The first integer represents the number of O's at the beginning of the sequence until the first 1. The second integer represents the number of l's until the following 0. The third integer represents the number of subsequent 0's until the next 1, and so on 9. Exl: 00000110000000111 leads to 5,2,7,3 Finally, this sequence of integers will be converted into a binary stream (for instance by using the binary representation of each integer). In this exercise you are asked to write two programs a) the first one to convert a binary sequence to the corresponding sequence of integers; b) the second one to do the reversed conversion from a sequence of positive integers into a binary stream. Each program has to read its input from a text file (named, for instance, inputfilel txt, respectively inputfile2.txt) and has to write the result into another text file The first input file contains a positive integer representing the total number of digits (actually bits) in the binary sequence. Then the digits in the sequence follow, with consecutive digits separated by one blank space. The output file should contain the corresponding sequence of integers with consecutive integers separated by one blank space inputfilel for Ex. 1: 17000001 10000000111 outputfilel for Ex. 1: 5273 The second input file starts with a positive number representing the number of integers in the sequence followed by a blank. Then the sequence of integers follows, with consecutive integers separated by one blank space. The second output file must have the same format as inputfilel inputfile2 for Ex. 1: 45273 To create an input file complying to the above specified format use simple text editors such as Notepad or Textpad. When using Netbeans you have to include the input file into the corresponding proiect by doing the following, First save the file in the directory containing the proiect. In the

Explanation / Answer

Please Read comments in code and comment if any problems Thanks #include #include void binaryTOintegerCoding(char * inFilename, char * outFilename); void integerTObinaryCoding(char * inFilename, char * outFilename); int main(int argc, char *argv[]) { //binaryTOintegerCoding("/home/ramkumar/Study/Chegg/QT/build-Chegg_BinaryCoding-Desktop-Debug/inputfile1.txt","/home/ramkumar/Study/Chegg/QT/build-Chegg_BinaryCoding-Desktop-Debug/outputfile1.txt"); integerTObinaryCoding("/home/ramkumar/Study/Chegg/QT/build-Chegg_BinaryCoding-Desktop-Debug/inputfile2.txt","/home/ramkumar/Study/Chegg/QT/build-Chegg_BinaryCoding-Desktop-Debug/outputfile2.txt"); return 0; } /** * This method converts Binary Sequence to Integer Coding as per given problem * */ void integerTObinaryCoding(char * inFilename, char * outFilename){ FILE * inputFile,*outputFile; //Open input file in Read Only mode inputFile = fopen(inFilename, "r"); //Open output file in Write mode outputFile = fopen(outFilename, "w"); int lengthOfSeq = -1; //Read first integer , Length of the sequence fscanf(inputFile, "%d", &lengthOfSeq); //If lengthOfSeq < 1 then not valid if(lengthOfSeq < 1) { printf("Invalid file "); return ; } int input; char seq0 = 1,seq1 = 0; int count = 0; while(lengthOfSeq > 0 && ( fscanf(inputFile, "%d", &input)) == 1) { if(input == ' ' || input == ' ') continue; lengthOfSeq--; //Currently counting 0's if(seq0){ while(input > 0){ fprintf(outputFile, "%c ", '1'); input--; } } else if(seq1){ while(input > 0){ fprintf(outputFile, "%c ", '0'); input--; } } if(seq0){ seq1 = 1; seq0 = 0; } else{ seq1 = 0; seq0 = 1; } } fclose(inputFile); fclose(outputFile); } /** * This method converts Binary Sequence to Integer Coding as per given problem * */ void binaryTOintegerCoding(char * inFilename, char * outFilename){ FILE * inputFile,*outputFile; //Open input file in Read Only mode inputFile = fopen(inFilename, "r"); //Open output file in Write mode outputFile = fopen(outFilename, "w"); int lengthOfSeq = -1; //Read first integer , Length of the sequence fscanf(inputFile, "%d", &lengthOfSeq); //If lengthOfSeq < 1 then not valid if(lengthOfSeq < 1) { printf("Invalid file "); return ; } char input; char seq0 = 1,seq1 = 0; int count = 0; while(lengthOfSeq > 0 && ( fscanf(inputFile, "%d", &input)) == 1) { if(input == ' ' || input == ' ') continue; lengthOfSeq--; //Currently counting 0's if(seq0){ //If 0 then increment count if(input == '0'){ count++; } //If 1 write 0's count to file and set count to 1 else{ fprintf(outputFile, "%d ", count); count = 1; seq0 = 0; seq1 = 1; } } else if(seq1){ if(input == '1'){ count++; } else{ fprintf(outputFile, "%d ", count); count = 1; seq0 = 1; seq1 = 0; } } } if(count > 0) fprintf(outputFile, "%d ", count); fclose(inputFile); fclose(outputFile); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote