2013-03-07 39 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初始值