Jump to content


Photo

array


  • Please log in to reply
7 replies to this topic

#1 bagnallc

bagnallc
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 06 October 2006 - 04:04 PM

Hi there,

There are so many array functions and im gradually trying to learn them.

For my current purpose i am trying to identify the longest number of occurences when something did not happen. The best way i can explain this is with an example.

Say i have the following - 1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 1, 3

and i want to return the fact that the longest sequence of numbers without 1 appearing was seven (when the chain was 5, 6, 7, 8, 8, 8, 9)

is there an easy way of doing this that you can think of?

many thanks

chris




#2 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 06 October 2006 - 06:10 PM

use a for loop over every character in the array, set the number/character you want to find the largest run without, i.e. in your example you would supply "1"

your for loop iterates over the array looking for "1", all the while adding up a counter, when the next time the "1" appears it stores the counter vairiable, it then resets the counter and starts counting again, when it finds another "1" it checks the counter value against the stored count value and saves whichever is the largest, so on so on untillt he array is finished then prinbtes out the stored counter value.

make sense??

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#3 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 06 October 2006 - 06:23 PM

I used this
<?PHP
$array = array(1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 1, 3);
$value = 1;                                               //the value you want to find out between i.e largest count between "1"'s
$answer = 0;                                             //initialise the answer
$count = 0;                                               //initialise the counter
for ($i = 0; $i < count($array); $i++) {           //set up the for loop based on the array elements count
$char = $array[$i];                                      //assign each value to the $char variable
 if ($char == $value) {                                 //check to see if it is the same as the supplied data i.e  1
     if ($count > $answer) {                       //if it is, check to see if the new value of count is larger than the stored value of answer
       $answer = $count;                                //if yes make the asnwer the same as count
       }
     $count = 0; //then reset the counter
}
else {
$count++;                                              //if it is not the same then increase the value of the counter
}
}

echo $answer;  //write out the answer

?>

and got the following solution

7

which is correct....

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 06 October 2006 - 06:36 PM

or make it a function
function countArray($array,$value){
$answer = 0;
$count = 0;
for ($i = 0; $i < count($array); $i++) {
$char = $array[$i];
 if ($char == $value) {
     if ($count > $answer) {
       $answer = $count;
       }
     $count = 0;
}
else {
$count++;
}
}
return $answer;
}

you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 bagnallc

bagnallc
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 06 October 2006 - 06:40 PM

many thanks for your kind help. just what i was looking for.

cheers

chris

#6 bagnallc

bagnallc
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 06 October 2006 - 07:13 PM

Actually guys ive struck a bit of a problem with this!

If the array was changed to

$array = array(1, 2, 5, 9, 8, 7, 1, 4, 1, 5, 6, 7, 8, 8, 8, 9, 3);

(ive taken the last one off)

then the answer returned is 5, when it should be 8. This seems to be as it is the last sequence of numbers in the array.

im playing around with it but havent had any joy as of yet??

#7 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 06 October 2006 - 07:22 PM

this should do it
function codeMode($arr, $value) {
        $count = 0;
		foreach($arr as $val) {
		     if($val == $value)
			      $count = 0;
		     else
			      $count++;
		}
		return $count;
}

EDIT: wait..no it won't gimme a sec

here
function codeMode($arr, $value) {
        $count = array();
		$i = 0;
		foreach($arr as $val) {
		     if($val == $value)
			      $i++;
		     else {
			      $count[$i]++;
			}
		}
		rsort($count);
		return $count[0];
}

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#8 bagnallc

bagnallc
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 06 October 2006 - 07:30 PM

cheers

i actually just got the other one working there by using

array_push($array, 1); which adds a one on the end so it does return the correct value

all a learning process for me.

thanks again




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users