Jump to content

[SOLVED] my algorythm works but still throw undefined offset notice


Recommended Posts

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

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).

 

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>

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.