Jump to content

Problem with encryption function ?


ivelin1012

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.