C 如何在已修改的函数外部打印数组值 void排序(int值[],int n); void将所有数组值修改为0(int值[],int数组大小); 内部主(空) { 国际[]={4,2,56,2,1,7,20,9,3,5,6,4,3,2,44,57}; int size_of_aray=sizeof(aray)/sizeof(aray[0]); 排序(aray、aray的大小); printf(“\n”); } 无效排序(int值[],int n) { //TODO:实现一个排序算法 int临时; //假设临时数组大小不超过1000 int临时_数组[1000]; int size_of_tmp_数组=sizeof(临时_数组)/sizeof(临时_数组[0]); //将临时数组中的所有值设置为0 将所有数组值修改为0(临时数组、tmp数组的大小); 对于(int i=0;i

C 如何在已修改的函数外部打印数组值 void排序(int值[],int n); void将所有数组值修改为0(int值[],int数组大小); 内部主(空) { 国际[]={4,2,56,2,1,7,20,9,3,5,6,4,3,2,44,57}; int size_of_aray=sizeof(aray)/sizeof(aray[0]); 排序(aray、aray的大小); printf(“\n”); } 无效排序(int值[],int n) { //TODO:实现一个排序算法 int临时; //假设临时数组大小不超过1000 int临时_数组[1000]; int size_of_tmp_数组=sizeof(临时_数组)/sizeof(临时_数组[0]); //将临时数组中的所有值设置为0 将所有数组值修改为0(临时数组、tmp数组的大小); 对于(int i=0;i,c,arrays,function,C,Arrays,Function,请帮我解决这个问题。 我以前发布过这个问题,但它不好理解,所以我决定删除这个问题并研究用户的评论,只是为了从我的错误中吸取教训,以便我能够提出我们能够理解的问题 我真的练习过代码格式,感谢上帝,现在它比我之前发布的帖子要好。我能够发现你的代码有很多问题。首先,你一定要读这本书。给你几点建议: 不要在代码中添加太多注释,这会让人难以理解 使用大家都知道的概念的名字,这样别人就很容易理解你在做什么。就像这里,从外观上看,您正在尝试实现 至于代码,您面临的主要错误是由于此代码块引起的 void sor

请帮我解决这个问题。 我以前发布过这个问题,但它不好理解,所以我决定删除这个问题并研究用户的评论,只是为了从我的错误中吸取教训,以便我能够提出我们能够理解的问题


我真的练习过代码格式,感谢上帝,现在它比我之前发布的帖子要好。

我能够发现你的代码有很多问题。首先,你一定要读这本书。给你几点建议:

  • 不要在代码中添加太多注释,这会让人难以理解
  • 使用大家都知道的概念的名字,这样别人就很容易理解你在做什么。就像这里,从外观上看,您正在尝试实现
  • 至于代码,您面临的主要错误是由于此代码块引起的

    void sort(int values[], int n);  
    
    void modify_all_aray_values_to_0(int value[], int array_size);
    
    int main(void)
    {
        int aray[] = {4,2,56,2,1,7,20,9,3,5,6,4,3,2,44,57};
        int size_of_aray = sizeof(aray)/sizeof(aray[0]);
        sort(aray, size_of_aray);
        printf("\n");
    }
    
    void sort(int values[], int n)
    {
        // TODO: implement a sorting algorithm
        int temporary;
        //assumes temporary array size will not exceed 1000
        int temporary_array[1000];
        int size_of_tmp_array = sizeof(temporary_array)/sizeof(temporary_array[0]);
        //set all values in temporary array to 0
        modify_all_aray_values_to_0(temporary_array, size_of_tmp_array);
        for(int i = 0; i < n; i++)
        {
            //store the values of values[] i'th in temporary variable
            //at this stage, temporary = 4, because first element in values[] is 4
            temporary = values[i];
            //store result of temporary_array[4]+1 in temporary_array[4]
            //at this stage temporary_array[4],s values is 0
            //when i assigned it, it gets the value (0) and increament it with 1
            //this means so far i have seen value 4 once in values[]
            /*so if this loops again and it found value 4, it will
            modify and increament temporary_array value to 2*/
            temporary_array[temporary] = temporary_array[temporary]+1;
            //so at the end of the loop
            //temporary_array[1] = 1
            //temporary_array[2] = 3
            //temporary_array[3] = 2
            //temporary_array[4] = 2 and so on...
        }
        int tu;
        //print all values in values[] before it get modified
        for(int i = 0; i < n; i++)
        {
            printf("%i-", values[i]);
        }
        printf("\n");
        //assumes it wont loop more than 100 times
        for(int i = 0; i < 100; i++)
        {
            if(temporary_array[i] > 0)
            {
                for(tu = 0; 0 < temporary_array[i]; tu++ )
                {
                    //modify valeus[] by assigning i'th value
                    //at this stage if when temporary_array[1] is greater than 0
                    //store 1 in values[0]
                    values[tu] = i;
                    //immediately decrease temporary_array[1] by 1
                    //now temporary_array[1] is now 0
                    //next time the loop will check if its greater than 0
                    //if so it will modify values[] and store the value
                    //else it wont bother and that means 1 doesnt appear in array again
                    temporary_array[i] = temporary_array[i]-1;
                    //print values[0] inside loop after modification
                    printf("%i,", values[tu]);
                    //printed 1
                    //will print 1,2,2,2,3,3... and so on after compilation
                }
            }
        }
        printf("\n");
        for(int i = 0; i < n; i++)
        {
            //print values[0] outside the previous loop
            printf("%i-", values[i]);
            //this time around it printed 57 instead of 1
            //prints 57-4-2-2-1-7-20... and so on
            //what could have happend?
        }
        printf("\n");
        return;
    }
    
    void modify_all_aray_values_to_0(int value[], int array_size)
    {
        for(int i = 0; i < array_size; i++)
        {
            value[i] = 0;
        }
    }
    

    请仔细阅读并特别说明。当你问一个问题时,试着告诉我们你做了什么,哪里出了问题,你期望发生什么,等等。你有没有理由让我们
    把所有的值都修改为
    ,而不是
    memset
    ?我不熟悉(memset)没有任何东西阻止你使用谷歌并查找它。这个代码太吵了。你认为应该发生的没有发生的具体情况是什么?感谢这一工作
    (inti=0;i<100;i++)if(temporary_array[i]>0)//如果(;0for(int i = 0; i < 100; i++)
        if(temporary_array[i] > 0)//If the count of a number is > 0
            for(tu = 0; 0 < temporary_array[i]; tu++ ){ //<- ERROR HERE 
                values[tu] = i;
                temporary_array[i] = temporary_array[i]-1;
                printf("%i,", values[tu]);
            }
    
    #include <stdio.h>
    
    void sort(int values[], int n);  
    
    void modify_all_aray_values_to_0(int value[], int array_size);
    
    int main(void){
        int aray[] = {4,2,56,2,1,7,20,9,3,5,6,4,3,2,44,57};
        int size_of_aray = sizeof(aray)/sizeof(aray[0]);
        sort(aray, size_of_aray);
    }
    
    void sort(int values[], int n){
        int temporary;
    
        int temporary_array[1000];
        int size_of_tmp_array = sizeof(temporary_array)/sizeof(temporary_array[0]);
    
        modify_all_aray_values_to_0(temporary_array, size_of_tmp_array);
        for(int i = 0; i < n; i++){
            temporary = values[i];
            temporary_array[temporary] = temporary_array[temporary]+1;
        }
        int tu;
    
        printf("Before getting modified: ");
        for(int i = 0; i < n; i++)
            printf("%i ", values[i]);
    
        printf("\n\n");
    
    
        printf("temporary_array: ");
        for(int i = 0; i < size_of_tmp_array; i++){
            if(temporary_array[i]>0) printf("%d-%i ", i,temporary_array[i]);
        }
        printf("\n\n");
    
        for(int i = 0; i < 100; i++)
            if(temporary_array[i] > 0)//If the count of a number is > 0
                for(tu = 0; 0 < temporary_array[i]; tu++ ){
                    values[tu] = i;
                    temporary_array[i] = temporary_array[i]-1;
                    printf("%i,", values[tu]);
                }
    
        printf("\n\nShould be 1 2 2 2 3 3 4 4 5 6 7 9 20 44 56 57");
        printf("\n\nAfter Sorting: ");
        for(int i = 0; i < n; i++)
            printf("%i ", values[i]);
    
        printf("\n");
        return;
    }
    
    void modify_all_aray_values_to_0(int value[], int array_size){
        for(int i = 0; i < array_size; i++) value[i] = 0;
    }