Decompress Function.

Mar 1, 2018 at 2:34am
Hi everyone, previously I had to write a compress function, which I will put below. Now I have to write a decompress function, I am lost on this I do not really know how to do this. Any help is appreciated.

unsigned int compress(unsigned int age, unsigned int grade, char sex, double GPA)
{

if ((age < 3) || (age > 18)) return 0;
if ((sex != 'M') && (sex != 'F')) return 0;
if (grade > 15) return 0;
if ((GPA < 0.0) || (GPA > 4.0)) return 0;

age -= 3;
sex = (sex == 'F') ? 0 : 1;

unsigned int gpa = (unsigned int)GPA;
GPA -= gpa;
GPA *= 10.0;
unsigned int gpa_frac = (unsigned int)(GPA + 0.5);

return (gpa_frac) + (gpa << 4) + (sex << 7) + (grade << 8) + (age << 12);
}

Here is my main for the decompress function.

// decompress: decompresses the low-order 16 bits of info and
// returns age, grade, sex, and GPA
void decompress(unsigned int info, unsigned int& age,

unsigned int& grade, char& sex, double& GPA);


extern const int N = sizeof(int) * CHAR_BIT; // # of bits in an int

int main()
{
unsigned int age, grade, info;
char sex;
double GPA;

// Prepare for floating-point output format; show one digitafter
// the decimal point
cout << fixed << showpoint << setprecision(1);

printLine(78, cout);

cout << setw(76) << "33222222222211111111110000000000" << endl;
cout << " Age Grade Sex GPA Compress "
<< "10987654321098765432109876543210" << endl;

printLine(78, cout);
while (cin >> info)
{
decompress(info, age, grade, sex, GPA);
cout << setw(5) << age << setw(8) << grade << setw(7) << sex
<< setw(8) << GPA << setw(11) << info << setw(5) << " "<< bitset<N>(info) <<endl;
}

printLine(78, cout);
return EXIT_SUCCESS;
}
void printLine(int length, ostream& os)
{
char ch = os.fill();
os << setfill(’-’) << setw(length) << "-" << setfill(ch) << e
ndl;
}
Mar 1, 2018 at 3:42am
Please don't delete your posts when you get answer.
Mar 1, 2018 at 3:52am
I am not anymore. It was selfish and stupid. Apologies kbw.
Mar 1, 2018 at 10:57am
This is your compression.
 
return (gpa_frac) + (gpa << 4) + (sex << 7) + (grade << 8) + (age << 12);


Are you able to describe the algorithm in words?

The decompress will be the converse, express that in words, then translate it to code.
Mar 1, 2018 at 12:22pm
Wow, helpful to see it that way. Thanks.
Topic archived. No new replies allowed.