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.
How are PHP arrays different than arrays in other programming languages? Please discuss in depth.
How are PHP arrays different than arrays in other programming languages? Please discuss in depth.
Utilizing PHP and HTML code Create a form that has text inputs for two numbers and...
Utilizing PHP and HTML code Create a form that has text inputs for two numbers and a submit button. Submit the values to a program that calculates and displays the GCD of the two numbers. The greatest common divisor of two integers a and b is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and 0 is that number. For example:...
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
Topic: HTML Styling Elements - CSS3 Write some HTML code that includes three Semantic HTML tags....
Topic: HTML Styling Elements - CSS3 Write some HTML code that includes three Semantic HTML tags. Describe the semantic meaning of each one you choose.
Compare PHP code used to create and update two-dimensional arrays to another programming language. Which is...
Compare PHP code used to create and update two-dimensional arrays to another programming language. Which is easier and more efficient? Why? Can be compared to any other well used language.
1. Create a PHP page with standard HTML tags. Remember to save the file with the...
1. Create a PHP page with standard HTML tags. Remember to save the file with the .php extension. Inside the <body> tag, create a PHP section that will show the text "Hello World!" 2. For this exercise, echo the phrase "Twinkle, Twinkle little star." Create two variables, one for the word "Twinkle" and one for the word "star". Echo the statement tothe browser. 3. PHP includes all the standard arithmetic operators. For this PHP exercise, you will use them along...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT