【问题标题】:Loop and array problem with stopping at one element在一个元素处停止的循环和数组问题
【发布时间】:2021-03-29 20:38:20
【问题描述】:

滚动了解更多详情

我的代码:

     #include<stdio.h>
main()
{
    int T[50], X[50], TEST[50] ,N, i, j, n_indices=0;
    do { printf("Enter size of aray : ");
         scanf("%d", &N);
    } while ( N <= 0 || N > 50);
    
    ////////////////////////////////////////////////
    
    //Filling the array
    printf("\nRemplissage :");
    for ( i = 0 ; i < N ; i++ )
    {
        printf("\n T[%d] = ", i);
        scanf("%d", &T[i]);
    }
    
    ///////////////////////////////////////////////
    
    //Array
    printf("\nArray : ");
    for ( i = 0 ; i < N ; i++ )
    {
        printf("\n T[%d] = %d ", i, T[i]);      
    }
    
    //////////////////////////////////////////////
    printf("\n /// Solution /// : ");
    //Problem part
    
    for( i=0 ; i< N ; i++ )
    {
        TEST[i] = T[i];
    }
    
        
    for ( i = 0 ; i < N ; i++ )
    {   
        for ( j=0 ; j < N ; j++ )
        {
            if ( TEST[i] == TEST[j])
            {
            X[n_indices] = j;
            n_indices++;
            }
        }
                
        //Show results
        for ( j = 0 ; j < n_indices ; j++ )
        {
        printf("\n T[%d] = %d ", X[j], TEST[X[j]]);     
        }
        printf("\n //////////// ");
        n_indices=0;
        
        //Removing T[i]
                
        for ( i=0 , j=0 ; i<N ; i++)
        {
        if (TEST[i] != T[i] )
        {
            TEST[j]=TEST[i];
            j++;
        }
        } 
        
        
            
    }

在最后一个循环中,代码在一个循环后停止。 该程序的目的是显示每个元素在数组中与其对应位置重复的次数。 如果可能的话,我很想知道是什么问题造成的。

输入:

    T[0] = 1
    T[1] = 2
    T[2] = 2
    T[3] = 1
    T[4] = 5
    T[5] = 1

我想要的输出:

    T[0] = 1
    T[3] = 1
    T[5] = 1
    /////////
    T[1] = 2
    T[2] = 2
    /////////
    T[4] = 5
    /////////

我得到了什么:

    T[0] = 1
    T[3] = 1
    T[5] = 1
    /////////
    

【问题讨论】:

  • 请避免需要水平和垂直滚动才能完整阅读的代码。
  • 您在一个内部循环 (for ( i=0 , j=0 ; i&lt;N ; i++)) 中覆盖了外部 i
  • 那么使用另一个变量会解决它吗? @obe
  • 我实际上并没有分析整个代码,我只是专门寻找这样的覆盖。如果其余的都是正确的,那么是的......
  • 即使更改后,T[i] 也没有完全删除

标签: arrays c loops


【解决方案1】:

这个:

for ( i=0 , j=0 ; i<N ; i++)

是这个的子循环:

for ( i = 0 ; i < N ; i++ )

您不能在子循环中更改i 的值,并且合理地期望这样做不会对将其作为索引引用的任何变量的值分配产生不利影响。包括当索引写入不属于您的内存时可能发生的未定义行为。

这里的要点是注意正在使用的索引,不要双倍增加它们。如果需要,选择不同的索引变量在子循环内使用,以避免覆盖外循环中的那些。

编辑您的既定目标是:

“这个程序的目的是显示每个元素在数组中与其对应的位置重复了多少次。”

以下对您的代码的改编是为了说明您如何通过在评估数组之前通过对数组进行排序来简化代码来实现这一目标。

查看 cmets in-line 以解释对代码的这种(广泛)改编的更改。

void sort(int array[], int n);
void swap(int *xp, int *yp);

int main(void)// main() is not a correct signature, use minimum int main(void)
{
    int T[50]={0}, TEST[50]={0} ,N=0, i=0, j=0;//Its a bad habit to leave variables uninitialized
    do { printf("Enter size of array : ");                             //initialize all of them
         scanf("%d", &N);
    } while ( N <= 0 || N > 50);
    
    ////////////////////////////////////////////////
    
    //Filling the array
    printf("\nFilling array :");
    for ( i = 0 ; i < N ; i++ )
    {
        printf("\n T[%d] = ", i);
        scanf("%d", &T[i]);
    }
    
    ///////////////////////////////////////////////
    
    //Array before sort
    printf("\nArray : ");
    for ( i = 0 ; i < N ; i++ )
    {
        printf("\n T[%d] = %d ", i, T[i]);      
    }
    
    //simplify following algorithm by sorting array first
    sort(T, N);
    
    //Array after sort
    printf("\nArray : ");
    for ( i = 0 ; i < N ; i++ )
    {
        printf("\n T[%d] = %d ", i, T[i]);      
    }

    //////////////////////////////////////////////
    printf("\n /// Solution /// : ");
    //Problem part
    
    for( i=0 ; i< N ; i++ )
    {
        TEST[i] = T[i];
    }
    //Now that both TEST and T are arranged in ascending order, 
    //only one loop is needed, and the comparison should work:    
    //for ( i=0 ; i < N-1 ; i++ )//adjust limits from beginning to N-1
    //{   
        for ( j=1 ; j < N ; j++ )
        {
            if ( TEST[j-1] == T[j])//note adjustments in indexes
            {
                printf("\n T[%d] = %d ", j, T[j-1]);
                //X[n_indices] = j;//no longer needed
                //n_indices++;//no longer needed
            }
            else
            {
                //print last element of value
                printf("\n T[%d] = %d ", j-1, T[j-1]);//last element in value group
                printf("\n //////////// ");
            }
        }
        printf("\n T[%d] = %d ", j-1, T[j-1]);//last element in array
    //}
                
 /*   //this section is no longer needed    
        //Show results
        for ( j = 0 ; j < n_indices ; j++ )
        {
            printf("\n T[%d] = %d ", X[j], TEST[X[j]]);     
        }
        printf("\n //////////// ");
        n_indices=0;
        
        //Removing T[i]
                
        for ( i=0 , j=0 ; i<N ; i++)
        {
            if (TEST[i] != T[i] )
            {
                TEST[j]=TEST[i];
                j++;
            }
        } 
        
        
            
    } */
    return 0;//int main(void) requres a return statement
}

void sort(int array[], int n)
{
   int i, j;
   for (i = 0; i < n-1; i++)      
  
       // Last i elements are already in place   
       for (j = 0; j < n-i-1; j++) 
           if (array[j] > array[j+1])
              swap(&array[j], &array[j+1]);
}

void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

【讨论】:

  • 你的意思是这样吗? onlinegdb.com/BkOzMaJrO 因为它仍然只显示第一个元素。很抱歉打扰您
  • 链接中的代码同样会出现双倍递增i的错误。此外,根据我在您帖子下的评论,对上述答案的此编辑说明了预排序数组将大大简化数组的评估,即按元素值分隔部分。
【解决方案2】:

最终代码^^

#include<stdio.h>
main()
{
    int T[50], X[50], TEST[50] ,N, i, j, n_indices=0;
    do { printf("Entrer la taille du tableau : ");
         scanf("%d", &N);
    } while ( N <= 0 || N > 50);

////////////////////////////////////////////////

//Remplissage
printf("\nRemplissage :");
for ( i = 0 ; i < N ; i++ )
{
    printf("\n T[%d] = ", i);
    scanf("%d", &T[i]);
}

///////////////////////////////////////////////

//Affichage
printf("\n Affichage avant traitement : ");
for ( i = 0 ; i < N ; i++ )
{
    printf("\n T[%d] = %d ", i, T[i]);      
}

//////////////////////////////////////////////
printf("\n /// Solution /// : ");
//traitement

for( i=0 ; i< N ; i++ )
{
    TEST[i] = T[i];
}

    
for ( i = 0 ; i < N ; i++ )
{   
    for ( j=0 ; j < N ; j++ )
    {
        if ( TEST[i] == TEST[j])
        {
        if(j >= i)
        {X[n_indices] = j;
        n_indices++;
        }
        else
        break;
        }
    }
            
    //Affichage des resultats
    if(n_indices >0)
    {
    for ( j = 0 ; j < n_indices ; j++ )
    {
    printf("\n T[%d] = %d ", X[j], TEST[X[j]]);     
    }
    printf("\n //////////// ");
    }


    n_indices=0;
        
        
        
        
            
    }
}

【讨论】:

    猜你喜欢
    • 2022-10-21
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2013-04-09
    相关资源
    最近更新 更多