PHP didn't pass the arithmetic class

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

I'm having problems getting my PHP to do simple math... such as add and subtract.

I'm pretty sure it has to do with the logic of my code, but for the life of me, I can't seem to figure it out!! Any help?

PHP Code: [ Select ]
<?php
function generate_math()
{
    // Initiating some variables used in the
    $i = 0;
    $int = random_num(-9, 9);
    $nums = null;
    $opr = $int;
   
    // Getting two numbers
    while($i < 2)
    {
        // Checking if the number is a negative (to put parenthesis around negative numbers)
        if($int < 0)
        {
            // Putting parenthesis around the negative number
            $nums .= "({$int})";
        }
        else
        {
            // It's a positive number, leaving it as is
            $nums .= $int;
        }
       
        // Generating another number, and based on it, determining if we are adding or subtracting
        // If the number generated is even, we are adding; if it's odd, we are subtracting
        if(((mt_rand(1, mt_getrandmax()) % 2) == 0))
        {
            $sign = '+';
           
            // Keeping a running total (not on the first loop so we don't end up doubling the number)
            if($i > 0)
            {
                $opr += $int;
            }
        }
        else
        {
            $sign = '-';
           
            // Keeping a running total (not on the first loop so we don't subtract the first number)
            if($i > 0)
            {
                $opr -= $int;
            }
        }
       
        // Putting the signs (+/-) between numbers... making sure we don't have an extra one at the very end
        if($i != 1)
        {
            $nums .= $sign;
        }
       
        // Generating the next random number
        $int = random_num(-9, 9);
       
        // incrementing the loop counter
        $i++;
    }
   
    echo $nums . '=' . $opr;
    //return $nums;
}
 
function random_num($min, $max)
{
    // Getting a random number
    $int = mt_rand($min, $max);
   
    // Making sure that a 0 is not one of them
    if($int == 0)
    {
        // Generating a new random number since this one is a 0
        $int = random_num($min, $max);
    }
   
    // Returning a non-0 random number
    return $int;
}
 
generate_math();
?>
  1. <?php
  2. function generate_math()
  3. {
  4.     // Initiating some variables used in the
  5.     $i = 0;
  6.     $int = random_num(-9, 9);
  7.     $nums = null;
  8.     $opr = $int;
  9.    
  10.     // Getting two numbers
  11.     while($i < 2)
  12.     {
  13.         // Checking if the number is a negative (to put parenthesis around negative numbers)
  14.         if($int < 0)
  15.         {
  16.             // Putting parenthesis around the negative number
  17.             $nums .= "({$int})";
  18.         }
  19.         else
  20.         {
  21.             // It's a positive number, leaving it as is
  22.             $nums .= $int;
  23.         }
  24.        
  25.         // Generating another number, and based on it, determining if we are adding or subtracting
  26.         // If the number generated is even, we are adding; if it's odd, we are subtracting
  27.         if(((mt_rand(1, mt_getrandmax()) % 2) == 0))
  28.         {
  29.             $sign = '+';
  30.            
  31.             // Keeping a running total (not on the first loop so we don't end up doubling the number)
  32.             if($i > 0)
  33.             {
  34.                 $opr += $int;
  35.             }
  36.         }
  37.         else
  38.         {
  39.             $sign = '-';
  40.            
  41.             // Keeping a running total (not on the first loop so we don't subtract the first number)
  42.             if($i > 0)
  43.             {
  44.                 $opr -= $int;
  45.             }
  46.         }
  47.        
  48.         // Putting the signs (+/-) between numbers... making sure we don't have an extra one at the very end
  49.         if($i != 1)
  50.         {
  51.             $nums .= $sign;
  52.         }
  53.        
  54.         // Generating the next random number
  55.         $int = random_num(-9, 9);
  56.        
  57.         // incrementing the loop counter
  58.         $i++;
  59.     }
  60.    
  61.     echo $nums . '=' . $opr;
  62.     //return $nums;
  63. }
  64.  
  65. function random_num($min, $max)
  66. {
  67.     // Getting a random number
  68.     $int = mt_rand($min, $max);
  69.    
  70.     // Making sure that a 0 is not one of them
  71.     if($int == 0)
  72.     {
  73.         // Generating a new random number since this one is a 0
  74.         $int = random_num($min, $max);
  75.     }
  76.    
  77.     // Returning a non-0 random number
  78.     return $int;
  79. }
  80.  
  81. generate_math();
  82. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

It seems its your logic.

change
PHP Code: [ Select ]
// Keeping a running total (not on the first loop so we don't end up doubling the number)
            if($i > 0)
            {
                $opr += $int;
            }
 
 
  1. // Keeping a running total (not on the first loop so we don't end up doubling the number)
  2.             if($i > 0)
  3.             {
  4.                 $opr += $int;
  5.             }
  6.  
  7.  

AND This
PHP Code: [ Select ]
 
// Keeping a running total (not on the first loop so we don't subtract the first number)
            if($i > 0)
            {
                $opr -= $int;
            }
 
  1.  
  2. // Keeping a running total (not on the first loop so we don't subtract the first number)
  3.             if($i > 0)
  4.             {
  5.                 $opr -= $int;
  6.             }
  7.  

To
PHP Code: [ Select ]
// Keeping a running total (not on the first loop so we don't end up doubling the number)
            if($i > 0)
            {
                $opr = $opr + $int;
            }
 
 
  1. // Keeping a running total (not on the first loop so we don't end up doubling the number)
  2.             if($i > 0)
  3.             {
  4.                 $opr = $opr + $int;
  5.             }
  6.  
  7.  

AND This
PHP Code: [ Select ]
 
// Keeping a running total (not on the first loop so we don't subtract the first number)
            if($i > 0)
            {
                $opr = $opr - $int;
            }
 
  1.  
  2. // Keeping a running total (not on the first loop so we don't subtract the first number)
  3.             if($i > 0)
  4.             {
  5.                 $opr = $opr - $int;
  6.             }
  7.  


EDIT:: there is something else going on
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

There was an underlining problem based on the logic in adding the sign and displaying it. the math was correct but the way you displayed is made it incorrect.

// When you added the sign here you would be this but once on $i=0; then the return to the beginning of your loop
PHP Code: [ Select ]
// Putting the signs (+/-) between numbers... making sure we don't have an extra one at the very end
        if($i != 1)
        {
            $nums .= $sign;
        }
 
  1. // Putting the signs (+/-) between numbers... making sure we don't have an extra one at the very end
  2.         if($i != 1)
  3.         {
  4.             $nums .= $sign;
  5.         }
  6.  


This is where things got weird.
PHP Code: [ Select ]
// Generating another number, and based on it, determining if we are adding or subtracting
        // If the number generated is even, we are adding; if it's odd, we are subtracting
        if(((mt_rand(1, mt_getrandmax()) % 2) == 0))
        {
 
  1. // Generating another number, and based on it, determining if we are adding or subtracting
  2.         // If the number generated is even, we are adding; if it's odd, we are subtracting
  3.         if(((mt_rand(1, mt_getrandmax()) % 2) == 0))
  4.         {
  5.  


At this point you would regenerate an odd or even and add the possibility to change the sign so sometimes it would change other times it wouldn't.

Now in the code below i made it build the output code as we did the calculations giving you the correct sign every time.

PHP Code: [ Select ]
    function generate_math()
    {
        // Initiating some variables used in the
        $i = 0;
        $int = random_num(-9, 9);
        $nums = NULL;
        $opr = $int;
      $tot = NULL;
       
        // Getting two numbers
        while($i < 2)
        {
         
            // Generating another number, and based on it, determining if we are adding or subtracting
            // If the number generated is even, we are adding; if it's odd, we are subtracting
            if(((mt_rand(1, mt_getrandmax()) % 2) == 0))
            {
 
                // Keeping a running total (not on the first loop so we don't end up doubling the number)
                if($i > 0)
                {
                    $tot = $opr + $int;
               $nums = (($opr < 0) ? "({$opr})" : $opr) . '+' . (($int < 0) ? "({$int})" : $int);
                }
           
            }
            else
            {
                // Keeping a running total (not on the first loop so we don't subtract the first number)
                if($i > 0)
                {
                    $tot = $opr - $int;
               $nums = (($opr < 0) ? "({$opr})" : $opr) . '-' . (($int < 0) ? "({$int})" : $int);
                }
 
            }
           
            // Generating the next random number
            $int = random_num(-9, 9);
 
            // incrementing the loop counter
            $i++;
        }
       
        echo $nums . '  =  ' . $tot;
        //return $nums;
    }
     
    function random_num($min, $max)
    {
        // Getting a random number
        $int = mt_rand($min, $max);
       
        // Making sure that a 0 is not one of them
        if($int == 0)
        {
            // Generating a new random number since this one is a 0
            $int = random_num($min, $max);
        }
       
        // Returning a non-0 random number
        return $int;
    }
     
    generate_math();
   ?>
 
  1.     function generate_math()
  2.     {
  3.         // Initiating some variables used in the
  4.         $i = 0;
  5.         $int = random_num(-9, 9);
  6.         $nums = NULL;
  7.         $opr = $int;
  8.       $tot = NULL;
  9.        
  10.         // Getting two numbers
  11.         while($i < 2)
  12.         {
  13.          
  14.             // Generating another number, and based on it, determining if we are adding or subtracting
  15.             // If the number generated is even, we are adding; if it's odd, we are subtracting
  16.             if(((mt_rand(1, mt_getrandmax()) % 2) == 0))
  17.             {
  18.  
  19.                 // Keeping a running total (not on the first loop so we don't end up doubling the number)
  20.                 if($i > 0)
  21.                 {
  22.                     $tot = $opr + $int;
  23.                $nums = (($opr < 0) ? "({$opr})" : $opr) . '+' . (($int < 0) ? "({$int})" : $int);
  24.                 }
  25.            
  26.             }
  27.             else
  28.             {
  29.                 // Keeping a running total (not on the first loop so we don't subtract the first number)
  30.                 if($i > 0)
  31.                 {
  32.                     $tot = $opr - $int;
  33.                $nums = (($opr < 0) ? "({$opr})" : $opr) . '-' . (($int < 0) ? "({$int})" : $int);
  34.                 }
  35.  
  36.             }
  37.            
  38.             // Generating the next random number
  39.             $int = random_num(-9, 9);
  40.  
  41.             // incrementing the loop counter
  42.             $i++;
  43.         }
  44.        
  45.         echo $nums . '  =  ' . $tot;
  46.         //return $nums;
  47.     }
  48.      
  49.     function random_num($min, $max)
  50.     {
  51.         // Getting a random number
  52.         $int = mt_rand($min, $max);
  53.        
  54.         // Making sure that a 0 is not one of them
  55.         if($int == 0)
  56.         {
  57.             // Generating a new random number since this one is a 0
  58.             $int = random_num($min, $max);
  59.         }
  60.        
  61.         // Returning a non-0 random number
  62.         return $int;
  63.     }
  64.      
  65.     generate_math();
  66.    ?>
  67.  
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Thanks, that seems to work... the thing though, I still don't understand why my way didn't work originally.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

you were using a loop while($i < 2) so you were looping twice. since you were looping twice your calling the if statement if(((mt_rand(1, mt_getrandmax()) % 2) == 0)) twice as well. With each call it could bee odd or even.

so after your loop has completed you have the possibility of having odd, odd OR odd, even OR even, even OR even, odd. this is important to know because later on within the loop you check to see if your on the first time around on your loop (if($i != 1)) to set the sign for your equation.

Given the odd even possibility you have a 50% chance of getting the correct sign. So if of the first time around you got a even you would add a plus sign to the output $nums and got an odd on the second time around your math would be subtracting and not adding.

You could have done this with out a loop as well
PHP Code: [ Select ]
function generate_math() {
     
        // Initiating some variables used in the
        $int1  = random_num(-9, 9);
        $int2  = random_num(-9, 9);
        $nums  = NULL;
      $total   = NULL;
       
      // Generating another number, and based on it, determining if we are adding or subtracting
      // If the number generated is even, we are adding; if it's odd, we are subtracting
      if(((mt_rand(1, mt_getrandmax()) % 2) == 0)) {
            $total = $int1 + $int2;
            $nums = (($int1 < 0) ? "({$int1})" : $int1) . '+' . (($int2 < 0) ? "({$int2})" : $int2);
         
      } else {
            $total = $int1 - $int2;
            $nums = (($int1 < 0) ? "({$int1})" : $int1) . '-' . (($int2 < 0) ? "({$int2})" : $int2);
 
      }
       
        echo $nums . '  =  ' . $total;
        //return $nums;
     
    }
 
  1. function generate_math() {
  2.      
  3.         // Initiating some variables used in the
  4.         $int1  = random_num(-9, 9);
  5.         $int2  = random_num(-9, 9);
  6.         $nums  = NULL;
  7.       $total   = NULL;
  8.        
  9.       // Generating another number, and based on it, determining if we are adding or subtracting
  10.       // If the number generated is even, we are adding; if it's odd, we are subtracting
  11.       if(((mt_rand(1, mt_getrandmax()) % 2) == 0)) {
  12.             $total = $int1 + $int2;
  13.             $nums = (($int1 < 0) ? "({$int1})" : $int1) . '+' . (($int2 < 0) ? "({$int2})" : $int2);
  14.          
  15.       } else {
  16.             $total = $int1 - $int2;
  17.             $nums = (($int1 < 0) ? "({$int1})" : $int1) . '-' . (($int2 < 0) ? "({$int2})" : $int2);
  18.  
  19.       }
  20.        
  21.         echo $nums . '  =  ' . $total;
  22.         //return $nums;
  23.      
  24.     }
  25.  
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Oh, that makes sense now... thanks. I did think about doing it without a loop, but at first I was doing with three numbers instead of two (2+3-4 instead of 2+3), so that is why I started off with a loop, but I think it would be faster without the loop (and better on bandwidth).
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

Image

Nice! Lol

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.