Username: Password:
Forum / Help / Programming / C++ /
Page [1]
heartsriker
Posted: 21-02-11' (11 years ago)
Post #0001


-Stats-
Rank: Member
Profession: Programmer
Level: 1
Posts: 7 (0% Spam)
XP: 14

-Medals-
No awards

So i've been reading this book.... and the target is to display a tic tac toe at command line like this

X O X
X X O
X O O

Apparently you can do this though vectors (2-d)

And through a nested For loop

So i want to ask. How do you make it with nested for loops? and how does it work?


supperbas
Posted: 22-02-11' (11 years ago)
Post #0002


-Stats-
Rank: Founder
Profession: Programmer
Level: 5
Posts: 538 (1% Spam)
XP: 1264

-Medals-
Crew
Founder
V1 Member
V2 Member


Well a nested for loop is rather simple

Regular for loop in PHP
Code:
for($i=0;$i<10;$i++)
{
echo $i."<br />";
}


This will out put

Code:
1
2
3
4
5
6
7
8
9


I'm pretty sure you're familiar with it.
So 'for I equals to zero and while I is less than 10 do what's between brackets and after each run increment I with one'
that's basically what for($i=0;$i<10;$i++) means

You could also rewrite this as a while loop and you would get this

Code:
$i=0;

while($i<10)
{
echo $i."<br />";
$i++;
}


Note this will do the exact same thing as the first one.

Okay to get back to your question what is a nested for loop?
Well a nested for loop is basically two for loops and one of the for loops is 'nested' inside the other one

E.g. you want to use a nested for loop to count from 0 until 100
Code:
for($i=0;$i<10;$i++)
{

for($j=0;$j<10;$j++)
{

echo $i.$j."<br />";

}

}


So what will this do?
Well first I becomes zero, then it will go into for loop two
Then J will become zero as well and it will print both numbers, then J is incremented and becomes one, but I remains one as we're instill looping in the second loop.

After J has become '10' the condition of the second loop will become false and it will 'move on' so it will increment I and then it will rerun the first loop but with I equals to one so we go to the second loop again J equals to zero and loop through that one with I remaining one.
So you get 10, 11, 12, 13 etc.
and it will loop until you get 99 then the inner loop's condition will become false and so will the outer loop (the 'first' loop) and then basically the program moves on.

Of course this program isn't very efficient but it does give you a clear idea how the nested loop works.

So to conclude this with the nested for loops the first loop will only run for 10 times but the inner loop (the nested one) will run for 10*10 times so 100 times. (10* from 0-9)

For C++ you just have to remove the dollar sign and change echo into printf with %d for the integers (if I remember correctly) and it should work!

Hope this is any help and if you don't get it working in C++ let me know and I will provide it for you!


--------------------
SupperBas[dot]Com
- site of power -
heartsriker
Posted: 24-02-11' (11 years ago)
Post #0003


-Stats-
Rank: Member
Profession: Programmer
Level: 1
Posts: 7 (0% Spam)
XP: 14

-Medals-
No awards

Yeah, i know how a nested for loop work....


but what if i want to use a nested for loop to make a 2d gird on command line?

X O X
X X O
X O O

So far you only have been explaining one dimensional, linear numbers like

1
2
3
4
5
6


What if i wanted something like that tic-tac-toe thing?

And could you explain how it works for 2dimensional stuff?


localhost
Posted: 24-02-11' (11 years ago)
Post #0004


-Stats-
Rank: Member
Profession: Programmer
Level: 1
Posts: 2 (0% Spam)
XP: 4

-Medals-
No awards

@heartsriker

I don't really understand what your trying to do but if your trying to achieve this in PHP I can help... but whats with the 'tic tac toe' stuff - if your trying to create some sort of game (as after googling 'tic tac toe' thats what it looks like) you'd need JavaScript not PHP (as you'll find PHP to be limiting in that sense) - I'm sure if you google their should be some opensource code to get you started.


heartsriker
Posted: 25-02-11' (11 years ago)
Post #0005


-Stats-
Rank: Member
Profession: Programmer
Level: 1
Posts: 7 (0% Spam)
XP: 14

-Medals-
No awards

I'm not trying to make a tic tac toe game, i'm merely trying to DISPLAY one at commandline.

Supperbas talked about making linear characters getting displayed, but what about two dimensional? This doesn't have to be restricted to Xs and Os either. even

1 2 3
2 3 4
3 4 5
4 5 6

or even

A B C
B C A
C A B

How do you do you display these column/rows 2d vectors with NESTED FOR LOOPS, and mind explaining how it works for that?


supperbas
Posted: 25-02-11' (11 years ago)
Post #0006


-Stats-
Rank: Founder
Profession: Programmer
Level: 5
Posts: 538 (1% Spam)
XP: 1264

-Medals-
Crew
Founder
V1 Member
V2 Member


Okay lets say you've a so called '3x3 matrix' (Three by three matrix)

Code:
A B C
D E F
G H I


You create this by using a so-called 2-dimensional array

in PHP you could do it like this

Code:
$array[0][0]="A"; // note an array index starts at zero!
$array[0][1]="B"; // most of the time the first [] is the row and the second the column
$array[0][2]="C";
$array[1][0]="D";
$array[1][1]="E";
// etc. until
$array[2][2]="I";


In your case I would use the numbers 1 and 0 to denote X or O (you could use "X" and "O" the characters but it might be easier to use numbers)

so let's say you want to make this tic tac toe result
Code:
X O X
X X O
X O O


Code:
// we assume that $input is an array with 9 values either 1 or 0
$input = array(1,0,1,1,1,0,1,0,0); // 1 is X, 0 is O
$array = array(); // our 2d tic tac toe result
$count = 0; // you can take away the counter but it makes it simpler if we put it in

for($i=0;$i<3;$i++)
for($j=0;$j<3;$j++)
$array[$i][$j]=$input[$count];

// lets say you want to echo this (note I haven't tried this so it might through an error if I forgot a semi column
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
echo ($array[$i][$j] ? "X" : "O")." "; // note it checks if its 1 if so echo X otherwise echo O
echo "<br />"; // and a break line after every row
}


And that's basically how you do it. Instead of a variable '$input' you of course take the real input that you gave via the command line or from an other source.

Note: You need to declare arrays in C first!
Code:
int array[3][3]


--------------------
SupperBas[dot]Com
- site of power -
heartsriker
Posted: 02-03-11' (11 years ago)
Post #0007


-Stats-
Rank: Member
Profession: Programmer
Level: 1
Posts: 7 (0% Spam)
XP: 14

-Medals-
No awards

I wanna ask....why php? Isn't that a different language to c++?


supperbas
Posted: 05-03-11' (11 years ago)
Post #0008


-Stats-
Rank: Founder
Profession: Programmer
Level: 5
Posts: 538 (1% Spam)
XP: 1264

-Medals-
Crew
Founder
V1 Member
V2 Member


Yes it is, but I think I said so as well in my first reply
I mainly use PHP (and some other languages but lets stick to PHP) but I almost never use C++ so I can do two things

Do it in PHP which costs me almost no time and I know the syntax etc. or look up all specifics of C++ make sure I get the syntax right and all the 'limitations'/'strict' behavior of the language because it is more picky than PHP.
First one would only take me a matter of minutes while the second one will probably force me to look up every line to see if things are different in C++

So what I did is I gave you the 'basic' idea and if you're familiar in C++ you should get the strategy you need and you can simply implement it yourself.

So PHP because I didn't want to look up all the specifics of C++ and then you might end up with code that wouldn't (directly) work if you put it in a C++ compiler.


--------------------
SupperBas[dot]Com
- site of power -
Page [1]
Forum / Help / Programming / C++ /
SupperBas[dot]com - site of power - SB
There are currently 2 visitors online