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

two different programs 1. (50pts) Run-length encoding: (a) Write a c++ program t

ID: 3593724 • Letter: T

Question

two different programs

1. (50pts) Run-length encoding:

(a) Write a c++ program that prompts users to input a decimal number and prints out the runlength encoding with 5-bit code.

(b) Write a c++ program that prompts users to input a compressed message (i.e. a binary number) encoded using 4-bit run-length encoding, and prints out the uncompressed message (i.e. the original decimal number). Assume that the users will always input a 0-1 message whose length is a multiple of 4 so that the 0-1 message is a valid message

Explanation / Answer

1.a

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define MAX_RLEN 50

char *encode(char *src){

int length;

char count[MAX_RLEN];

int len = strlen(src);

char *destination = (char *)malloc(sizeof(char)*(len*2 + 1));

int i, j = 0, k;

for(i = 0; i < len; i++){

length = 1;

while(i + 1 < len && src[i] == src[i+1]){

length++;

i++;

}

sprintf(count, "%d", length);

for(k = 0; *(count+k); k++, j++){

destination[j] = count[k];

}

}

destination[j] = '';

return destination;

}

int main(){

char str[] = "geeksforgeeks";

char *res = encode(str);

printf("%s", res);

getchar();

}

1.b.

#include<iostream>
using namespace std;

int main()
{
long bin, dec = 0, rem, num, base = 1;

cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout << "The decimal equivalent of " << bin << " : " << dec << endl;
return 0;
}