How to do 3*3 matrix multiplication in php? - php

How can i do 3*3 or 3*1 matrix multiplication in php with following code. where should i have to change the logic ?????
this code completely work for only 2*2 matrix multiplication
Here is my code:
$a = Array( Array(1,2),Array(4,5));
$b = Array( Array(7,5), Array(3,2));
$sumArray = array();
$c = array();
for($i=0;$i<2;$i++)
{
for($j=0;$j<2;$j++)
{
$c[$i][$j]=0;
for($k=0;$k<2;$k++)
{
$c[$i][$j]=$c[$i][$j]+($a[$i][$k]*$b[$k][$j]);
}
}
}
echo "<pre/>";
print_r($c);
?>

Matrix Multiplication
Rules :
First matrix's column and Second matrix's row must be same
Result matrix's size will be First matrix's Row and Second matrix's Column
$a = Array( Array(1,2),Array(4,5));
$b = Array( Array(7,5), Array(3,2));
$r=count($a);
$c=count($b[0]);
$p=count($b);
if(count($a[0]) != $p){
echo "Incompatible matrices";
exit(0);
}
$result=array();
for ($i=0; $i < $r; $i++){
for($j=0; $j < $c; $j++){
$result[$i][$j] = 0;
for($k=0; $k < $p; $k++){
$result[$i][$j] += $a[$i][$k] * $b[$k][$j];
}
}
}
print_r($result);

Matrix computation is achieved by multiplying any column or row elements by their own cofactors.
Codes according to your request;
<?php
if ($_POST['hsp']) {
$de3t11=$_POST['m11']; $de3t12=$_POST['m12']; $de3t13=$_POST['m13'];
$de3t21=$_POST['m21']; $de3t22=$_POST['m22']; $de3t23=$_POST['m14'];
$de3t31=$_POST['m31']; $de3t32=$_POST['m23']; $de3t33=$_POST['m15'];
//show on screen
echo $de3t11 ." ". $de3t12. " ". $de3t13;
echo "<br>";
echo $de3t21 ." ". $de3t22. " ". $de3t23;
echo "<br>";
echo $de3t31 ." ". $de3t32. " ". $de3t33;
echo "<br>";
//calculate matrix
$m3m11=(($de3t22*$de3t33)-($de3t23*$de3t32))*(1);
$m3m12=(($de3t21*$de3t33)-($de3t23*$de3t31))*(-1);
$m3m13=(($de3t21*$de3t32)-($de3t22*$de3t31))*(1);
//Determinant
$det3t3=(($de3t11*$m3m11)+($de3t12*$m3m12)+($de3t13*$m3m13));
echo $det3t3;
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="POST">
<input type="number" name="m11">
<input type="number" name="m12">
<input type="number" name="m13">
<br><br>
<input type="number" name="m21">
<input type="number" name="m22">
<input type="number" name="m23">
<br><br>
<input type="number" name="m31">
<input type="number" name="m32">
<input type="number" name="m33">
<input type="submit" name="hsp">
</form>
</body>
</html>

Related

php code for find largest and second largest number without using array

<form id="form1" name="form1" method="post" action="">
<input name="txt1" type="text" /><br />
<input name="txt2" type="text" /><br />
<input name="txt3" type="text" /><br />
<input name="s" type="submit" value="Find No"/>
</form>
Here is my html code.
<?php
$a = 10;
$b = 25;
$c = 20;
if($a > $b)
{
if($a > $c)
{
echo "a is biggest number";
}
else
{
echo "c is biggest number";
}
}
if($b > $c)
{
echo "b is biggest number";
}
?>
` Here is the code for finding
largest among three no.s . I wish to get largest and second largest no
Given an array of numerical strings obtained from the input fields, for example one that mirrors your original values:
$a = array('1', '5', '19', '200', '999');
you can convert it into a true numerical array using the following approach:
$a = array_map('intval', $a);
and then proceed exactly as you did before:
$last = max($a);
$second = max(array_diff($a,[$last]));
Fiddle here.
you have to use input box for this and a submit button for very simple basic working model.
HTML:
<form action="/path to your php file or leave blank if both html and php are in same file">
<input type="text" name="arr" value=''>
<input type="submit" name="submit" value="submit">
</form>
// now user can provide input with space in input box such as 9 11 13 15 16 29 etc.
PHP
<?php
if(isset($_REQUEST['arr']) && !empty($_REQUEST['arr'])){
$a = explode(' ', $_REQUEST['arr']);
$last = max($a);
$second = max(array_diff($a,[$last]));
echo $second;
echo $last;
?>
I think this will resolve your query.
Here we use reverse array sort with largest and second largest number
$result = array(1,5,19,200,999);
$count = count($result);
for($i=0;$i<$count;$i++){
for($j=$i+1;$j<$count;$j++){
if($result[$i]<$result[$j]){
$m = $result[$i];
$result[$i] = $result[$j];
$result[$j] = $m;
}
}
}
echo "Largest Number : ".(!empty($result[0])?$result[0]:FALSE)." Largest Second Number : ".(!empty($result[1])?$result[1]:FALSE);

PHP get first four item in array on first loop then get next four item on second loop?

//First file
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<div>
<form action="questions.php" method="post">
Quiz Name: <input type="text" name="quizname" /><br/><br/>
Number of M.C.Q questions: <input type="number" name="mcq" min="0" /><br/><br/>
Number of True/False questions: <input type="number" name="truefalse" min="0" /><br/><br/>
<input type="submit" name="submit" value="Start Generating Questions" />
</form>
</div>
</body>
</html>
//Second file
<?php
if(isset($_POST['submit'])){
$quizName = $_POST['quizname'];
$mcq = $_POST['mcq'];
$truefalse = $_POST['truefalse'];
echo '<form action="finalquestions.php" method="post">';
for($i=1;$i<=$mcq;$i++){
echo 'Question:'.$i.' <input type="text" name="question1[]" /> </br></br>';
echo '<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>
<input type="text" name="radiooptions[]" /> </br>';
echo '</br></br>';
}
for($i=1;$i<=$truefalse;$i++){
echo 'Question:'.$i.' <input type="text" name="question2[]" /> </br></br>';
echo '<input type="radio[]" name="question2">True</br>
<input type="radio[]" name="question1">False</br>';
echo '</br></br>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>
//Third file
<?php
if(isset($_POST['submit'])){
//array of input elements named "question1"
$question1 = $_POST['question1'];
//array of input elements named "radiooptions"
$radiooptions = $_POST['radiooptions'];
//get the length of those array
$question1length = count($question1);
$radiooptionslength = count($radiooptions);
//loop through array to get first input with four radiooptions inputs
for($x = 0; $x < $question1length; $x++) {
echo $question1[$x];
echo "<br>";
echo "<br>";
for($i = 0; $i < $radiooptionslength; $i++) {
echo $radiooptions[$i];
echo "<br>";
echo "<br>";
}
}
guys in first loop of the array [question1] I want to get first input in array of [question1] and in second loop I want to get the first 4 inputs of array [radiooptions] and when the loop gets second time I want it to give the second input of array [question1] and in the [radiooptions] array loop I want it to give me the next 4 inputs for example next 4 inputs will be 5,6,7,8.....But its giving me different [question] inputs but same first 4 item of array [radiooptions]
You would need to do something like the following using a simple pagination function on an array.
<?php
$array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
for($i = 1; $i < 5; $i++){
$utility = new Utility();
$utility->getItems($i, $array);
echo '<br/>';
}
class Utility{
public function getItems($index, $array, $count = 4){
$min = ($index - 1) * $count;
$max = $index * $count;
for($k = $min; $k < $max; $k++){
echo $array[$k] . ' ';
}
}
}
?>

PHP(simple): Trouble with loops

I'm very new to programming, I am trying to take create a converter from Celsius to Fahrenheit to Kelvin. The user inputs the values (in celsius) they want converted in the 2 input boxes and it creates a table using loops. The first set of data where it outputs the amounts in celsius looks great however the second data stream (fahrenheit) will only output one value of celsius which is the converted value of the final nuber in the celsius loop.
<form name="calculator" action="" method="post">
From: <input class="inputbox" type="number" name="one" value="" /><br />
<p>to</p><br>
To: <input class="inputbox" type="number" name="two" value="" /><br />
<input type="submit" class="submit" name="submit" value="Get Conversions!" />
</form>
<br>
<table border='1' cellpadding='5px'>
<tr>
<th>Degrees Celsius</th>
<?php
if ($_POST['submit']) {
$one = $_POST['one'];
$two = $_POST['two'];
}
if ($two < $one) {
echo "<p>Please put the lowest number in the first input box.</p>";
} else if ($one < (-273) OR $two < (-273)) {
echo "<p> Tempature cant go below -273 Celsius (0 kelvin), please enter higher values.</p>";
} else {
$c = $one - 1;
do {
$c++;
echo "<td>" . $c . "</td>";
} while ($c < $two);
}
?>
</tr>
<tr>
<th>Degrees Fahrenheit</th>
<?php
$f = (1.8 * $c) + 32;
do {
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>
</table>
Your have 2 problems in your code.
One problem is that you are only assigning c once, when you are running the first while, it's counting C up and then when you get to the second do{}while() it's allready at the maximum.
you should "reset" C after The first while loop like here:
<th>Degrees Fahrenheit</th>
<?php
$c = $one - 1;
Secondly your only counting your f variable once, you should either make a function for it (might be overkill in this case) or move your calculation of f down inside the while loop, the last part of your code would be something like this
Degrees Fahrenheit
<?php
$c = $one - 1;
do {
$f = (1.8 * $c) + 32;
$c++;
echo "<td>" . $f . "</td>";
} while ($c < $two);
$k = $x - 273;
?>
</tr>

php word check if they are the same

I need that they check my word witch i written in text field. If 2 words the same i should get message - "They are the same".
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
$a = array("snow", "ball", "side");
//print_r(array_random($a));
//print_r(array_random($a, 3));
?>
<form name='form' method='post' align = "center">
<?php
$zod = (array_random($a)); echo $zod; ?> <input type="text" name="name" id="name" ><?php
if((isset($_POST['name'])) && !empty($_POST['name']))
{
$name = $_POST['name'];
echo ' '.$name;
}
?><br/><br>
<input name="select" type="submit" onclick="select()" value="select" />
</form>
A simple comparison uses the '==' operator. These can be used to compare something. You want to compare a posted value with the one from your array. which is random:
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
if ( isset ( $_POST['submit'] ) && isset ( $_POST['name'] ) ) {
if ( $_POST['compare'] == $_POST['name'] )
echo "They are the same, hurray!<br />";
else
echo "These don't match!";
}
$a = array ( "snow", "ball", "side" );
$rand = array_random ( $a );
?>
<form name='form' method='post' align = "center">
Please type the word: <?php echo $rand; ?>
<input type="text" name="name" id="name" >
<input type="hidden" name="compare" id="compare" value="<?php echo $rand ?>" >
<input name="submit" type="submit" onclick="select()" value="select" />
</form>
As you wanted, a random value is picked and compared to what the user posted.

Form not outputting number [duplicate]

This question already has an answer here:
Output of numbers are incorrect sometimes.
(1 answer)
Closed 9 years ago.
Given an input of an expression consisting of a string of letters and operators (plus sign, minus sign, and letters. IE: ‘b-d+e-f’) and a file with a set of variable/value pairs separated by commas (i.e: a=1,b=7,c=3,d=14) write a program that would output the result of the inputted expression.
For example, if the expression input was ("a + b+c -d") and the file input was ( a=1,b=7,c=3,d=14) the output would be -3.
Thus far I have got this, it calculates the numbers but not sure how to calculate it by the letters abcd,
<html>
<head>
</head>
<body>
<form action = "" method = "GET">
Number 1: <input type = "text" name = "input[]" size=3>
<br/>
Number 2: <input type = "text" name = "input[]" size=3> <br>
Number 3: <input type = "text" name = "input[]" size=3> <br>
Number 4: <input type = "text" name = "input[]" size=3> <br>
<input type="hidden" name="calc" value ="yes">
<input type = "submit" name = "Calculate"/>
</form>
</body>
</html>
<?php
print_r($_GET);
$myInputs = $_GET['input'];
print_r($myInputs);
$myArray = array();
$myArray['a'] = 5;
$myArray['b'] = 10 ;
$myArray['c'] = 15 ;
$myArray['d'] = 20 ;
echo $myArray[$_GET['a']] + $myArray[$_GET['b']] ;
/*
if ($_GET['calc'] == "yes")
{
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
$d = $_GET['d'];
$total = $a+$b+$c+$d;
print "Total is: $total <p>";
}
*/
?>
<?php
$expression = '';
$input = array( 'a' => 5,
'b' => 10,
'c' => 15,
'd' => 20);
$total = 0;
$sum = '';
// Override default expression and input values
if(isset($_POST['Calculate']))
{
$input = $_POST['input']; //input being passed via POST method
$expression = $_POST['expression']; //expression being passed via POST method
}
// make sure the users expression is safe to use
if(preg_match('#^[a-z+-\s]+$#i', $expression))
{
$sum = $expression; //evaluates sum as expression
$input_letters = array(); //puts letters into a array
$input_operators = array(); //puts operators into a array
for ($i = 0; $i < strlen($expression) ; $i++) { //gets string length
if (($i % 2) == 0) {
$input_letters[] = $expression[$i]; //getting the character for the position i
} else {
$input_operators[] = $expression[$i];
}
}
//
for ($i = 0 ; $i < sizeof($input_letters); $i++) { //size of= numbers (4)
$value = $input[$input_letters[$i]]; //takes the value from the letters
if ($i == 0) {
$sum += $value; //adds the sum if = 0
} else {
if ($input_operators[$i-1] == '+') { //checks to see if the operator is +
$sum += $value;
} else if ($input_operators[$i-1] == '-') { //checks to see if the operator is -
$sum -= $value;
}
}
print_r($value);
}
} else {
echo "Error: expression not correct form"; //error message
}
?>
<form action="" method="post"> <!--Form begins-->
Number A: <input type="text" name="input[a]" value="<?php echo $input['a']; ?>" size=3> <br/>
Number B: <input type="text" name="input[b]" value="<?php echo $input['b']; ?>" size=3> <br>
Number C: <input type="text" name="input[c]" value="<?php echo $input['c']; ?>" size=3> <br>
Number D: <input type="text" name="input[d]" value="<?php echo $input['d']; ?>" size=3> <br>
<br>
Expression: <input type = "text" name = "expression" value="<?php echo $expression ?>"> =
<?php echo $sum; ?><br>
Sum: <?php echo $sum; ?><br>
<input type="submit" name="Calculate" />
</form>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$( "form" ).submit(function( event ) {
var val_is_good=($('input[name="expression"]').val().length==4)?true:false;
if(!val_is_good){alert('Enter four letters or die!');}
});
</script>
Now I added a script to tell users if they enter more then 4 letters a alert message says please only enter four letters, would someone know why my script is outputing the alert message even when enter only four letters?
<html>
<head>
</head>
<body>
<form action = "" method = "GET">
Number 1: <input type = "text" name = "input[]" size=3>
<br/>
Number 2: <input type = "text" name = "input[]" size=3> <br>
Number 3: <input type = "text" name = "input[]" size=3> <br>
Number 4: <input type = "text" name = "input[]" size=3> <br>
<input type="hidden" name="calc" value ="yes">
<input type = "submit" name = "Calculate"/>
</form>
</body>
</html>
<?php
print_r($_GET);
$myInputs = $_GET['input'];
//you are assigning $_GET values to an array myInputs so no need here
print_r($myInputs);
echo $myInputs[0]+ $myInputs[1];
$myArray = array();
$myArray['a'] = 5;
$myArray['b'] = 10 ;
$myArray['c'] = 15 ;
$myArray['d'] = 20 ;
// In case of myarray it has keys a,b,c,d only... so need of using $_GET here also.
echo $myArray['a']+$myArray['b'];
// but if you want to it directly from$_GET values.
echo $_GET['input'][0]+ $_GET['input'][1];
?>
I found a solution at this site. Here is the code from that post:
<?php
$expression = '';
$input = array( 'a' => 5,
'b' => 10,
'c' => 15,
'd' => 20);
$total = 0;
$sum = '';
// Override default expression and input values
if(isset($_POST['Calculate']))
{
$input = $_POST['input'];
$expression = $_POST['expression'];
}
// make sure the users expression is safe to use
if(preg_match('#^[a-z+-\s]+$#i', $expression))
{
$sum = $expression;
$input_letters = array();
$input_operators = array();
for ($i = 0; $i < strlen($expression) ; $i++) {
if (($i % 2) == 0) {
$input_letters[] = $expression[$i];
} else {
$input_operators[] = $expression[$i];
}
}
//
for ($i = 0 ; $i < sizeof($input_letters); $i++) {
$value = $input[$input_letters[$i]];
if ($i == 0) {
$sum += $value;
} else {
if ($input_operators[$i-1] == '+') {
$sum += $value;
} else if ($input_operators[$i-1] == '-') {
$sum -= $value;
}
}
}
} else {
echo "Error: expression not correct form";
}
?>
<form action="" method="post">
Number A: <input type="text" name="input[a]" value="<?php echo $input['a']; ?>" size=3> <br/>
Number B: <input type="text" name="input[b]" value="<?php echo $input['b']; ?>" size=3> <br>
Number C: <input type="text" name="input[c]" value="<?php echo $input['c']; ?>" size=3> <br>
Number D: <input type="text" name="input[d]" value="<?php echo $input['d']; ?>" size=3> <br>
<br>
Expression: <input type = "text" name = "expression" value="<?php echo $expression ?>"> =
<?php echo $sum; ?><br>
Sum: <?php echo $sum; ?><br>
<input type="submit" name="Calculate" />
</form>

Categories