注册 登录
编程论坛 C语言论坛

数组累加

zzmx 发布于 2021-09-15 17:42, 1763 次点击
想用一个数组b[n]表示a[n]数组的累加值,比如
b[0]=a[0];
b[1]=a[0]+a[1];
b[2]=a[0]+a[1]+a[2];
...
b[n]=a[0]+...a[n];
请问这个用C语言的程序该怎么实现呢?
2 回复
#2
自由而无用2021-09-15 17:59
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>
#include <string.h>

int plus_v(char *v, int idx)
{
    if(0 == idx) return v[idx];
   
    return v[idx] + plus_v(v, idx - 1);
}

int main(int argc, char *argv[])
{
    char a[100];
    int b[100];
    int i;
   
    memcpy(a, main, 100);
   
    for(i = 0; i < sizeof(a); i++) {
        b[i] = plus_v(a, i);
        printf("b[%02d] = %d\n", i, b[i]);
    }
   
    return 0;
}
#3
自由而无用2021-09-15 20:58
//online parser: https://www.bccn.net/run/
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE_A 5
#define PRINT_ON

int main(int argc, char *argv[]);

static int cs;

void init_A(char *v)
{
    int i;
   
    memcpy(v, main, SIZE_A);
#ifdef PRINT_ON
    for(i = 1; i <= SIZE_A; i++) {
        printf("a[%d] = %d\t", i - 1, v[i - 1]);
        if(0 == i % 5) puts("");
    }
#endif
}

int plus_v(char *v, int idx)
{
    if(0 == idx) {
#ifdef PRINT_ON
        printf("call stack[%d], return a[0]-----", cs++);
#endif
        return v[idx];
    } else {
#ifdef PRINT_ON
        printf("call stack[%d]\n", cs++);
        printf("return a[%d] + plus_v(a, %d),\n", idx, idx - 1);
#endif
    }
    return v[idx] + plus_v(v, idx - 1);
}

void gcc_make(void)
{
    //system("rm -r *;ls");
   
//system("gcc -D PRINT_ON *.c -o v.out");
}

int main(int argc, char *argv[])
{

    char a[SIZE_A];
    int b[SIZE_A];
    int i;

    init_A(a);
   
    for(i = 0; i < sizeof(a); i++) {
        b[i] = plus_v(a, i);
#ifdef PRINT_ON
        printf("b[%02d] = %d\n", i, (cs = 0, b[i]));
        puts("-----next-----");
#endif
    }
   
    gcc_make();
   
    return 0;
}
#4
自由而无用2021-09-15 21:00
output sample:
a[0] = 85    a[1] = 72    a[2] = -119    a[3] = -27    a[4] = 72   
call stack[0], return a[0]-----b[00] = 85
-----next-----
call stack[0]
return a[1] + plus_v(a, 0),
call stack[1], return a[0]-----b[01] = 157
-----next-----
call stack[0]
return a[2] + plus_v(a, 1),
call stack[1]
return a[1] + plus_v(a, 0),
call stack[2], return a[0]-----b[02] = 38
-----next-----
call stack[0]
return a[3] + plus_v(a, 2),
call stack[1]
return a[2] + plus_v(a, 1),
call stack[2]
return a[1] + plus_v(a, 0),
call stack[3], return a[0]-----b[03] = 11
-----next-----
call stack[0]
return a[4] + plus_v(a, 3),
call stack[1]
return a[3] + plus_v(a, 2),
call stack[2]
return a[2] + plus_v(a, 1),
call stack[3]
return a[1] + plus_v(a, 0),
call stack[4], return a[0]-----b[04] = 83
-----next-----
#5
rjsp2021-09-16 09:19
程序代码:
#include <stdio.h>

void std_partial_sum( const int a[restrict], int b[restrict], size_t n )
{
    if( n == 0 )
        return;

    b[0] = a[0];
    for( size_t i=1; i!=n; ++i )
        b[i] = b[i-1] + a[i];
}

int main( void )
{
    int a[] = { 1, 2, 3, 5, 7, 9, 11 };

    enum { LEN=sizeof(a)/sizeof(*a) };
    int b[LEN]; // 期待结果是 { 1, 3, 6, 11, 18, 27, 38 }
    std_partial_sum( a, b, LEN );

    for( size_t i=0; i!=LEN; ++i )
        printf( "%d\n", b[i] );
}
#6
我善治鬼2021-09-17 13:02
有那么复杂吗

程序代码:


#include <stdio.h>

#define N 100

int main()
{
    int a[N], b[N], c = 0;
    for (int i = 0; i < N; i++) {
        printf("a[%d] = %d\t", i, a[i] = i);
        printf("b[%d] = %d\n", i, b[i] = (c += a[i]));
    }
    return 0;
}


1