Question

In: Computer Science

Please code the arrays. This is in PHP/HTML Perform some calculations: 1. $ages = [24, 12,...

Please code the arrays. This is in PHP/HTML

Perform some calculations:

1. $ages = [24, 12, 10, 17, 23, 29, 35, 32, 30, 22] Using a FOR loop, calculate the sum and average of these numbers, then print them. Please provide legible output.

2. If I change the array to $ages = [1 => 24, 12, 10, 17, 23, 29, 35, 32, 30, 22] How will the program change? Re-run the program and provide output

3. Assume that the above array is an associative array as follows:

$ages = ['Sam' => 24, 'Tina' => 12, 'Liz' => 10, 'Tom' => 17, 'Dave' => 23, 'Sandra' => 29, 'Kris' => 35, 'Mike' => 32, 'Daja' => 30, 'Rajan' => 22]

Write a program to print each name and its associated age on a single line; i.e., Sam => 24 . . . and so on

Solutions

Expert Solution

Answer 1:

PHP code:

<?php
$sum = 0;
$avg = 0;
$ages = [24, 12, 10, 17, 23, 29, 35, 32, 30, 22];
for ($i = 0; $i < count($ages); $i++){
$sum += $ages[$i];
}
$avg = $sum/count($ages);
echo "Sum = $sum\n";
echo "Average = $avg\n";

Output:

Answer 2:

The indexing of the array will now start from index 1 instead of index 0. Therefore, for loop should now be iterated over 1 to 10.

Output when program is run without modifying the FOR loop:

Modified program:

<?php
$sum = 0;
$avg = 0;
$ages = [1 => 24, 12, 10, 17, 23, 29, 35, 32, 30, 22];
for ($i = 1; $i <= count($ages); $i++){
$sum += $ages[$i];
}
$avg = $sum/count($ages);
echo "Sum = $sum\n";
echo "Average = $avg\n";

Output:

Answer 3:

PHP code:

<?php
$ages = ['Sam' => 24, 'Tina' => 12, 'Liz' => 10, 'Tom' => 17, 'Dave' => 23, 'Sandra' => 29, 'Kris' => 35, 'Mike' => 32, 'Daja' => 30, 'Rajan' => 22];
foreach ($ages as $name => $age){
echo "$name => $age\n";
}

Output:

FOR ANY HELP JUST DROP A COMMENT


Related Solutions

Please create a PHP action codes for the HTML CODE provided below. <!DOCTYPE html> <html> <head>...
Please create a PHP action codes for the HTML CODE provided below. <!DOCTYPE html> <html> <head> <title> Project for keeping all your input </title> </head> <body> <h1>Welcome to this Web Based Test!!!</h1> <p>Please answer the following questions:</p> <hr/> <form action="" method="post"> Name: <input type="text" name="name" value=""> <font color=red>*</font><br/><br/> E-mail: <input type="text" name="email" value=""> <font color=red>*</font><br> <hr/> Choose your major area of study: <select name="major"> <option value="Digital Media" >Digital Media</option> <option value="Software" >Software</option> <option value="Security" >Security</option> <option value="Business" >Business</option> <option value="Other"...
- Create an html form (bank form) and the PHP code with 2 accounts with options...
- Create an html form (bank form) and the PHP code with 2 accounts with options to transfer, deposit or withdraw money.
write php code for buying and selling web using sql, javascript ,html ,css style
write php code for buying and selling web using sql, javascript ,html ,css style
Please provide HTML code for the following: - Create a page that lists a set of...
Please provide HTML code for the following: - Create a page that lists a set of audio files and shows their duration - Create a page that lists a set of video files and plays a different video when you click on the play icon
a) write a python programme for converting url.to html please provide valid code and please share...
a) write a python programme for converting url.to html please provide valid code and please share screenshots to me
Use a style sheet to define the following rules and implement the given HTML code. Please...
Use a style sheet to define the following rules and implement the given HTML code. Please put your style information within the same file as the HTML code. Rules • Hyperlinks using the nodec class should display no decoration. • Hyperlinks should display text in white with a green background color when the mouse pointer is held over the link. (use the hover pseudo-class) • Unordered lists not nested within any other lists should be displayed in blue text and...
Monopoly ABC is asking you to perform some calculations to determine its profit at the profit...
Monopoly ABC is asking you to perform some calculations to determine its profit at the profit maximizing quantity. The equation of demand for the monopoly is P = 109 - 3Q. The TC function = 30 + 3Q. The MR function = 109 - 6Q. Let us assume constant MC at $3. (Note that P = price, Q = quantity, MR = marginal revenue and MC = marginal cost.). For Monopoly ABC calculate the profit (or loss) at the profit...
specify the code to create html with the following content: 1. a paragraph stating, this is...
specify the code to create html with the following content: 1. a paragraph stating, this is an example. 2 image logo.jpg with alternate text ABC Logo below the paragraph created in 1.
1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2)...
1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2) a) How would you compare two arrays to check if they have the same values? b) Assume array1 and array2 are int arrays with 10 elements in each. if(array1 == array2) What is this comparing? 3 a) Can you encounter memory violation using an array? b) If yes explain. If no, explain why not.
Perform the following tasks with NumPy arrays. All of them can be done (elegantly) in 1...
Perform the following tasks with NumPy arrays. All of them can be done (elegantly) in 1 to 3 lines. (a) Create an 8 × 8 array with ones on all the edges and zeros everywhere else. (b) Create an 8 × 8 array of integers with a checkerboard pattern of ones and zeros. (c) Given the array c = np.arange(2, 50, 5), make all the numbers not divisible by 3 negative. (d) Find the size, shape, mean, and standard deviation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT