Jump to content

Basic Loop and Itterational Variable Question!


laraggg111

Recommended Posts

If anyone could give me some advice on this problem that would be great!

Rewrite the code below so it would contain only one loop and only one itterational variable would be used.

$result = Array();

for ($x = 0; $x < 6; $x++)
{
    for ($y = 0; $y < 6; $y++)
    {
        for ($z = 0; $z < 6; $z++)
        {
            $result[$x][$y][$z] = $x * $y * $z;
        }
    }
}
 

Thank you so much

Link to comment
Share on other sites

I assume it is homework. So, rather than just giving you the answer why don't you explain what you have tried or what you think may be the right approach. We like to see people at least put forth the effort when asking for help with homework.

 

But, I will offer the following. This is more about a math problem than it is a programming problem. You do need to know how to implement the coding to implement the math solution, but you must first understand the math. A very large portion of programming involves mathematics and logic. Just knowing the functions is a small portion of being able to program. So, pull out a sheet of paper and determine a mathematical formula to solve the problem.

Edited by Psycho
Link to comment
Share on other sites

Well it isn't homework, just a simple challenge that I have no idea how to go about.

 

My biggest issue with programming is not the actual the mathematical aspect, but the syntax and purpose. I feel that I could solve this problem and begin to analyze the mathematical portion if I could define each part of the code.

Basically, I am confused about what it is is asking me to do. Which parts define the loop and which part is the itterational value?

 

I appreciate your advice - to elaborate my situation, I am a web developer, proficient with HTML and CSS. I have been becoming more involved with Wordpress and have been doing freelance work creating websites for people. I never got the chance to study PHP in depth to gain an actual understanding of it, so I am simply looking for some advice on where to begin with this challenge.

 

Thank you,

Lara

Edited by laraggg111
Link to comment
Share on other sites

Hmm . . . not homework you say? Then what? Did you just pick some code out of the air and write yourself a requirement to

 

 

Rewrite the code below so it would contain only one loop and only one itterational variable would be used.

? Where exactly did you find the challenge? I'm not accusing you of anything and am happy to help, but I do not appreciate people coming here wanting their homework to be done without actually learning. Or worse, someone trying to get help with an employment evaluation assignment.

 

Again, I would advise figuring out the math first. Look at what is being produced and decide how you would approach it mathematically. You have three loops, each one repeats 0 - 5. The outer loop repeats 0-5 one time. The first inner loop repeats 0-5 six times. And the most inner loop repeats 0-5 36 times (6 for the outer loop time 6 for the first inner loop). So, the outer loop is executed a total of 6 times, the first inner loop 36 times (6 * 6) and the most inner loop 216 times (6 * 6 * 6). That would be your first clues on how to approach this. The other clue I will provide is to use the modulus operator.

 

FYI: I already solved this before I responded to your first post. If you were to actually post some code showing you made an attempt or asked some specific questions I would provide more help. But, as of yet, you haven't showed any effort to trying to solve it yourself. Maybe you have done quite a bit and just haven't shared it. As I said, I won't just provide a solution for something that looks like homework - even if you say it isn't.

 

EDIT: I did some searching and see where this exact problem had been posted other places at least a year ago and there was one search result that linked back to the Careers page for a firm that has PHP developer positions. Again, not accusing, but I don't think you're telling the whole truth. I'm interested in seeing what you come up with.

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.