2013-03-07 40 views
1

我遇到了一些我正在編寫的代碼的問題。 我經常使用這個網站,因爲我發現很多人已經問過我想知道的相同問題。有了這個,我想感謝這裏的社區瞭解以前對於我的編程難題的所有見解。 (在我們太過分了之前,這不是一個'學校項目'或'學校作業',我只是試圖解決'旅行推銷員問題'和更好的C技能。的代碼,我一直停留在:傳遞2d數組,然後沒有得到值

void printAndFlip(int arrayone[][20], int citytotal, int arrayCities[]) 
    { 

////Finds cost: 
int x, y, z; 
int totalCost 
int singleTrip; 
int cheepestTrip; 
int nCity = citytotal + 1;  //nCity is the number of Cities //Adding one to accomadate going back to the first city 
int gCounter; 
int gCounterTrue = 1; 
int cheepestTrip[20]; 
int totalCost = 0; 
int lCounter; 
int i; 
int n = citytotal; 


////Sets up for a default case to set cheepestTrip: 
for(gCounter = 1; gCounter <= nCity; gCounter++) 
{ 
    while(gCounterTrue == 1) 
    { 
     if(gCounter == arrayCities[gCounter]) 
     { 
      gCounterTrue = 1; 
     } 
     else 
     { 
      gCounterTrue = 0; 
      gCounter = 50;  //Stopping the larger for loop with 50 (the nCity can't be larger than 20) so that it will hopefully be faster 
     } 
     if(gCounter == nCity) 
     { 
      if(arrayCities[1] == arrayCities[nCity]) 
      { 
!!!!!    cheepestTrip = totalCost; 
      } 
     } 
    } 
} 
for(x = 1; x < nCity; x++) 
{ 
    y = arrayCities[x]; 
    z = arrayCities[x+1]; 
    singleTrip = arrayone[y][z];  //finding individual cost of each trip...will be added to 'totalCost' below 
    totalCost = singleTrip + totalCost; 
} 

!!!!!!!! if(totalCost <= cheepestTrip) 
{ 
    for(lCounter = 1; lCounter <= nCity; lCounter++) 
    { 
     cheepestTrip[lCounter] = arrayCities[lCounter]; 
    } 
} 

爲了更容易展現在我的編譯錯誤都在我把感嘆號上線 請告訴我,如果我錯了,但我傳遞一個數組。當我發送'arrayone'printOFFlip對嗎?我知道編譯錯誤與指針有關,但我只是不確定它們應該放在哪裏。 任何和所有的幫助將不勝感激。 許多感謝, 亞歷

+0

我已經爲初學程序員提出了一個問答網站。應該以積極的態度來滿足簡單的問題,並鼓勵人們學習成爲更好的程序員。如果你喜歡這個主意,請按照提案http://area51.stackexchange.com/proposals/52242/beginner-programmers?referrer=YHFcRobXPDGfDpFmz1HCvA2 – AxelOmega 2013-03-07 19:45:56

回答

1

爲了明確其他答案的內容:您有兩個具有相同名稱但類型不同的變量:

int cheepestTrip; /* This is an single integer... */ 

int cheepestTrip[20]; /* ...but this is an array of integers. */ 

這應該在編譯時觸發報警(可能是一些關於重新聲明現有的變量)。

+0

哦,我的天哪!我花了三個小時試圖弄清楚這一點!我不敢相信這就是這麼簡單!感謝大家的幫助! – AbsoluteZ3r0 2013-03-08 14:43:16

1

在這裏,你用一個int值

if(totalCost <= cheepestTrip) 

比較陣列指針例如,你應該比較它該數組的元素

if(totalCost <= cheepestTrip[0]) 
1

cheepestTrip是數組的名稱,相當於指向第一個元素的指針。 totalCostint。只需從代碼頂部的聲明中刪除[20]即可。

0

你正在比較一個int指針,你的特定編譯器可能不允許(雖然我用C可以)。但cheapestTrip本質上是指向您的整數數組中第一個元素的指針,而totalcost僅僅是一個int初始值