15,945,603 members
1.00/5 (1 vote)
See more:
I am trying to compress binary numbers via using Run Length Compressing but i can not be successful i am writing my code. PLEASE HELP ME... Thanks for all your appreciation!!!

MY CODE:

```#include <stdio.h>
#include <stdlib.h>
#define max 100
#define middle 50
#define cMax 1000
int main()
{
int entrance = 0;
int i = 0;
int k = 0;
int j = 0;
int diffrence = 0;
int zeroCounter = 0;
int oneCounter = 0;
int *p[max] = { 0 };
int *zeroOne[middle] = { 0 };
int *one[middle] = { 0 };
int *convertedNumber[cMax] = { 0 };
do{
printf("Please enter integer 1 for Compress, 2 for Decompress\n");
scanf("%d",&entrance);
if(entrance == 1){
printf("Please Enter The Binary You Want to Compress\n");
scanf("%s",p);
for(i=0;p[i]!=0;i++)
{
if(p[i] == 0)
{
if(oneCounter!=0)
{
for(k=k;k<(k + oneCounter-1);k++){
convertedNumber[k] = 0;
}
oneCounter = 0;
}
zeroCounter++;
}
else if(p[i]==1 && i!= 0)
{
if(zeroCounter!=0)
{
for(k=k;diffrence>2;k++)
{
convertedNumber[k] = zeroCounter % 2;
}
zeroCounter = 0;
}
oneCounter++;
}
else{
if(i!=0)
{
break;
}
}
}
for(j=0;convertedNumber != 0;j++){
printf("%d",convertedNumber[j]);
if (convertedNumber== 0)
break;
}

}
}while(entrance != 1 && entrance != 2);
printf("Hello world!\n");
return 0;
}```
Posted
Sergey Alexandrovich Kryukov 10-Oct-11 15:35pm
OK, where is your compress/decompress functions? What's the problem? Why anyone would be interested in looking at this code without precise problem report from you?
--SA

## Solution 1

First of all, "run length compression" is a bit too wide term. You can compress data using run-length approach in a number of similar ways. Do you mean RLE? The here is the implementation:

As to your C code, I cannot see even the attempt of creation of encoding/decoding function. Instead, you've just written some `main` function. Why? Ever heard of functions and other means of abstraction? Don't entangle I/O into algorithm, to start with.

—SA