Question

#!/usr/bin/php <?php $A = array( 1, 2, 3, 4 ); $B = array( 1, 2, 3, 4); foreach($A as $i) { foreach ($B as $j) { print (Foo\

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a) A and B both the array contain elements from 1 to 4 . For each element of A the script prints foo 4 times. Since there are 4 elements in array A. Thus the number of foo output is 4X4=16

(b) When $i is 1, bar will be printed 3 times when $j is 2,3,4

When $i is 2, bar will be printed 2 times when $j is 3,4

When $i is 3, bar will be printed 1 times when $j is 4

When $i is 4, bar will be printed 0 times

Thus number of times of bar output is (3+2+1) = 6

(c) Following the same logic as (b)

When $i is k, bar will be printed (n - k) times where k can take value in 1,2,...,n

Thus total number of "bar" output is

(n-1 ) + (n-2) + (n - 3) + ... + 1

= (n-1)n/2 [sum of first (n-1) natural numbers]

=\frac{n(n-1)}{2}

Add a comment
Know the answer?
Add Answer to:
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT