-2

I am using a nested for loop to use to solve the given problem.

I have 4 integers to declare: i (for initialization of the outer loop), j (for initialization of the inner loop, s (for displaying the sum of the series) & term (for finding the term values in the series). up to here i'm ok with. What I do not get is the necessity in assigning s & term with value 0.

Also, why is term = term +j and s = s+term?

int i,j,n,s,term;
s= 0;
for(i=1; i<=n; i++) {
    term=0;
    for(j=1; j<=i; j++) {
        term = term +j;
    }
    s = s+term;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
Bao
  • 25
  • 1
  • 1
  • 10
  • Can you please clarify your questions in more detail? What do you think would happen if `s` and `term` were not set to `0` and what do you expect instead of the two later assignments? – walnut Sep 08 '19 at 19:47
  • Up until here(loops) there has been no requirement of assigning values to variable. Hence, I have the question. I am very very new to programming. As well as to Stack overflow. So, pardon if I make a gaffe in the usage of the site. – Bao Sep 08 '19 at 19:51
  • Then you should be reading some introductory book to C, for some possible choices see [this list](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). If you then have a specific question, ask about it here. – walnut Sep 08 '19 at 19:54
  • "Up until here(loops) there has been no requirement of assigning values to variable." That is not true. You always have to assign something to a variable before reading it. That has already been true for all your previous topics/assignments/textbook-chapters. But you probably might not have noticed because it always happened implicitly. If you ever read a variable before it is initialised/written you are vulnerable to "undefined behaviour" (look it up) and you will not like it. (There is strictly spoken an exception for global variables. Using them however is a separate discouraged concept.) – Yunnosch Sep 08 '19 at 19:55
  • I began with programming of finding the sum of two integers. In which 'sum' was the variable, and the program was written w/o the assignment. But as you may have suggested it could have been done implicitly. I wasn't made to be aware of it. What do you think was the value of sum in such a program? – Bao Sep 08 '19 at 20:25
  • Also, could somebody hint to me why my question may have been downvoted. I am merely 24 hours old on this site. I am just not well versed with the stack overflow etiquette as of yet. – Bao Sep 08 '19 at 20:28
  • The suggestion for book reading has been well noted. I am imdeed in the midst of reading one. It's just the book I have, hasn't been all that helpful, going by the looks of it. – Bao Sep 08 '19 at 20:32
  • You can avoid assignment by writing your code recursively. Unfortunately you will then be at your compiler's mercy, because recursion will quickly cause stack overflow unless the compiler eliminates it (particularly for tail-recursion), but quality compilers do (for sane architectures). – EOF Sep 08 '19 at 20:36
  • You can simplify this to a 3rd-degree polynomial that runs in `O(1)` time; I think, `n(n+1)(n+2)/6`. – Neil Sep 08 '19 at 21:00

2 Answers2

1

In the line,

term = term +j;

if you dont initialize term with zero, default value which is garbage is stored in term, so

You were expecting term = 0 + j, which would give correct value

But you actually get G = G + j where G is garbage. Similarly, sum will be garbage as well and you will never get the correct value

arvind
  • 275
  • 2
  • 11
1

It is entirely possible to simplify this mathematically and get a better run-time.

S = \sum_{n=1}^N (\sum_{k=1}^n k)

Wikipedia's explanation of \sum_{k=1}^n k = n(n+1)/2.

S = 1/2 \sum_{n=1}^N n^2 + 1/2 \sum_{n=1}^N n
  = 1/12 N(N+1)(2N+1) + 1/4 N(N+1)
  = 1/6 N(N+1)(N+2)

Simplified,

#include <stdio.h>

static int s(const int n) {
    return n * (n + 1) * (n + 2) / 6;
}

int main(void) {
    printf("S(5) = %d\n", s(5));
    return 0;
}
Neil
  • 1,767
  • 2
  • 16
  • 22