Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Liquid Web Cloud VPS Hosting

10,903 questions

14,810 answers

573 users

How to implement a cumulative sum of numbers in int array in C

1 Answer

0 votes
#include <stdio.h>

void accumulate(int arr[], int len) { 
    int sum = 0;
    for (int i = 0; i < len; i++) {
        sum += i;
        printf("%3d", sum);
	}
	printf("\n");
} 
    
int main() 
{ 
    // 0 : 0+1=1 : 0+1+2=3 : 0+1+2+3=6 : 0+1+2+3+4=10 : 0+1+2+3+4+5=15 ...
    int arr[] = {0, 1, 2, 3, 4, 5, 6}; 
    int len = sizeof(arr) / sizeof(arr[0]); 
      
    accumulate(arr, len); 
      
    return 0; 
} 
  
  
  
/*
run:
  
  0  1  3  6 10 15 21
  
*/

 




answered May 25 by avibootz
...