/* Run-length Encoding (15 points) "Run-length encoding (RLE) is a very simple form of data compression in which runs of data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and a count, rather than as the original run. This is most useful on data that contains many such runs; for example, simple graphic images such as icons and line drawings. For example, consider a screen containing plain black text on a solid white background. There will be many long runs of white pixels in the blank space, and many short runs of black pixels within the text." (Source: Wikipedia) As an example, consider this scan line, where 0 represents a black pixel and 1 represents a white pixel: 00111000110111100001 If apply the run-length encoding algorithm to that scan line, then we will get the following encoded result: 2B3W3B2W1B4W4B1W Interpret this as 2 Black pixels, 3 White pixels, 3 Black pixels, etc. Write a complete C program that reads a line of 20 pixels from the user (represented with 0's and 1's), encodes them using run-length encoding, and prints the encoding on the screen. Sample runs are shown below. ============= START OF SAMPLE RUN ======================= Original: 00001110111111100001 Encoded: 4B3W1B7W4B1W ============= END OF SAMPLE RUN ======================= ============= START OF SAMPLE RUN ======================= Original: 11110011000011100000 Encoded: 4W2B2W4B3W5B ============= END OF SAMPLE RUN ======================= ============= START OF SAMPLE RUN ======================= Original: 01001100011100001111 Encoded: 1B1W2B2W3B3W4B4W ============= END OF SAMPLE RUN ======================= ============= START OF SAMPLE RUN ======================= Original: 11111100101001100000 Encoded: 6W2B1W1B1W2B2W5B ============= END OF SAMPLE RUN ======================= ============= START OF SAMPLE RUN ======================= Original: 10101010101010101010 Encoded: 1W1B1W1B1W1B1W1B1W1B1W1B1W1B1W1B1W1B1W1B ============= END OF SAMPLE RUN ======================= */ #include #include #include #define DEBUG 0 int main() { int a[20]; char ch; int i; printf("Original: "); for(i=0; i<20; i++) { scanf("%c", &ch); a[i]= (int)(ch-'0'); } // create a random array //srand(time(NULL)); //for(i=0; i<20; i++) // a[i]=rand()%2; // print the original array if(DEBUG) { printf("Original: "); for(i=0; i<20; i++) printf("%c", a[i]+'0'); printf("\n\n"); } // use a temporary array count; zero it first int count[20]; for(i=0; i<20; i++) count[i]=0; int n=0; i=0; do { count[n]=1; while( (a[i] == a[i+1]) && (i<20) ) { count[n]++; i++; } n++; i++; }while(i<20); // print the count array char c; if(a[0]== 0) c='B'; else c='W'; printf("Encoded: "); for(i=0; i