Jazzua Posted May 14, 2008 Share Posted May 14, 2008 Hi there php freaks, I'm new to php having been to this point predominately a java, c++ coder. So far I'm loving my php experience, although i'm a bit confused about some things still. At the moment I'm trying to get a chunck of code working that takes an array populated with random numbers between 1 and 5, counts duplicates and how many occurences there are of each duplicate number. The program then prints this info in a basic html table. It actually works, except theres a whole bunch of errors that look something like this: Notice: Undefined offset: 11 in C:\Inetpub\wwwroot\ex4-11.php on line 73 here is the code in question: <html> <head> <title>Duplicates in Array</title> </head> <?php for($x = 0; $x < 15; $x++) { $num = rand(0,5); $myArray[] = $num; } print "Numbers in array in ascending order are: "."<br>"; sort($myArray); foreach($myArray as $num) //Print numbers in ascending order { print $num." "; } print "<br>"; $end = count($myArray); for($x = 0; $x < $end; ++ $x) //Iterates through each subscript in array, { //shifting index[0] and comaring it to the remaining array $num = array_shift($myArray); $rep = 0; $size = count($myArray); $y = 0; while($y < $size) { $temp = $myArray[$y]; if($temp == $num ) //if a duplicate is found... { ++$rep; //...increase the repitition counter for this index... if($y == 0) //...if the duplicate is at the start of the array... { array_shift($myArray); //...cut off the first index... } else //...otherwise... { $tmp = $myArray[0]; //...swap the duplicate with the first index of the array... $myArray[$y] = $tmp; array_shift($myArray); //...and then cut off the duplicate (which has been swapped to myArray[0]... } } else { $y++; //If no duplicate at this index in myArray is found increase $y counter } } if($rep != 0) { $dupNum[] = $num; $dupNumRep[] = $rep; } } $end = count($dupNum); if($end != 0) { echo '<table border="1">'; echo "<tr> <td><b>Number<b></td> <td><b>Duplicates<b></td> </tr>"; for($x = 0; $x < $end; $x++) { echo "<tr> <td>$dupNum[$x]</td> <td>$dupNumRep[$x]</td> </tr>"; } echo "</table>"; } ?> <body> </body> </html> output is: Numbers in array in ascending order are: <br>1 1 1 1 1 2 2 3 3 3 3 4 4 4 5 <br><br /> [whole bunch of errors] Number Duplicates 1 4 2 1 3 3 4 2 [another whole bunch of errors] If anyone could hlp me with this it would be greatly appreciated. Cheers ~ Jazz Quote Link to comment https://forums.phpfreaks.com/topic/105673-solved-my-algorythm-works-but-still-throw-undefined-offset-notice/ Share on other sites More sharing options...
benphp Posted May 14, 2008 Share Posted May 14, 2008 You have an infinite loop in your while loop - $y never increments, because $temp always = $num Quote Link to comment https://forums.phpfreaks.com/topic/105673-solved-my-algorythm-works-but-still-throw-undefined-offset-notice/#findComment-541410 Share on other sites More sharing options...
Jazzua Posted May 15, 2008 Author Share Posted May 15, 2008 nope... the while loop checks against $y not $temp and the loops and if statements actually resolve correctly because the program actually does work. It calculates the duplicates and repetitions and after the whole big loop section it prints them in a table. It's just that along the way it throws a bunch of error notices (which don't actually effect the program). Quote Link to comment https://forums.phpfreaks.com/topic/105673-solved-my-algorythm-works-but-still-throw-undefined-offset-notice/#findComment-541417 Share on other sites More sharing options...
MadTechie Posted May 15, 2008 Share Posted May 15, 2008 See the 2 //MADTECHIE: comments <html> <head> <title>Duplicates in Array</title> </head> <?php for($x = 0; $x < 15; $x++) { $num = rand(0,5); $myArray[] = $num; } print "Numbers in array in ascending order are: "."<br>"; sort($myArray); foreach($myArray as $num) //Print numbers in ascending order { print $num." "; } print "<br>"; $end = count($myArray); for($x = 0; $x < $end; ++ $x) //Iterates through each subscript in array, { //shifting index[0] and comaring it to the remaining array $num = array_shift($myArray); $rep = 0; $size = count($myArray); $y = 0; while($y < $size) { if(isset($myArray[$y])) //MADTECHIE: Checks the array element exists { $temp = $myArray[$y]; if($temp == $num ) //if a duplicate is found... { ++$rep; //...increase the repitition counter for this index... if($y == 0) //...if the duplicate is at the start of the array... { array_shift($myArray); //...cut off the first index... } else //...otherwise... { $tmp = $myArray[0]; //...swap the duplicate with the first index of the array... $myArray[$y] = $tmp; array_shift($myArray); //...and then cut off the duplicate (which has been swapped to myArray[0]... } } else { $y++; //If no duplicate at this index in myArray is found increase $y counter } }else{ $y++;//MADTECHIE: If doesn't exist then move to next } } if($rep != 0) { $dupNum[] = $num; $dupNumRep[] = $rep; } } $end = count($dupNum); if($end != 0) { echo '<table border="1">'; echo "<tr> <td><b>Number<b></td> <td><b>Duplicates<b></td> </tr>"; for($x = 0; $x < $end; $x++) { echo "<tr> <td>$dupNum[$x]</td> <td>$dupNumRep[$x]</td> </tr>"; } echo "</table>"; } ?> <body> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/105673-solved-my-algorythm-works-but-still-throw-undefined-offset-notice/#findComment-541427 Share on other sites More sharing options...
Jazzua Posted May 15, 2008 Author Share Posted May 15, 2008 Thanks heaps mate... you're a legend. works beautifully Quote Link to comment https://forums.phpfreaks.com/topic/105673-solved-my-algorythm-works-but-still-throw-undefined-offset-notice/#findComment-541434 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.