Saisie de 2 matrices puis calcul(Somme,produit)

Résolu
oum87 Messages postés 21 Date d'inscription lundi 2 août 2010 Statut Membre Dernière intervention 23 janvier 2013 - 8 mai 2011 à 17:39
soukaouta Messages postés 39 Date d'inscription vendredi 29 octobre 2010 Statut Membre Dernière intervention 16 novembre 2012 - 13 mai 2011 à 18:51
page1:form1.php
//Cette page me permet de saisir le nombre de lignes et de colonnes des 2 matrices,
je n'ai pas trouvé de problème dessus

<?php
<form action="form2.php" method="post">
MATRICE A:

Nombre de lignes de A:

Nombre de Colonnes de A:




MATRICE B:

Nombre de lignes de B:

Nombre de colonnes de B:



</form>
?>

</html>


page:form2.php
//je voudrais initialiser mes matrices en tenant compte des variables que j'avais saisi auparavant,mon code ne tourne pas !!le code que je propose est le suivant:




<?php

<form method="post" action="form3.php">
$a=$_POST['nlA'];
$b=$_POST['ncA'];
$c=$_POST['nlB'];
$d=$_POST['ncB'];

$MATA=array();
$MATB=array();

for($i=0; $i<$a; $i++)
{ $MATA[i]=array();
for($j=0; $j<$b; $j++)
{$MATA[$i][$j]:

}
}


for($k=0; $k<$c; $k++)
{ $MATB[k]=array();
for($l=0; $l<$d; $l++)
{$MATB[$k][$l}:
}
}


/</form>
?>


</html>
A voir également:

9 réponses

oum87 Messages postés 21 Date d'inscription lundi 2 août 2010 Statut Membre Dernière intervention 23 janvier 2013
10 mai 2011 à 16:51
Bonjour,
Finalement,j'ai tjs besoin de votre aide pour ke je puisse publier le code
Voila!!je bloke sur la page3.php ki ne se génère guère,
j'ai ajouter des variables de session ds la page 2 pour que je puisse les utiliser à la page 3
le code est le suivant:

<!--
page1:form1.php
//Cette page me permet de saisir le nombre de lignes et de colonnes des 2 matrices,
je n'ai pas trouvé de problème dessus
-->




<form action= "form2.php" method="post">
MATRICE A:

Nombre de lignes de A:

Nombre de Colonnes de A:




MATRICE B:

Nombre de lignes de B:

Nombre de colonnes de B:






</form>




</html>
page2:form2.php
//je voudrais initialiser mes matrices en tenant compte des variables que j'avais saisi auparavant,mon code ne tourne pas !!le code que je propose est le suivant:
j'ai ouvert une session pr que je puisse transmettre le nombre de lignes et de colonnes de chaque matrice à la page suivante qui permet de faire le test,

<?php
// On démarre la session AVANT d'écrire du code HTML
session_start();
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>


<?php

echo '<form method="post" action="form3.php">';
echo $a=$_POST['nlA'];
echo $b=$_POST['ncA'];
echo $c=$_POST['nlB'];
echo $d=$_POST['ncB'];
$_SESSION['la']='a';
$_SESSION['ca']='b';
$_SESSION['lb']='c';
$_SESSION['cb']='d';


echo '<hr>';


$MATA=array();
$MATB=array();

echo " la matrice A :
";
for($i =0; $i<$a; $i++){



for($j= 0; $j<$b; $j++){
//$MATA[$i]=array();
echo $MATA[$i][$j]=''."\r\n";
}
echo '
';
}

echo '';
//print_r($MATA);
echo '

';

echo " la matrice B :
";
for($k =0; $k<$c; $k++){
//$MATB[$k]= array();
for($l=0; $l<$d; $l++){
echo $MATB[$k][$l]=''."\r\n";
}
echo '
';
}

echo '';
//print_r($MATA);
echo '

';
$_SESSION['A']='MATA';
$_SESSION['B']='MATB';

?>

<select name="choix">
<option value="affiche A">affiche A</option>
<option value="affiche B">affiche B</option>
<option value="somme">somme</option>
<option value="produit">produit</option>
<option value="transposee A">transposee A</option>
<option value="transposee B">transposee B</option>
</select>

<?php
echo '';
echo'</form>';
?>


</html>

la page form3.php que je propose est la suivante:(mais elle ne marche pas)

<?php
// On démarre la session AVANT d'écrire du code HTML
session_start();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>


<?php include ("fonction.php"); ?>

<?php
$s=array();
$p=array();
$t=array();

switch($ch)


case ($_POST['choix']=="affiche A") :

echo " la matrice A est :
";
afficher($_SESSION['A'],$_SESSION['la'],$_SESSION['ca'],"center");
break;


case ($_POST['choix'] == "affiche B") :

echo " la matrice B est :
";
afficher($_SESSION['B'],$_SESSION['lb'],$_SESSION['cb'],"center");
break;


case ($_POST['choix'] == "somme") :

echo " la Somme est :
";
somme($_SESSION['A'],$_SESSION['B'],$_SESSION['la'],$_SESSION['ca'],"center",$s);
afficher($s,$_SESSION['la'],$_SESSION['ca'],"center");
break;

case ($_POST['choix'] == "produit") :

echo " le produit A est :
";
Produit($_SESSION['A'],$_SESSION['B'],$_SESSION['la'],$_SESSION['cb'],$_POST['lb'],$p);
afficher($p,$_SESSION['la'],$_SESSION['cb'],"center");
break;


case($_POST['choix'] == "transposee A") :

echo " la transposee de A est :
";
transpose($_SESSION['A'],$_SESSION['la'],$_SESSION['ca'],$t);
afficher($t,$_SESSION['la'],$_SESSION['ca'],"center");
break;

case($_POST['choix'] == "transposee B") :

echo " la transposee de B est :
";
transpose($_SESSION['B'],$_SESSION['lb'],$_SESSION['cb'],$t);
afficher($t,$_SESSION['lb'],$_SESSION['cb'],"center");
break;

?>

?>

</html>




//j'ai inclut une page fonction.php qui permet de faire les calculs;cette page ne comprend pas d'érreur en principe:

page fonction.php


<?php
// On démarre la session AVANT d'écrire du code HTML
session_start();
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns ="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<?php

function afficher($a,$x,$y,$l)
{

echo "\";
for($i=0;$i<$x;$i++)
{
echo\"----
\";
for($j=0;$j<$y;$j++)
{
$f=$a[$i][$j];
echo \" $f, \";
}
echo \"\";
}
echo "
";
}

function somme($a,$b,$x,$y,$l,$s)
{
for($i=0;$i<$x;$i++)
{
for($j=0;$j<$y;$j++)
{
$s[$i][$j]=$a[$i][$j]+$b[$i][$j];
}
}

}

function Produit($a,$b,$la,$cb,$ca,&$p)
{
for($i=0;$i<$la;$i++)
{
for($j=0;$j<$cb;$j++)
{
$p[$i][$j]=0;
for($k=0;$k<$ca;$k++)
{
$p[$i][$j]=$p[$i][$j]+$a[$i][$k]*$b[$k][$j];
}
}
}
}

function transpose($a,$x,$y,&$t)
{
for($i=0;$i<$x;$i++)
{
for($j=0;$j<$y;$j++)
{
$t[$j][$i]=$a[$i][$j];
}
}
}

?>

</html>



Voila!!Aidez moi svp !!
3
Rejoignez-nous