停在一个元素上的循环和数组问题 [英] Loop and array problem with stopping at one element

查看:79
本文介绍了停在一个元素上的循环和数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

滚动以获取更多详细信息

Scroll for more details

我的代码:

     #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++;
        }
        } 
        
        
            
    }

在最后一个循环中,代码在一个循环后停止.该程序的目的是显示每个元素在与其对应位置的数组中重复多少次.如果可能的话,我想知道是什么原因引起的.

In the last loop the code stops after one loop. The purpose of this program is to show how many times each element is repeated in the array with its correspondant position. If possible i would love to know what problem caused this.

输入:

    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
    /////////
    

推荐答案

最终代码^^

#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;
        
        
        
        
            
    }
}

这篇关于停在一个元素上的循环和数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆