2011-07-18 87 views
0

Helo,對於嵌套循環不完美的情況,我對內部循環的定義有些困惑。考慮這個代碼什麼是不完美嵌套循環中的最內層循環?

for (i = 0; i < n; ++i) 
{ 
    for (j = 0; j <= i - 1; ++j) 
     /*some statement*/ 
    p[i] = 1.0/sqrt (x); 
    for (j = i + 1; j < n; ++j) 
    { 
     x = a[i][j]; 
     for (k = 0; k <= i - 1; ++k) 
      /*some statement*/ 
     a[j][i] = x * p[i]; 
    } 
} 

在這裏,我們有兩個循環在相同的嵌套級別。但是,在從j + 1開始迭代「j」的第二個循環中,又有一個嵌套層次。考慮整個循環結構,這是代碼中最內層的循環?

+1

這是mayhap作業嗎?我很好奇你爲什麼問。 –

+0

聽起來像一個功課問題?爲什麼你需要知道哪個是最內圈?我想你已經通過引用嵌套層次來回答你自己的問題。 – skyfoot

+0

我被困在我的論文中,在那裏我需要模擬一些循環..而且這一次我看到完美的嵌套循環... – psteelk

回答

0

大聲笑我不知道如何解釋這個,所以我會給它我最好的拍攝我推薦使用debugger!它可能會幫助你這麼多,你甚至不知道

for (i = 0; i < n; ++i) 
{ 
    //Goes in here first.. i = 0.. 
    for (j = 0; j <= i - 1; ++j) { 
     //Goes here second.. 
     //Goes inside here and gets stuck until j is greater then (i- 1) (right now i = 0) 
     //So (i-1) = -1 so it does this only once. 
     /*some statement*/ 
    p[i] = 1.0/sqrt (x); 
    } 
    for (j = i + 1; j < n; ++j) 
    { 
     //Goes sixth here.. etc.. .. 
     //when this is done.. goes to loop for (i = 0; i < n; ++i) 

     //Goes here third and gets stuck 
     //j = i which is 0 + 1.. so, j == 1 
     //keeps looping inside this loop until j is greater then n.. idk what is n.. 
     //Can stay here until it hits n.. which could be a while. 
     x = a[i][j]; 
     for (k = 0; k <= i - 1; ++k) { 
      //Goes in here fourth until k > (i-1).. i is still 0.. 
      //So (i-1) = -1 so it does this only once 
      /*some statement*/ 
     a[j][i] = x * p[i]; 
     } 
     //Goes here fifth.. which goes.... to this same loop! 
    } 
} 
2

兩個j環路嵌套在i同樣,k是最內側的循環

0

我會說,k是最內部的循環,因爲如果算上環路從到達它所需的數在外面,它是三個循環,這是代碼中所有四個循環中最重要的部分。