ASSIGNMENT NO. 4 : TO STUDY ARRAYS
Set A
1. Create your array of 30 high temperatures, approximating the weather for a spring month, then find the average high temp, the five warmest high temps and the five coolest high temps. Display the result on the browser.
Hint: a) Use array_slice b) the HTML character entity for the degree sign is & deg;.
Solution:-
<html>
<head>
<title>High temparature Array</title>
</head>
<h2>High temperature for spring month </h2>
<?php
$highTemps = array(68,70,72,58,60,79,82,73,75,77,73,58,63,79,78,68,72,73,80,79,68,72,75,77,73,78,82,85,89,83);
$count=count($highTemps);
$total=0;
foreach($highTemps as $h)
{
$total+=$h;
}
$avg = round($total/$count);
echo"<p> the average high temp for the month was $avg ° f.<p>\n";
rsort($highTemps);
$topTemps = array_slice($highTemps,0,5);
echo"<p>the warmest fire high temperature were;<br>\n";
foreach($topTemps as $t)
{
echo "$t °F<br>\n";
}
echo"<p>";
$lowTemps = array_slice($highTemps,-5,5);
echo "<p> the coolest fire high temp.were:<br>\n";
foreach($lowTemps as $l)
{
echo "$l °F<br> \n";
}
echo"<p>";
?>
</html>
2. Write a menu driven program to perform the following stack and queue related operations:[Hint: use Array_push(), Array_pop(), Array_shift(), Array_unshift() ]
a) Insert an element in stack
b) Delete an element from stack
c) Display the contents of stack
d) Insert an element in queue
e) Delete an element from queue
f) Display the contents of queue
Solution:-
html
file!!!
<!DOCTYPE
html>
<html>
<body>
<form
name=f method=get action=http://192.168.100.252/sy31/a4sa2.php>
<center>
<table>
<tr><th
colspan=2><h1>Choose Menu</h1></th></tr>
<tr><td>Ente
an array:</td> <td><input type=text name
=a1></td></tr><br>
<tr><td>Enter
an element to be inserted:</td> <td><input type=text
name =w></td></tr><br>
<tr><td><input
type=radio name=op value=1></td> <td>a.Insert an
element in stack.</td></tr><br>
<tr><td><input
type=radio name=op value=2></td> <td>b.Delete an
element from stack.</td></tr><br>
<tr><td><input
type=radio name=op value=3></td> <td>c.Display the
contents of stack.</td></tr><br>
<tr><td><input
type=radio name=op value=4></td> <td>d.Insert an
element in queue.</td></tr><br>
<tr><td><input
type=radio name=op value=5></td> <td>d.Delete an
element from queue.</td></tr><br>
<tr><td><input
type=radio name=op value=6></td> <td>d.Display the
contents of queue.</td></tr><br>
<tr><th
colspan=2><input type=submit name=sb value=Submit></th></tr>
</table>
</center>
</body>
</html>
/*as4seta2.php*/
<?php
$w=explode(",",$_GET['a1']);
//1,2,3,7,10
$a=$_GET['op'];
/*$b=array(1,2,3,4,5,6,7,8,9);*/
switch($a)
{
case
1:
print_r($w);
array_push($w,10);
echo"<br>";
echo"After
insert an element in stack:<br>";
print_r($w);break;
case
2:
print_r($w);
array_pop($w);
echo"<br>";
echo"After
delete an element from stack :<br>";
print_r($w);break;
case
3:
echo
"Contents of stack.<br>";
print_r($w);break;
case
4:
print_r($w);
array_unshift($w,9);
echo"<br>";
echo"After
insert an element in queue:<br>";
print_r($w);break;
case
5:
print_r($w);
array_shift($w);
echo"<br>";
echo"After
delete an element in queue:<br>";
print_r($w);break;
case
6:
echo
"Contents of queue<br>";
print_r($w);break;
}
?>
Set B
1. Write a PHP script that inserts a new item in an array at any position.
(hint : use array_splice())
Solution :-
<?php
$given=array('1','2','3','4','5');
echo "Given Array: <br>";
foreach($given as $x)
{
echo "$x";
}
$inserted ='@';
array_splice($given,3,0,$inserted);
echo "<br> After inserting '@' the array is :"."\n";
foreach($given as $x)
{
echo "$x";
}
echo "\n";
?>
<!--
O/P:
Given Array:
12345
After inserting '@' the array is : 123@45
-->
2. Define an array. Find the elements from the array that matches the given value using appropriate search function.
Solution:-
<!DOCTYPE
html>
<html>
<body>
<form
name=f method=get action=http://localhost/as4setb2.php>
<center>
<table>
<tr><th
colspan=2><h1>Choose Menu</h1></th></tr>
<tr><td>Ente
an array:</td> <td><input type=text name
=w></td></tr><br>
<tr><th
colspan=2><input type=submit name=sb value=Submit></th></tr>
</table>
</center>
</body>
</html>
/*as4setb2.php*/
<?php
$a=array('inaara
rajwani',' bcs', 'ty');
print_r($a);
$w1=$_GET
['w'];
print_r($w1);
if(in_array($w1,$a))
{
echo
"The given element is found in defined array ";
}
else
{
echo
"The given element is not found in defined array ";
}
?>
Set C
1. Write a PHP script to sort the following associative array :
array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40") in
a) ascending order sort by value
b) ascending order sort by Key
c) descending order sorting by Value
d) descending order sorting by Key
Solution:-
/*as4setc1*/
<?php
$array=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");
echo
"Associative array : Ascending order sort by value <br>";
asort($array);
foreach($array
as $y=>$y_value)
{
echo
"Age of ".$y." is : ".$y_value."<br>";
}
echo
"Associative array : Ascending order sort by Key <br>";
ksort($array);
foreach($array
as $y=>$y_value)
{
echo
"Age of ".$y." is : ".$y_value."<br>";
}
echo
"Associative array : Descending order sorting by Value<br>";
arsort($array);
foreach($array
as $y=>$y_value)
{
echo
"Age of ".$y." is : ".$y_value."<br>";
}
echo
"Associative array : Descending order sorting by Key<br>";
krsort($array);
foreach($array
as $y=>$y_value)
{
echo
"Age of ".$y." is : ".$y_value."<br>";
}
?>
2. Write a menu driven program to perform the following operations on associative arrays:
a) Split an array into chunks
b) Sort the array by values without changing the keys.
c) Filter the odd elements from an array.
d) Merge the given arrays.
e) Find the intersection of two arrays.
f) Find the union of two arrays.
g) Find set difference of two arrays.
Solution:-
<html>
<title>Assign4_SETC_Q2</title>
<body>
<center>
<table>
<form
name=f method=get action="http://localhost/as4setc2.php">
<tr><th
colspan=2>Menu</th></tr>
<tr><td>1.Split
array into chunks</td><td><input type=radio name=r
value=1></td></tr>
<tr><td>2.Sort
array by value without changing key</td><td><input
type=radio name=r value=2></td></tr>
<tr><td>3.Filter
odd elements</td><td><input type=radio name=r
value=3></td></tr>
<tr><td>4.Merge
given array</td><td><input type=radio name=r
value=4></td></tr>
<tr><td>5.Find
intersection</td><td><input type=radio name=r
value=5></td></tr>
<tr><td>6.Find
union</td><td><input type=radio name=r
value=6></td></tr>
<tr><td>7.Find
set difference</td><td><input type=radio name=r
value=7></td></tr>
<tr><td>SUBMIT</td><td><input
type=submit name=s value=go></td></tr>
<center>
</form>
</table>
</body>
</html>
/*as4setc2.php*/
<?php
$c=$_GET['r'];
$a=array('pen'=>15,'pencil'=>5,'rubber'=>3,'book'=>30);
$a1=array('ira'=>15,'nia'=>57,'ria'=>3,'alia'=>30);
//$a2=array(15,48,10,155,2,78);
switch($c)
{
case
1:
echo"<br>Array
in chunks of two <br>";
print_r(array_chunk($a,2,true));
echo"<br><br>";
print_r(array_chunk($a1,2,true));
echo"<br><br>";
break;
case
2:
print_r($a);
asort($a);//array
sort by values
echo"<br>Array
in ascending order<br>";
print_r($a);
arsort($a);//reverse
array sort by values
echo"<br>Array
in desecending order<br>";
print_r($a);
break;
case
3:
function
isodd($v)
{
return
$v%2;
}
print_r($a);
echo
"<br>";
echo"<br>Filter
the odd elements from an array.<br>";
print_r(array_filter($a,'isodd'));
break;
case
4 :
echo
"<br>1st array<br>";
print_r($a);
echo
"<br>2nd array<br>";
print_r($a1);
echo
"<br>Array Merge<br>";
print_r(array_merge($a,$a1));
break;
case
5:
echo
"<br>1st array<br>";
print_r($a);
echo
"<br>2nd array<br>";
print_r($a1);
echo
"<br>Array Intersection<br>";
print_r(array_intersect($a,$a1));
break;
case
6:
echo
"<br>1st array<br>";
print_r($a);
echo
"<br>2nd array<br>";
print_r($a1);
echo
"<br>Array Union<br>";
$union=array_merge($a,$a1);
print_r(array_unique($union));
break;
case
7:
echo
"<br>1st array<br>";
print_r($a);
echo
"<br>2nd array<br>";
print_r($a1);
echo
"<br>Array Differnce<br>";
print_r(array_diff($a,$a1));
break;
}