Laboratorio 06: Programación con arrays o Matrices
Programación con arrays o Matrices
Ejercicio1.php: creando vector y almacenando datos numéricos
<html>
<head>
<title>Instituto Pedro P. diaz</title>
</head>
<body>
<?php
$vec[0]=20;
$vec[1]=30;
$vec[2]=50;
echo $vec[0]; echo "<br>";
echo $vec[1]; echo "<br>";
echo $vec[2]; echo "<br>";
$suma=$vec[0]+$vec[2];
echo $suma;
?>
</body>
</html>
Ejercicio2.php: obviando el índice dentro del corchete en la creación, este se autogenera
<html>
<head> <title>Instituto Pedro P. Diaz</title> </head>
<body>
<?php
$vec[]="Arequipa";
$vec[]="Lima";
$vec[]="Cusco";
$vec[]="Piura";
$vec[]="Tacna";
echo $vec[0]; echo "<br>"; echo $vec[1]; echo "<br>"; echo $vec[2]; echo "<br>";
echo $vec[3]; echo "<br>"; echo $vec[4]; echo "<br>";
?>
</body>
</html>
Ejercicio3.php: obviando el índice dentro del corchete en la creación, este se autogenera,
así mismo usando el for() para acceso de su contenido
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$vec[]="Arequipa";
$vec[]="Lima";
$vec[]="Cusco";
$vec[]="Piura";
$vec[]="Tacna";
for($x=0; $x<5; $x++)
{
echo "$vec[$x] <br>";
}
?>
</body>
</html>
Ejercicio4.php aplicando función count() para determinar el numero de elementos del array
<!doctype html>
<html>
<head> <meta charset="utf-8"> <title>Instituto Pedro P. Diaz</title></head>
<body>
<?php
$vec[]="Desarrollo de Sistemas de Información";
$vec[]="Contabilidad";
$vec[]="Secretariado";
$vec[]="Construcción Civil";
$vec[]="Agropecuaria";
$vec[]="Mecatrónica";
$vec[]="Electrónica";
$vec[]="Electricidad";
$vec[]="Mecanica de produccion";
for($x=0;$x<count($vec);$x++)
{ echo "$vec[$x] <br>"; }
?>
</body>
</html>
Ejercicio5. Aplicando el constructor predefinido del Php para crear arrays: array()
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$vec=array("Arequipa","Lima","Cusco","Piura","Tacna");
for ($x=0; $x<count($vec); $x++) {
echo "$vec[$x] <br>";
}
?>
</body>
</html>
Ejercicio6. Aplicando arrays asociativos: Este tipo de vectores no es común a otros lenguajes,
pero en PHP son de uso indispensable en distintas situaciones. Un vector asociativo permite
acceder a un elemento del vector por medio de un subíndice de tipo string entre los
corchetes []
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$vec['dni']="29324747";
$vec['nombre']="Pedro Pablo Díaz Cornejo";
$vec['direccion']="Av. Pizarro Nro 130 J.L.B. y Rivero";
echo $vec['dni'];
echo $vec['nombre'];
echo $vec['direccion'];
?>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$vec=array('dni'=>"29324747",'nombre'=>"Pedro Pablo", 'direccion'=>"Av. Pizarro
130 JLBR" ,'fecnac'=>"14/11/1956");
echo $vec['dni']; echo $vec['nombre'];
echo $vec['direccion']; echo $vec['fecnac'];
?>
</body>
</html>
Ejercicio 8. Aplicando La función var_dump nos devuelve la cantidad de elementos del
arreglo, sus nombres y el tipo
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$vec=array('dni'=>"41015588",'nombre'=>"Juan Carlos Oblitas", 'direccion'=>"Puerto
Bravo 100", 'fecnac'=>"14/11/1957",
'Sueldo'=>15000);
var_dump($vec);
echo "<br>";
$x=5000;
var_dump($x); echo "<br>";
$y=true;
var_dump($y);
echo "<br>";
$z=58.25;
var_dump($z);
?>
</body>
</html>
Ejercicio 9. Comparando vectores
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$vec1=array(0=>"pera",1=>"manzana");
$vec2=array(0=>"pera",1=>"manzana");
echo $vec1===$vec2;
?>
</body>
</html>
Ejercicio 10. Aplicando foreach, teniendo en cuenta que la instrucción foreach() recorre cada
elemento de un determinado vector y extrae su clave y valor que contiene
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$visitas["lunes"] = 200;
$visitas["martes"] = 186;
$visitas["miercoles"] = 136;
$visitas["jueves"] = 222;
$visitas["viernes"] = 1000;
$visitas["sabado"] = 2000;
$visitas["feriado"] = 12000;
foreach( $visitas as $key => $value) {
echo "Clave: $key <br>";
echo "Valor: $value <br>";
echo "<hr>";
}
?>
</body>
</html>
Ejercicio 11. Aplicando arrays multidimensionales
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$calendario[] = array (1, "Enero", 31);
$calendario[] = array (2, "Febrero", 28);
$calendario[] = array (3, "Marzo", 31);
$calendario[] = array (4, "Abril", 30);
$calendario[] = array (5, "Mayo", 31);
$calendario[] = array (6, "Junio", 30);
$calendario[] = array (7, "Julio", 31);
$calendario[] = array (8, "Agosto", 31);
for ($x=0; $x<count($calendario); $x++) {
echo "Id:" . $calendario[$x][0] . "<br>";
echo "Mes:" . $calendario[$x][1] . "<br>";
echo "Días del mes:" . $calendario[$x][2] . "<br>";
echo "<hr>";
}
?>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
$calendario[] = array (1, "Enero", 31);
$calendario[] = array (2, "Febrero", 28);
$calendario[] = array (3, "Marzo", 31);
$calendario[] = array (4, "Abril", 30);
$calendario[] = array (5, "Mayo", 31);
$calendario[] = array (6, "Junio", 30);
$calendario[] = array (7, "Julio", 31);
$calendario[] = array (8, "Agosto", 31);
foreach($calendario as $clave => $valor) {
echo $clave;
echo "<br>";
echo "Id: ". $valor[0]. "<br>";
echo "Mes: ". $valor[1]. "<br>";
echo "Días: ". $valor[2]. "<br>";
echo "<hr>";
}
?>
</body>
</html>
Ejercicio 13. Aplicando printf
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<?php
printf("Formato decimal: %d, %d <br>",2,10);
printf("Formato double: %f <br>",88);
printf("Formato double redondeado: %.2f <br>",2.5687);
printf("El número dos con diferentes formatos: %d %f %5.2f",2,4,6);
?>
</body>
</html>
<html>
<head>
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<form action="Ejercicio14_2.php" method="post">
Ingrese un número : <input type="text" name="numero"> <br>
<input type="submit" value="Mostrar"><br>
</form>
</body>
</html>
Ejercicio14_2.php
<?php
$num=$_REQUEST['numero'];
printf("El número en decimal es: %d <br>",$num);
printf("El número en binario es: %b <br>",$num);
printf("El número en octal es: %o <br>",$num);
printf("El número en hexadecimal es: %x <br>",$num);
?>
Ejercicio 13.3
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<form action="Ejercicio15_2.php" method="post">
Ingrese un número menor: <input type="text" name="menor"> <br>
Ingrese un número mayor: <input type="text" name="mayor"> <br>
<input type="submit" value="Mostrar"><br>
</form>
</body>
</html>
Ejercicio15_2.php
<?php
$menor=$_REQUEST['menor'];
$mayor=$_REQUEST['mayor'];
for($x=$menor;$x<=$mayor;$x++){
printf("El número %d en ascci es: %c <br>",$x, $x);
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Instituto Pedro P. Diaz</title>
</head>
<body>
<form name="form1" method="post" action="Ejercicio16_2.php">
<table border="1" width=100% height=400>
<tr>
<td colspan=3 bgcolor="#aaaaaa" align="center"> FACTURACION</td>
</tr>
<tr bgcolor="#aaaaaa" align="center">
<td>Cantidad </td>
<td>Descripción </td>
<td>Precio </td>
</tr>
<tr>
<td><input type="text" name="c1" value= "-" </td>
<td>Monitor </td>
<td><input type="text" name="p1" value= "-" </td>
</tr>
<tr>
<td><input type="text" name="c2" value= "-" </td>
<td>Procesador </td>
<td><input type="text" name="p2" value= "-" </td>
</tr>
<tr>
<td><input type="text" name="c3" value= "-" </td>
<td>Case </td>
<td><input type="text" name="p3" value= "-" </td>
</tr>
<tr>
<td><input type="text" name="c4" value="-" </td>
<td>Teclado </td>
<td><input type="text" name="p4" value="-" </td>
</tr>
<tr align="center">
<td colspan=3> <input type="submit" action="-"> </td>
</tr>
</table>
</form>
</body>
</html>
Ejercicio16_2.php
<?php
$c1=$_REQUEST['c1'];
$c2=$_REQUEST['c2'];
$c3=$_REQUEST['c3'];
$c4=$_REQUEST['c4'];
$p1=$_REQUEST['p1'];
$p2=$_REQUEST['p2'];
$p3=$_REQUEST['p3'];
$p4=$_REQUEST['p4'];
$subtotal=$c1*$p1 + $c2*$p2 + $c3*$p3 + $c4*$p4;
$igv= $subtotal * 0.18;
$total= $igv+$subtotal;
printf("-------------------------- Subtotal: S/. %.2f <br>" ,$subtotal);
printf("-------------------------- IGV: S/. %.2f <br>" ,$igv);
printf("-------------------------- Total: S/. %.2f <br>" ,$total);
?>
Comentarios
Publicar un comentario