Home > Back-end >  C, array computer exercises, help!!!!!!
C, array computer exercises, help!!!!!!

Time:11-18

#include
using namespace std;
Int func (int a [])
{
for (int i=0; i <16. I++) a [I]=I + 1;
A [16]=0;
Int test=0, the head;
While (the test!=a [test])
{
for (int i=1; i <3; I++)
{
The head=test;
Test=a, [test].
}
A [head]=a, [test].
Test=a, [head].
}
Return the test;
}
Int main ()
{
Int arr [17].
Cout & lt; system("pause"); return 0;
}
Why the result is 10? Want to for a day, didn't want to know, please everyone a great god answer the principle, explain the meaning of several key steps, a little bit more simple, thank you

CodePudding user response:

For once, please answer!!!!!

CodePudding user response:

A []={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,0}
Test=0, a [test]=1
For the first time, while
For the first time
The head=0
Test=1
The second time for
The head=1
Test=2

A [head]=a [test]==& gt; A [1] a [2]==3=
Test=a=[head] a [1]=3

The second while
For the first time
The head=3
Test=4
The second time for
The head=4
Test=5

A [head]=a [test]==& gt; A [4]=a [5]==6
Test=a=[head] a [4]=6

The third time while
For the first time
The head=6
Test=7
The second time for
The head=7
Test=8

A [head]=a [test]==& gt; A [7] a [8]===9
Test=a=[head] a [7]=9

Originally thought to give you a step by step deduction shows that, later found that such deduction is too much, just typed program, look at the way to print the results yourself

 int func (int a []) 
{
for (int i=0; i <16. I++) a [I]=I + 1;
A [16]=0;
Int test=0, the head;
While (the test!=a [test])
{
for (int i=1; i <3; I++)
{
The head=test;
Test=a, [test].
}
A [head]=a, [test].
Test=a, [head].
Printf (" head=% d, test=% d, a [test]=% d \ n ", the head, the test, a [test]);//the way results printing, later encountered a similar problem is not clear, midway through his print the results to know
}
Return the test;
}
Int main ()
{
Int arr [17].
Cout & lt; system("pause"); return 0;
}