简体   繁体   English

在c中的数组中找到'X'数字的部分和

[英]Find partial sum of 'X' numbers in array in c

can you help me with code which returns partial sum of 'X' numbers in array in c? 你能用在C中的数组中返回“ X”数字的部分和的代码帮助我吗?

Complete : 完成:

    int arr_sum( int arr[], int n )//Recursive sum of array
     { 
      if (n < 0) //base case:
      {

        return arr[0];
      }
      else
      {
        return arr[n] + arr_sum(arr, n-1);
      }
    }
    void sum_till_last (int *ar,int si )
    {

        **int sum,i;// the problem some where here
        ar=(int*) malloc(si*sizeof(int));

        for (i=0; i<si;i++)
        {

            sum=arr_sum(ar,i);
            ar [i]=sum;
        }
        free (ar);**
    }


 void main ()
{
    int i;
    int a [5];
    for (i = 0; i < 5; i++)
      scanf_s("%d", &a[i]);

    sum_till_last(a,5);
    printf("%d\n",a[5]);
}

\\i want to create new array with this this legality: My input : \\ i要创建具有此合法性的新数组:我的输入:

4 13 23 21 11 4 13 23 21 11

The output should be (without brackets or commas): 输出应为(不带括号或逗号):

4 17 40 61 72 4 17 40 61 72

Now when we can see the full code, it's quite obvious that the problem is in the sum_till_last function where you overwrite the pointer you pass to the function with some new and uninitialized memory you allocate. 现在,当我们看到完整的代码时,很明显问题出在sum_till_last函数中,在该函数中,您使用分配的一些新的未初始化的内存覆盖了传递给该函数的指针。

Drop the allocation (and the call to free of course). 删除分配(当然也要free )。 And fix the logical bug in arr_sum that causes you to get arr[0] + arr[0] when i is zero. 并修复arr_sum中的逻辑错误,当i为零时,该错误会导致您获得arr[0] + arr[0]

Here you go: 干得好:

#include <stdio.h>

int main () {
   int in_arr[5] = {4,13,23,21,11};
   int out_arr[5];
   int p_sum =0;
   int i;

   for ( i=0;i<5;i++){
      out_arr[i] = in_arr[i]+p_sum;
      p_sum=p_sum+in_arr[i];
   }

   for (i=0;i<5;i++){
      printf("%d", out_arr[i] );
   }  
}

I just made it simple so it´s easy to understand :) I´m assuming "n" is always equal or less then array element number. 我只是简单了一下,所以很容易理解:)我假设“ n”总是等于或小于数组元素号。 Then you just print the SUM. 然后,您只需打印SUM。

  #include  <stdio.h>

     int arr_sum( int arr[], int n ){
         int i=0,SUM=0;   
         for(; i < n;i++){
           SUM= SUM+ arr[i];
           printf("%d ",SUM);
         }  
    }

     int main(void) {
         int array[] = {4, 13, 23, 21, 11};
         arr_sum(array,5);
         return 0;
     }

Fix according to your policy 根据您的政策进行修复

#include <stdio.h>
#include <stdlib.h>

int arr_sum(int arr[], int n){
    if (n == 0){//Change to this
        return arr[0];
    } else {
        return arr[n] + arr_sum(arr, n-1);
    }
}

void sum_till_last(int *ar, int si){
    int sum,i;
    int *temp = malloc(si * sizeof(int));//variable name ar is shadowing parameter name ar.

    for(i = 0; i < si; i++){
        temp[i] = arr_sum(ar, i);
        if(i)
            putchar(' ');
        printf("%d", temp[i]);//need print out :D
    }
    free(temp);
}

int main(void){
    int i, a[5];

    for (i = 0; i < 5; i++)
        scanf_s("%d", &a[i]);

    sum_till_last(a, 5);
    //printf("%d\n",a[5]);<-- this print only one element. and It's out of bounds element XD 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM