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

- 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...
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.
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...
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...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT