ivelin1012 Posted January 5, 2011 Share Posted January 5, 2011 hello everyone i'm new to PHP and i need your help. I'm developing a code which implements shannon-fano encryption algorithm with php. But i have problems with my function. Basicly i try to do this: 1.Specofy a string to be coded. 2.Count the number of occurences of a character and write it in assoc array like this : "A"=>4,"B"=>2 etc. then i copy this array 3.After i have the array i sort it descending. 4.Divide the given array into two arrays where the sum of the values is almost equal.In the Copied array i set the value of the elements which fit into the fisrt divided array with 0 the rest with 1. 5.Each of the divided arrays i divide with recursion again until every symbol is into different array.and add to the value in the copied array 0 or 1; 6.I try to print the copied array. here is my function which doesnt work and gives a lot of errors function divide_array($array) { $sum=0; $mid=array_sum($array)/2; foreach($array as $k=>$v) { if($sum<$mid){ $sum=$sum+$array[$k]; $up[$k]=$array[$k]; $codeArr[$k]=0; } else { $down=array_slice($array,$k+1); $codeArr[$k]=1; } } divide_array($up); divide_array($down); echo "<pre>"; print_r($codeArr); echo "</pre>"; } i appreciate any help PS:I know this can be done easier with trees but i don't understand them. Link to comment https://forums.phpfreaks.com/topic/223529-problem-with-encryption-function/ Share on other sites More sharing options...
BlueSkyIS Posted January 5, 2011 Share Posted January 5, 2011 gives a lot of errors. like what? Link to comment https://forums.phpfreaks.com/topic/223529-problem-with-encryption-function/#findComment-1155432 Share on other sites More sharing options...
ivelin1012 Posted January 5, 2011 Author Share Posted January 5, 2011 Allowed memory size of 134217728 bytes exhausted (tried to allocate 261904 bytes) in from this sort Link to comment https://forums.phpfreaks.com/topic/223529-problem-with-encryption-function/#findComment-1155440 Share on other sites More sharing options...
btherl Posted January 5, 2011 Share Posted January 5, 2011 Try calling divide_array(array()) and see what happens. And try divide_array(array(1)). In both cases it loops forever until it crashes. One of the most important things with recursion is how to handle the base case, which in your situation would be an array of length 1 or length 0. You should probably have a special case for each of these. The other important thing is to ensure that each recursive call ALWAYS has a shorter array than what you were originally called with. Link to comment https://forums.phpfreaks.com/topic/223529-problem-with-encryption-function/#findComment-1155445 Share on other sites More sharing options...
ivelin1012 Posted January 5, 2011 Author Share Posted January 5, 2011 i'm very bad with recursion,with this function i tried to do this if we have array ("B"=>5,"A"=>2,"C"=>1,"D"=>1) B=>5 A=>2 C=>1 D=>1 on the first divide has to become : up-array :B=>5; down-array: A=>2 C=>1 D=>1 and in the other array to write : codeArray B=>0 A=>1 C=>1 D=>1 down array divides again to : up1: A=>2; down1: C=>1 D=>1 and codeArray Becomes: B=>0 A=>10 C=>11 D=>11 then down1 array divides into: up2: C=>1 down2: D=>1 and codeArray Becomes: B=>0 A=>10 C=>110 D=>111 recursion stops and prints codeArray Link to comment https://forums.phpfreaks.com/topic/223529-problem-with-encryption-function/#findComment-1155456 Share on other sites More sharing options...
btherl Posted January 5, 2011 Share Posted January 5, 2011 Have you tried simpler recursive functions? I think you need more practice with recursion before tackling this, because the basics are not there. You can try factorial, reverse (make an array with the elements in the opposite order), "sum of array values" and "count of array values". Link to comment https://forums.phpfreaks.com/topic/223529-problem-with-encryption-function/#findComment-1155461 Share on other sites More sharing options...
ivelin1012 Posted January 6, 2011 Author Share Posted January 6, 2011 Have you tried simpler recursive functions? I think you need more practice with recursion before tackling this, because the basics are not there. You can try factorial, reverse (make an array with the elements in the opposite order), "sum of array values" and "count of array values". i have to present it till monday.I am trying to develop myself in the web design & programing . Never had any interest in encryption and software development. But i have to present this implementation till monday. It can be done with trees but those are dark waters for me Link to comment https://forums.phpfreaks.com/topic/223529-problem-with-encryption-function/#findComment-1155462 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.