Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NIM game

Status
Not open for further replies.

towjam

Programmer
Jun 14, 2006
4
CA
Can someone explain to me in great detail what is goin on in this loop.It's pretty simple but i can't seem to fully understand the bottom half(the second loop).




Basiclly this function will display this pattern (which is used when playing NIM) :-
|
| |
| | |
| | | |
| | | | |



Also the variable "a" just point at an array , which is :-

int row[6] = {0, 1, 2, 3, 4, 5};





Example -- when i call this function , i would go like this

display (row)

void display(int* a)
{
cout << "|-----------------------------|\n";
for(int i = 0; i < 6; i++)
{
if(i==1) cout << " ";
if(i==2) cout << " ";
if(i==3) cout << " ";
if(i==4) cout << " ";
if(i==5) cout << " ";

for(int j =1; j <= *a; j++)
cout << " |";
a++;
cout << "\n\n";
}
cout << "\n|-----------------------------|\n";
}


So if someone understands this please reply
thanks in advance
 
If I understand correctly...
You have two loops nested
First loop counts rows
"if" things puts spaces in the beginning of a row
so you would get
" | "
"| |"
instead of just
"| "
"| |"
second loop - inside one - prints " |" several times, there "several" is taken from array a.
(that is all this loop do - one line).
so *a is current array item (starting from the 0th )
And "a++" instruction advances to the next item.

Hope this helps.
 
It really helps to read the code if you put the [ignore]
Code:
[/ignore] tags around it.

I hope I'm understanding your question:

What is happening in your second loop (and just after) is pointer dereferencing and classic pointer arithmetic.

As you know, a pointer points to some area in memory. In this case the pointer a points to the memory that stores the integer array row. Dereferencing a pointer (*a) simply means getting the value that the pointer points to.

The first time through the loop, the pointer a points to the beginning of the array row. *a means the data at the memory location pointed to by pointer a, so the first time through the loop, *a holds the value 0 (which simply results in two blank lines printing). You then increment a (a++ which is your pointer arithmetic) and a now points to the 2nd array element and *a now gives the value 1. This process continues through the entire array with *a next giving the value 2 then 3 then 4 then 5.

So stepping through the loop:
[tt]
{0,1,2,3,4,5}
|
a [red]*a == 0, loop evaluates to: for (j=1; j <= 0; j++)[/red]

now a++
{0,1,2,3,4,5}
|
a [red]*a == 1, loop evaluates to: for (j=1; j <= 1; j++)[/red]

now a++
{0,1,2,3,4,5}
|
a [red]*a == 2, loop evaluates to: for (j=1; j <= 2; j++)[/red]

and so on...
[/tt]

Hope that answered your question.
 
tsh73, looks like we were drafting answers at the same time. towjam, yes, it's just like tsh73 said.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top