2013-10-20 172 views
-1

相同的值需要做哪些计算阵列中的谁具有唯一值功能检查列

作为例子数列的功能: ARRAY

1 2 3 4 
2 2 1 4 

应该算两个独特的列,其是:

1 3 
2 1 

结果应该是:2

这是我得到了多少,但代码不起作用(列出错误的列数),我有点不知道。

int search_ind(int array[row][col], int r, int c,int column) 
{ 
    column=0; 
    int i,j,k; 

    for(j=0;j<c;j++) 
    { 
     for(i=0;i<r;i++) 
     { 
     for(k=i+1;k<r;k++)    
     { 
      if (array[i][j] == array[i+k][j]) 
       {            
       fail=1; 
       break; 
      } 
      else 
       { 
      fail=0; 
      } 

     } 
      if (fail == 1) 
      { 
       break; 
      } 
     } 
     if (fail == 0) 
     { 
      column++; 
     } 

    } 
    printf("With indexes:\nColumn count with unique elements: %d\n\n",column);   
} 
+4

您的缩进真的很奇怪。你从来没有宣布失败,所以这甚至不会编译;第一个暗示是,这不是你尝试解决问题的原因。 –

+0

听起来像作业... – dtech

+0

为什么你通过列作为参数? – DashControl

回答

0

你应该做到以下几点:

Starting at column 0 loop over all columns 
    Save the value of the cell [this column][row: 0] to a variable 
    Starting at row 1 of this column loop through all cells of this column 
     if this cell is not equal to the variable: break this loop 

    We exited above loop without encountering a break. We can increase our matching-columns counter by 1 
We looped through all columns. It's time to return the matching-columns counter. 

顺便说一句:请为您指数更好的名字。 currentColumn,currentLine等而不是i,j,k