Question related to for loop

I’m making a grid of Row=5 and Col = 8. Using this gird to instantiate. So actually 40 GameObjects are being Instantiated. But I want to to print 0 to 40 in a separate for loop. I have created the for loop in Start()

I’m not supposed to use any variables, add, subtract, multiply or divide do anything thin but just by using i and j. Getting me? Not to use Row and Col variables too in the statement.

Row = 5, Col = 8;
for(int i=0;i<Row;i++)
{
    for(int j=0;j<Col;j++)
    {
        //Print 0 to 40, just by using i and j, don't use any variables.
    }
}

What is the question?

I'm making a grid of Row=5 and Col = 8. Using this gird to instantiate. So actually 40 GameObjects are being Instantiated. But I want to to print 0 to 40 in a separate for loop. I have created the for loop in Start().

I'm not supposed to use any variables, add, subtract, multiply or divide do anything thin but just by using i and j. Getting me? Not to use Row and Col variables too in the statement.

0 to 40 are 41 numbers.

sorry 0 to 39 numbers.

1 Answer

1

As it was pointed out in the comment, your question is lacking the question. I assume your comment reply is your actual question?

I’m making a grid of Row=5 and Col = 8. Using this gird to instantiate. So actually 40 GameObjects are being Instantiated. But I want to to print 0 to 40 in a separate for loop. I have created the for loop in Start().

First of all, please format code as code. The editor on this site has a button for preformatted code </> button. Just insert the code between the 3 backticks

    ```
    your code goes here
    ```

So it would look like this:

Row = 5, Col = 8;
for(int i=0;i<Row;i++)
{
    for(int j=0;j<Col;j++)
    {
        //Print 0 to 40, just by using i and j, don’t use any variables.
    }
}

You can also write “CSharp” after the 3 opening backticks to get proper C# syntax highlighting:

Row = 5, Col = 8;
for(int i=0;i<Row;i++)
{
    for(int j=0;j<Col;j++)
    {
        //Print 0 to 40, just by using i and j, don’t use any variables.
    }
}

SOLUTION

Anyways, in order to print the numbers 0 to 39 you would just do (i*Col + j)

Debug.Log(""+i+" * "+Col+" + "+j+" == " + (i*Col + j));

So for every completed row you have to add one full length of that row (so Col).

ps: I just read that you want to print the number in a separate loop? That sounds kinda weird, especially since you said no extra variable. That second loop would require it’s own loop variable in order to be able to loop and have a breaking condition. So I’m confused what you actually want. Is this an assignment? We’re not here to help with homework ^^. This is about game development.

Yes this way it works. But it is very much possible to do this using only i and j but I don't know how.

Sorry but that doesn't make any sense. You now wrote in your question that you can not use addition, subtraction or multiply, just i and j. This makes no sense at all. Instead of Col you could use the value of Col directly. However that would be the worst coding style ever. You can not produce a certain number that only has a mathematical relationship with i and j and not using any math. You either don't know what you're actually asking, or you don't really know what you want.