User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 375,168 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,266 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 281 | Replies: 0
Reply
Join Date: Oct 2007
Posts: 1
Reputation: j2swift is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
j2swift j2swift is offline Offline
Newbie Poster

Troubleshooting {Urgent} need help with file compression and decompression

  #1  
Oct 2nd, 2007
I am a beginner in C++ and I need some help because my decoding is coming out as a series of numbers. Here are the detail as to what I am trying to do.

I wrote 2 programs that compress and decompress a text file, each taking input and out file names as commands.
Use this Algorithm:
read input file in bytes (in Char). The first 100 byte are copied to outputfile, and is used to create model. Model keeps track of each bit, the next bit, how often do they occur. for the first 1000 bytes.

You will print out all the characters that were observed, the four most frequent next characters.

Next the model take rest of file and encode it by a prefix code. You look up character X and character Y and if Y is 1 of the 4 most frequent letters following x, we encode Y by the bit patterns 000 for most frequent, 010 to 110 for the 4th most frquent. Compression is achieved replacing bits by 3 bits. Else encode Y by 9 bit pattern( the bits of Y+1) and length increases from 8bits to 9 bits.

After finding the encoding for the next character Y, and it's length, these bits are put in a interger variable used as accumulator, behind the bits already contained in that accumulator. If Accumulator now contains at least 8 bit, we mask out the front 8bits, interpret them as char, and write the byte to the file, then shift the accumulator by 8 bits to the right. at the end , all remaining bit in the accumulator are written to the file. This finished the compression program.

To decompress, you read the first 100 byte, copy them to the decoded file, and collect the same model information. then you read from the file; if the next codeword starts with a 0, it is three bits long, and represents one of the four most probable next characters after the current character, which is looked up and written to file. Otherwise , it starts with a 1, then it is nine bits long, the leading 1-bit if deleted, and the remaining 8 bits are written to the file.

Here is the code I wrote. Can any one help me out. Since I am new a beginner in c++ can please anyone help me to get this program working. I think I might be a little confused with the bitwise functions. I am using a 256 by 256 array to keep track of the frequency of characters and I also created a sorted version of that array to keep track of most frequent character combinations.
//******************************ENCODING CODE*****************
  1. #include <iostream.h> // I/O
  2. #include <fstream.h> // file I/O
  3. #include <iomanip.h> // format manipulation
  4. #include <cstring>
  5. #include <cstddef>
  6. #include <stdio.h>
  7.  
  8.  
  9. //using namespace std;
  10. void displayBits(unsigned);
  11. const long indexSize = 256;
  12. void selectionSort(int[][indexSize],int);
  13. void swap(int, int);
  14.  
  15. int main () {
  16. ifstream fp_in,fp_in1,fp_in2; // declarations of streams fp_in and fp_out
  17. ofstream fp_out,fp_out1;
  18.  
  19. char * buffer;
  20. long size, size1;
  21. long copySize;
  22. char * readBuffer;
  23. char * restOfFileBuffer;
  24. char * next;
  25.  
  26.  
  27. char infile[30]; // input file
  28. char outfile[30];
  29. char input[1000];
  30. char output[1000];
  31. int model[65536]={0};
  32. int count_array[256][256]={0};
  33. int sort_array[256][256]={0};
  34. int most_frequent[256][4]={0};
  35. //char n[];
  36.  
  37.  
  38. // *****give input file a name ****
  39. cout << "Please enter in the input file name: " << endl;
  40. cin >> infile;
  41. cout << "Please enter in the output file name: " << endl;
  42. cin >> outfile;
  43.  
  44. fp_in.open(infile, ios::in);
  45. fp_out.open(outfile, ios::out); // open the streams
  46.  
  47. if (fp_in.is_open())
  48. {
  49. cout<<"yes";
  50. }
  51. //get the size of the file
  52. fp_in.seekg(0,ifstream::end);
  53. size=fp_in.tellg();
  54. fp_in.seekg(0);
  55. size=1000;
  56. // allocate memory for file content
  57. buffer = new char [size];
  58.  
  59.  
  60. // read content of infile
  61. fp_in.read (buffer,size);
  62. copySize=1000;
  63. // write to outfile
  64. fp_out.write (buffer,copySize);
  65.  
  66. // release dynamically-allocated memory
  67. delete[] buffer;
  68.  
  69. fp_out.close();
  70. fp_in.close();
  71.  
  72.  
  73.  
  74. //keeping track of bytes
  75.  
  76. fp_in1.open(outfile, ios::in); //open output file
  77. //allocate memory for file content
  78. readBuffer = new char [copySize];
  79.  
  80. if(!fp_in1.is_open())
  81. {
  82.  
  83. cout<<"not open";
  84. }
  85.  
  86.  
  87. while(fp_in1>>readBuffer)
  88. /*
  89. ***test*****
  90. int a=readBuffer[0];
  91. int b=readBuffer[3];
  92. cout<<readBuffer[0]<<""<<readBuffer[1];
  93. cout<<static_cast<int>('a') + static_cast<int>('a');
  94. */
  95.  
  96.  
  97. //cout<<readBuffer;
  98.  
  99.  
  100.  
  101. //put frequency of character and next char into 2D array
  102. for(int current=0; current< 1000; current++)
  103. {
  104. int charPosition;
  105. int nextPosition;
  106. int next;
  107. unsigned char a;
  108. unsigned char b;
  109. next=current+1;
  110. a=readBuffer[current];
  111. b=readBuffer[next];
  112. charPosition=static_cast<int>(a);
  113. nextPosition=static_cast<int>(b);
  114. ++count_array[charPosition][nextPosition];
  115. ++sort_array[charPosition][nextPosition];
  116. }
  117.  
  118.  
  119. selectionSort(sort_array, indexSize);
  120. //**TEST
  121.  
  122. for(int x=0; x<indexSize;x++)
  123. {
  124. int i=0;
  125. unsigned char c = readBuffer[0];
  126. int position=static_cast<int>(c);
  127. // cout<<count_array[position][x];
  128. cout<<sort_array[position][x]<<" ";
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135. cout<<"Character "<<"Next Character "<<"Freqency"<<endl;
  136.  
  137. for(int display=0; display < 1000; display++) //traverse readbuffer
  138. {
  139. int displayChar;
  140. int displayNext;
  141. int nextChar;
  142. unsigned char display1;
  143. unsigned char display2;
  144.  
  145.  
  146. nextChar=display+1;
  147. display1=readBuffer[display];
  148. display2=readBuffer[nextChar];
  149. displayChar=static_cast<int>(display1);
  150. displayNext=static_cast<int>(display2);
  151. cout<<display1<<setw(12)<<display2<<setw(18)<<count_array[display1][display2]<<endl;
  152.  
  153. }
  154. //display four most frequent
  155.  
  156. cout<<"Character "<<"Next Character "<<"Most Frequent"<<endl;
  157.  
  158. for(int frequency=0; frequency < 1000; frequency++) //traverse readbuffer
  159. {
  160. int frequencyChar;
  161. int frequencyNext;
  162. int nextChar;
  163. unsigned char frequency1;
  164. unsigned char frequency2;
  165.  
  166. nextChar=frequency+1;
  167. frequency1=readBuffer[frequency];
  168. frequency2=readBuffer[nextChar];
  169. frequencyChar=static_cast<int>(frequency1);
  170. frequencyNext=static_cast<int>(frequency2);
  171. //cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<count_array[frequency1][frequency2]<<endl;
  172. if(count_array[frequencyChar][frequencyNext]==sort_array[frequencyChar][0])
  173. cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<sort_array[frequencyChar][0]<<endl;
  174. if(count_array[frequencyChar][frequencyNext]==sort_array[frequencyChar][1])
  175. cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<sort_array[frequencyChar][1]<<endl;
  176. if(count_array[frequencyChar][frequencyNext]==sort_array[frequencyChar][2])
  177. cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<sort_array[frequencyChar][2]<<endl;
  178. if(count_array[frequencyChar][frequencyNext]==sort_array[frequencyChar][3])
  179. cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<sort_array[frequencyChar][3]<<endl;
  180.  
  181. }
  182.  
  183. cout<<"*********************************************************"<<endl;
  184.  
  185. fp_in1.close();//close read buffer
  186.  
  187.  
  188.  
  189. cout<<"*********************************encoding******************************************";
  190.  
  191.  
  192. fp_in2.open(infile, ios::in);
  193. // open the streams
  194.  
  195. if (fp_in2.is_open())
  196. {
  197. cout<<"fp_in2 open";
  198. }
  199.  
  200.  
  201.  
  202. //get the size of the file
  203. fp_in2.seekg(0,ifstream::end);
  204. size1=fp_in2.tellg();
  205. fp_in2.seekg(0);
  206.  
  207.  
  208. // allocate memory for file content
  209. restOfFileBuffer = new char [size1-1000];
  210.  
  211. fp_in2.seekg(1000);
  212. fp_in2.read(restOfFileBuffer,size1 - 1000);
  213. //fp_in1.close();
  214. //****************************************test restOfFileBuffer****************
  215. cout<<endl<<"restOfFileBuffer"<<endl;
  216.  
  217. for(int f=0; f<size1-1000;f++)
  218. {
  219. cout<<"restOfBuffer"<<restOfFileBuffer[f];
  220. }
  221. //****************************************************************************
  222.  
  223.  
  224. fp_out1.open(outfile, ios::app);
  225.  
  226. unsigned int acc_length=0;
  227. unsigned int accumulator=0;
  228. for(int mostFrequency=0; mostFrequency < size1-1000; mostFrequency++) //traverse readbuffer
  229. {
  230. unsigned int mostFrequencyChar;
  231. unsigned int mostFrequencyNext;
  232. unsigned int nextChar;
  233. unsigned char mostFrequency1;
  234. unsigned char mostFrequency2;
  235. unsigned int codeword;
  236.  
  237.  
  238.  
  239. nextChar=mostFrequency+1;
  240. mostFrequency1=restOfFileBuffer[mostFrequency];
  241. mostFrequency2=restOfFileBuffer[nextChar];
  242. mostFrequencyChar=static_cast<int>(mostFrequency1);
  243. mostFrequencyNext=static_cast<int>(mostFrequency2);
  244.  
  245.  
  246. if((count_array[mostFrequencyChar][mostFrequencyNext]==sort_array[mostFrequencyChar][0]))
  247. {
  248. codeword=0;
  249. accumulator=accumulator|(codeword<<acc_length);
  250. acc_length += 3;
  251.  
  252.  
  253. }
  254. else if((count_array[mostFrequencyChar][mostFrequencyNext]==sort_array[mostFrequencyChar][1]))
  255. {
  256. codeword=2;
  257. accumulator=accumulator|(codeword<<acc_length);
  258. acc_length += 3;
  259.  
  260.  
  261. }
  262. else if((count_array[mostFrequencyChar][mostFrequencyNext]==sort_array[mostFrequencyChar][2]))
  263. {
  264. codeword=4;
  265. accumulator=accumulator|(codeword<<acc_length);
  266. acc_length += 3;
  267.  
  268.  
  269. }
  270. else if((count_array[mostFrequencyChar][mostFrequencyNext]==sort_array[mostFrequencyChar][3]))
  271. {
  272. codeword=6;
  273. accumulator=accumulator|(codeword<<acc_length);
  274. acc_length += 3;
  275.  
  276.  
  277. }
  278.  
  279. else {
  280.  
  281. accumulator=accumulator|((mostFrequencyNext<<1|1)<<acc_length);
  282. acc_length +=9;
  283.  
  284.  
  285. }
  286. if(acc_length >= 8)
  287. {
  288.  
  289. //******write*************************
  290.  
  291.  
  292.  
  293.  
  294.  
  295. fp_out1<<static_cast<char>(accumulator&0xFF); //print out last 8 bits
  296.  
  297.  
  298. accumulator= accumulator>>8;
  299. acc_length-=8;
  300. }
  301. }//end for loop
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309. fp_in2.close();
  310. fp_out1.close();
  311. return 0;
  312.  
  313. }
  314.  
  315. //2D selection sort used to sort each row in 2D array
  316. //values in descending order
  317.  
  318.  
  319. void selectionSort(int arr[][indexSize], int size)
  320. {
  321. int indexOfMax, row,col,j;
  322. for( row=0; row<size;row++)
  323. {
  324. for( col=0; col<size-1;col++)
  325.  
  326. {
  327. indexOfMax = col;
  328. for(j=col+1; j<size; j++)
  329. if(arr[row][j]>arr[row][indexOfMax])
  330. indexOfMax = j;
  331. if(indexOfMax != col)
  332.  
  333. {
  334. int temp;
  335. temp= arr[row][col];
  336. arr[row][col]=arr[row][indexOfMax];
  337. arr[row][indexOfMax] = temp;
  338. }
  339. }
  340.  
  341.  
  342.  
  343. }
  344.  
  345.  
  346.  
  347.  
  348.  
  349. }
  350.  
  351.  
  352. //swap function for int values
  353. void swap(int *x, int *y)
  354. {
  355.  
  356. int temp;
  357. temp = *x;
  358. *x =*y;
  359. *y = temp;
  360.  
  361.  
  362.  
  363. }
  364.  
  365.  
  366. //printing an unsigned integer in bits used to check bits
  367. void displayBits(unsigned value)
  368. {
  369.  
  370. const int SHIFT = 8 *sizeof(unsigned) - 1;
  371. const unsigned MASK = 1 <<SHIFT;
  372.  
  373. cout<<setw(7) << value << " = ";
  374.  
  375. for (unsigned c=1; c<= SHIFT + 1; c++){
  376. cout <<(value & MASK ? '1' : '0');
  377. value <<=1;
  378.  
  379. if(c % 8 ==0)
  380. cout<< ' ';
  381. }
  382.  
  383.  
  384. cout<<endl;
  385. }
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416. //***************************DECODE**********************
  417. #include <iostream.h> // I/O
  418. #include <fstream.h> // file I/O
  419. #include <iomanip.h> // format manipulation
  420. #include <cstring>
  421. #include <cstddef>
  422. #include <stdio.h>
  423.  
  424.  
  425. //using namespace std;
  426. void displayBits(unsigned);
  427. const long indexSize = 256;
  428. void selectionSort(int[][indexSize],int);
  429. void swap(int, int);
  430.  
  431. int main () {
  432. ifstream fp_in,fp_in1,fp_in2; // declarations of streams fp_in and fp_out
  433. ofstream fp_out,fp_out1;
  434.  
  435. char * buffer;
  436. long size, size1;
  437. long copySize;
  438. char * readBuffer;
  439. char * restOfFileBuffer;
  440. char * next;
  441.  
  442.  
  443. char infile[30]; // input file
  444. char outfile[30];
  445. char input[1000];
  446. char output[1000];
  447. int model[65536]={0};
  448. int count_array[256][256]={0};
  449. int sort_array[256][256]={0};
  450. int most_frequent[256][4]={0};
  451. //char n[];
  452.  
  453.  
  454. // *****give input file a name ****
  455. cout << "Please enter in the input file name: " << endl;
  456. cin >> infile;
  457. cout << "Please enter in the output file name: " << endl;
  458. cin >> outfile;
  459.  
  460. fp_in.open(infile, ios::in);
  461. fp_out.open(outfile, ios::out); // open the streams
  462.  
  463. if (fp_in.is_open())
  464. {
  465. cout<<"yes";
  466. }
  467. //get the size of the file
  468. fp_in.seekg(0,ifstream::end);
  469. size=fp_in.tellg();
  470. fp_in.seekg(0);
  471. size=1000;
  472. // allocate memory for file content
  473. buffer = new char [size];
  474.  
  475.  
  476. // read content of infile
  477. fp_in.read (buffer,size);
  478. copySize=1000;
  479. // write to outfile
  480. fp_out.write (buffer,copySize);
  481.  
  482. // release dynamically-allocated memory
  483. delete[] buffer;
  484.  
  485. fp_out.close();
  486. fp_in.close();
  487.  
  488.  
  489.  
  490. //keeping track of bytes
  491.  
  492. fp_in1.open(outfile, ios::in); //open output file
  493. //allocate memory for file content
  494. readBuffer = new char [copySize];
  495.  
  496. if(!fp_in1.is_open())
  497. {
  498.  
  499. cout<<"not open";
  500. }
  501.  
  502.  
  503. while(fp_in1>>readBuffer)
  504.  
  505.  
  506.  
  507. //cout<<readBuffer;
  508.  
  509.  
  510.  
  511. //put frequency of character and next char into 2D array
  512. for(int current=0; current< 1000; current++)
  513. {
  514. int charPosition;
  515. int nextPosition;
  516. int next;
  517. unsigned char a;
  518. unsigned char b;
  519. next=current+1;
  520. a=readBuffer[current];
  521. b=readBuffer[next];
  522. charPosition=static_cast<int>(a);
  523. nextPosition=static_cast<int>(b);
  524. ++count_array[charPosition][nextPosition];
  525. ++sort_array[charPosition][nextPosition];
  526. }
  527.  
  528.  
  529. selectionSort(sort_array, indexSize);
  530.  
  531.  
  532. //Display frequency
  533. //option to display values
  534. /*
  535. char y;
  536.  
  537. char toDisplay[1];
  538. //cout<<"Do you want to display frequency?";
  539. //cin>>toDisplay;
  540. //if (toDisplay == y)
  541.  //{
  542.  
  543. //cout<<"Character "<<"Next Character "<<"Freqency"<<endl;
  544.  
  545.  
  546. for(int display=0; display < 1000; display++) //traverse readbuffer
  547.  {
  548. int displayChar;
  549.   int displayNext;
  550. int nextChar;
  551. unsigned char display1;
  552. unsigned char display2;
  553.  
  554.  
  555. nextChar=display+1;
  556. display1=readBuffer[display];
  557. display2=readBuffer[nextChar];
  558. displayChar=static_cast<int>(display1);
  559.   displayNext=static_cast<int>(display2);
  560. // cout<<display1<<setw(12)<<display2<<setw(18)<<count_array[display1][display2]<<endl;
  561.  
  562.  }*/
  563. //display four most frequent
  564. //}
  565. //selectionSort(sort_array, indexSize);
  566. /* cout<<"Character "<<"Next Character "<<"Most Frequent"<<endl;
  567.  
  568. for(int frequency=0; frequency < 1000; frequency++) //traverse readbuffer
  569.  {
  570. int frequencyChar;
  571.   int frequencyNext;
  572. int nextChar;
  573. unsigned char frequency1;
  574. unsigned char frequency2;
  575.  
  576. nextChar=frequency+1;
  577. frequency1=readBuffer[frequency];
  578. frequency2=readBuffer[nextChar];
  579. frequencyChar=static_cast<int>(frequency1);
  580.   frequencyNext=static_cast<int>(frequency2);
  581. //cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<count_array[frequency1][frequency2]<<endl;
  582. if(count_array[frequencyChar][frequencyNext]==sort_array[frequencyChar][0])
  583. cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<sort_array[frequencyChar][0]<<endl;
  584. if(count_array[frequencyChar][frequencyNext]==sort_array[frequencyChar][1])
  585. cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<sort_array[frequencyChar][1]<<endl;
  586. if(count_array[frequencyChar][frequencyNext]==sort_array[frequencyChar][2])
  587. cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<sort_array[frequencyChar][2]<<endl;
  588. if(count_array[frequencyChar][frequencyNext]==sort_array[frequencyChar][3])
  589. cout<<frequency1<<setw(12)<<frequency2<<setw(18)<<sort_array[frequencyChar][3]<<endl;
  590.  
  591.  }
  592. */
  593. cout<<"*********************************************************"<<endl;
  594.  
  595. fp_in1.close();//close read buffer
  596.  
  597. //delete[] readBuffer;
  598.  
  599. cout<<"*********************************decoding******************************************";
  600.  
  601.  
  602. fp_in2.open(infile, ios::in);
  603. // open the streams
  604.  
  605. if (fp_in2.is_open())
  606. {
  607. cout<<"fp_in2 open";
  608. }
  609.  
  610.  
  611.  
  612. //get the size of the file
  613. fp_in2.seekg(0,ifstream::end);
  614. size1=fp_in2.tellg();
  615. fp_in2.seekg(0);
  616.  
  617.  
  618. // allocate memory for file content
  619. restOfFileBuffer = new char [size1-1000];
  620.  
  621. fp_in2.seekg(1000);
  622. fp_in2.read(restOfFileBuffer,size1 - 1000);
  623. //fp_in1.close();
  624. //****************************************test restOfFileBuffer****************
  625.  
  626. cout<<endl<<"restOfFileBuffer"<<endl;
  627.  
  628. for(int f=0; f<15;f++)
  629. {
  630. cout<<restOfFileBuffer[f];
  631.  
  632. }
  633.  
  634. //****************************************************************************
  635.  
  636.  
  637. fp_out1.open(outfile, ios::app);
  638. cout<<"SIZE:"<<size1-1000<<endl;
  639. unsigned int acc_length=0;
  640. unsigned int accumulator=0;
  641. //for(int mostFrequency=0; mostFrequency < size1-1001; mostFrequency++) //traverse readbuffer
  642. for(int mostFrequency=0; mostFrequency < size1-1000; mostFrequency++)
  643. {
  644. unsigned int mostFrequencyChar;
  645. unsigned int mostFrequencyNext;
  646. unsigned int nextChar;
  647. unsigned char mostFrequency1;
  648. unsigned char mostFrequency2;
  649. unsigned int codeword;
  650. unsigned int mask=1;
  651. unsigned int value;
  652. int nextValue;
  653. nextChar=mostFrequency+1;
  654. mostFrequency1=restOfFileBuffer[mostFrequency];
  655. mostFrequency2=restOfFileBuffer[nextChar];
  656. mostFrequencyChar=static_cast<int>(mostFrequency1);
  657. mostFrequencyNext=static_cast<int>(mostFrequency2);
  658. // cout<<mostFrequency1<<setw(12)<<mostFrequency2<<setw(18)<<count_array[mostFrequency1][mostFrequency2]<<endl;
  659.  
  660. accumulator=accumulator|mostFrequencyNext;
  661.  
  662. //cout<<mostFrequencyChar<<" ";
  663.  
  664.  
  665.  
  666. if(!accumulator&mask)
  667. {
  668. acc_length+=3;
  669. accumulator=accumulator&0x7;
  670. // for(int nextValue=0; nextValue< 256; nextValue++)
  671. //{
  672.  
  673. if(accumulator==0)
  674. {
  675. for(nextValue=0; nextValue< 256; nextValue++)
  676. {
  677. if(count_array[mostFrequencyChar][nextValue]==sort_array[mostFrequencyChar][0] )
  678. {
  679. fp_out1<<static_cast<char>(nextValue);
  680. break;
  681. }
  682. }
  683. }
  684. else if(accumulator==2)
  685. {
  686. for(nextValue=0; nextValue< 256; nextValue++)
  687. {
  688. if(count_array[mostFrequencyChar][nextValue]==sort_array[mostFrequencyChar][1])
  689. {
  690. fp_out1<<static_cast<char>(nextValue);
  691. break;
  692. }
  693. }
  694. }
  695. else if(accumulator==4)
  696. {
  697. for(nextValue=0; nextValue< 256; nextValue++)
  698. {
  699. if(count_array[mostFrequencyChar][nextValue]==sort_array[mostFrequencyChar][2])
  700. {
  701. fp_out1<<static_cast<char>(nextValue);
  702. break;
  703. }
  704. }
  705. }
  706. else if(accumulator==6)
  707. {
  708. //
  709. for(nextValue=0; nextValue< 256; nextValue++)
  710. {
  711. if(count_array[mostFrequencyChar][nextValue]==sort_array[mostFrequencyChar][3])
  712. {
  713. fp_out1<<static_cast<char>(nextValue);
  714. break;
  715. }
  716. }
  717. }
  718. else
  719. {
  720. acc_length+=9;
  721. accumulator>>=acc_length-8;
  722. // cout<<"9 bit:"<<static_cast<char>(mostFrequencyNext)<<" ";
  723. fp_out1<<accumulator;
  724. acc_length-=9;
  725. accumulator=accumulator>>8;
  726.  
  727. }
  728.  
  729.  
  730. acc_length-=3;
  731. accumulator=accumulator>>8;
  732.  
  733. }//end if
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742. }//end for loop
  743.  
  744.  
  745.  
  746.  
  747.  
  748. fp_in2.close();
  749. fp_out1.close();
  750. return 0;
  751.  
  752. }
  753.  
  754. //2D selection sort used to sort each row in 2D array
  755. //values in descending order
  756.  
  757.  
  758. void selectionSort(int arr[][indexSize], int size)
  759. {
  760. int indexOfMax, row,col,j;
  761. for( row=0; row<size;row++)
  762. {
  763. for( col=0; col<size-1;col++)
  764.  
  765. {
  766. indexOfMax = col;
  767. for(j=col+1; j<size; j++)
  768. if(arr[row][j]>arr[row][indexOfMax])
  769. indexOfMax = j;
  770. if(indexOfMax != col)
  771. // swap(&arr[row][col], &arr[indexOfMax]);
  772. {
  773. int temp;
  774. temp= arr[row][col];
  775. arr[row][col]=arr[row][indexOfMax];
  776. arr[row][indexOfMax] = temp;
  777. }
  778. }
  779.  
  780.  
  781.  
  782. }
  783.  
  784.  
  785.  
  786.  
  787.  
  788. }
  789.  
  790.  
  791. //swap function for int values
  792. void swap(int *x, int *y)
  793. {
  794.  
  795. int temp;
  796. temp = *x;
  797. *x =*y;
  798. *y = temp;
  799.  
  800.  
  801.  
  802. }
  803.  
  804.  
  805. //printing an unsigned integer in bits used to check bits
  806. void displayBits(unsigned value)
  807. {
  808.  
  809. const int SHIFT = 8 *sizeof(unsigned) - 1;
  810. const unsigned MASK = 1 <<SHIFT;
  811.  
  812. cout<<setw(7) << value << " = ";
  813.  
  814. for (unsigned c=1; c<= SHIFT + 1; c++){
  815. cout <<(value & MASK ? '1' : '0');
  816. value <<=1;
  817.  
  818. if(c % 8 ==0)
  819. cout<< ' ';
  820. }
  821.  
  822.  
  823. cout<<endl;
  824. }