-2

What I want to do is to get a cumulative sum of previous integers starting from 1, for example: If my input is 4, then the function should work in this way; 1 + (1+2) + (1+2+3) + (1+2+3+4) = 20. And the output needs to be 20. Also, I have to get this done by a function, not in main(); function while using int n as the only variable.

What I've tried is to make a function which adds from 1 to integer N, and use 'for'to make N start from 1, so that it can fully add the whole numbers until it reaches N.

#include <stdio.h>
int sum(int n);
int main() {

    int n, input, sum;
    sum = 0;
    scanf("%d", &n);
    for (n = 0; n <= input; n++) {
        sum += n;
    }
    printf("%d", sum);
    return 0;
}

int sum(int n) {
    int i, n, sum = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i += 1){
        sum += i;
    }   
return n;
}

What I expected when the input is 4 is 20, but the actual output is 10.

violagang
  • 11
  • 1
  • 2
  • 1
    Look at your code. You're calculating 1+2+3+4 – klutt May 21 '19 at 10:06
  • 4
    It seems to me that you need to go back to your books or tutorials or class notes and read more about functions and how to use them. Because you're never calling the function `sum`, instead you define a separate *variable* with the name `sum`, and that variable is totally unrelated to the function with the same name. – Some programmer dude May 21 '19 at 10:06
  • Hi, looks also on this https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems and feel free to try to use debugger, I believe you will get the issue soon :) Basically, I can tell you, you are doing only first iteration, with complete result (as mentioned in previous comment) – xxxvodnikxxx May 21 '19 at 10:06
  • There are also other problem in your code, like using the uninitialized variable `input`. Or reading from the user into `n` and then directly afterward do `n = 0`. The function you have is also very wrong and won't do what you expect it to (consider what it returns). – Some programmer dude May 21 '19 at 10:08
  • Your program should not compile. And you don't even call the `sum` function. What is your platform compiler/OS/IDE etc.? – Jabberwocky May 21 '19 at 10:08
  • 1
    Note that you have called a function and a variable with the same name, I suggest you to change sum(int n) with mysum(int n) – enrico_ay May 21 '19 at 10:08
  • This does not even compile. – klutt May 21 '19 at 10:10
  • I'm using Visual Basic 2017 on Windows8.1, and it does compile but I'm trying to look in what is wrong with this code, starting from taking a look into how function actually works and the overall structure of my code. I'll double check if it doesn't really compile and get to fix what is wrong with it. – violagang May 21 '19 at 10:18

3 Answers3

1

I would have written it this way, remarks are where changes been made

#include <stdio.h>
int sum(int n);
int main() {

int n, input, sum;
// sum = 0;  // no need for this
scanf("%d", &n);
/* the next for has no use
for (n = 0; n <= input; n++) {
    sum += n;
} */
// I would be adding some input sanitazing if possible here
printf("%d", sum(n));
   return 0;
}

int sum(int n) {
  int i, /*n, */ rsum = 0; // n is already a parameter, rsum for running sum

  // scanf("%d", &n);   // nope nope, scanf and printf should be avoided in functions
  for (i = 1; i <= n; i++){    // changed i +=1 with i++ , easier to read
     for (j=1;j<=i;j++) // need this other loop inside  
          rsum += j;
   }   
 return rsum;
}
Gar
  • 852
  • 13
  • 20
0

The main issue is in the function, you are doing only 1 loop (you have also some logical things, which compiler should tell you, like same naming of variable and function.. eg.),

so in case you will put 4 as the input, loop will do only 1+2+3+4, but your case if different, you want to make suma of all iterations like 1 + (1+2) + (1+2+3) + (1+2+3+4)

you are doing only last step basically (1+2+3+4), 4 iterations (4x suma), but actually you need 10 iterations (due suma of all particular suma of elements)

As suggested, try to debug your code - What is a debugger and how can it help me diagnose problems? - it will really help you do understand your code

As mentioned, the issue is in

int sum(int n) {
    int i, n, sum = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i += 1){
        sum += i;
    }   
return n;
}

You have to make two loops eg. like follows:

 int sum,n = 0;

    //scanf("%d", &n);
    n = 4; //input simulation 

    //just for demonstration
    int iterations = 0;

    //counter for total loops (how many "brackets" needs to be count)
    for(int loopsCounter = 1; loopsCounter <= n;loopsCounter++){
        //counter for child elements in brackets (like 1+2 ,1+2+3, ..)
        for (int sumaLoopCounter = 1; sumaLoopCounter <= loopsCounter; sumaLoopCounter++){
            //simply make sum with the given number 
            /* first step 0 +1 
             second 1+2 added to previous suma = 1+3
             third 1+2+3,.. added to previous = 4+6
             ...
            */
            sum += sumaLoopCounter;

            //just testing for demonstration
            iterations++; //iterations = iterations + 1
        }       
    }


    printf("%i \n",iterations);
    printf("%i",sum);

Then you got output as expected - sum of all "bracket elements" and 10 iterations, which matches numbers of needed additions

10
20
xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
0

Here it is with a single loop; very fast.

#include <stdio.h>

int cumulative_sum(int m)
{
    int sum = 0;
    for(int n=1; n<=m; ++n) sum += n*(n+1);
    return sum/2;
}

int main(void)
{
    int n;
    printf("Input value N: ");
    scanf("%d", &n);

    printf("Answer is %d\n", cumulative_sum(n));
    return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159