C: Implementing a series

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashiela
    New Member
    • Apr 2010
    • 7

    C: Implementing a series

    Write a C program to find the sum of the
    following
    1*1*1+(1*1*1+2* 2*2)+(1*1*1+2*2 *2+3*3*3)+…n
    terms
    using for, while, do...while

    reply to my inbox pleasee..thank you..:(
    i've tried but still didnt work


    Code:
    main()
    {
    int i=1,sum=1,c=1,j;
    scanf("%d", &j);
    while(i<=j)
    {
    printf("%d\n", sum);
    i=i+1;
    c=(c*c*c)+i;
    sum=sum+c;
    }
    }
    can anyone check for me the mistake i made??? please..
    my output should be
    10
    9
    36
    Last edited by Banfa; Apr 19 '10, 10:24 AM. Reason: Added [code]...[/code] tags
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Code:
    for(i=1;i<=n;i++)
    {
            for(j=i;j>=1;j--)
            {
                    interSum = (j*j*j);
                    sum = sum + interSum;
            }
    }
    printf("%d\n",sum);

    Try this.

    Regards
    Dheeraj Joshi

    Comment

    • ashiela
      New Member
      • Apr 2010
      • 7

      #3
      i followed what you have done. but i've got error like this:

      C:\Users\user\D ocuments\Docume nts\Downloads\D ocuments\output \acd.c(1): warning #2099: Missing type specifier; assuming 'int'.
      C:\Users\user\D ocuments\Docume nts\Downloads\D ocuments\output \acd.c(5): error #2048: Undeclared identifier 'n'.
      C:\Users\user\D ocuments\Docume nts\Downloads\D ocuments\output \acd.c(9): error #2048: Undeclared identifier 'i'.
      C:\Users\user\D ocuments\Docume nts\Downloads\D ocuments\output \acd.c(14): warning #2027: Missing prototype for 'printf'.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        @ashiela, what dheerajjoshim provided is not a complete program it as a coding of the algorithm design for you to use with-in the frame work you have already created. However I do not believe it is coded correctly, you should work out in your head or using a calculator or using a spread sheet the result of the algorithm for a few know points, for example the value when n = 1, 2, 3 and 10 so that when you test your program you can run some basic checks to test it is is working.

        @dheerajjoshim if it weren't for the fact that I do not believe you have posted a correct implementation of the algorithm required I would say that you were dangerously close to providing the solution to the OP which we discourage here in favour of helping the understand the problem and how to solve it so they can create there own solution.

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Have you ever tried using debugging statements?
          eg, in between each round you could put in a statement like so:
          printf("i = %d and c = %d \n", i,c);
          If those values aren't what you're expecting, you should fix them first.


          You can comment out these lines in the final implementation.

          Comment

          • ashiela
            New Member
            • Apr 2010
            • 7

            #6
            i've got my answer thanks anyway :D

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Write an expression (on paper) for the ith term of the sequence. Part of this is deciding if terms go from 0 to n-1; or 1 to n; or 0 to n. Use a calculator or spreadsheet (as suggested by Banfa) to verify this expression. Once you have it on paper, it should be easy to implement in code.

              Computing the sum of the terms is trivial:
              Code:
              sum = 0;
              for (i=1; i<=n; i++)
                 sum += term(i);
              Adjust the starting and ending i accordingly if your terms don't go from 1 to n.
              FYI: this code snippet presents a concept; don't expect it to compile.

              The value of the sequence gets big pretty fast as n increases. You should do a quick check to make sure your integral type won't overflow. Conversely, it would be a very good idea to determine the smallest value of n that causes overflow and reject any value greater than or equal to that limit. You should also reject negative n; and you should reject n==0 if your terms start at 1. I advise you to get in the habit of validity-checking all user input.

              Once you get this working you may want to make it run faster: look for a relationship between term(i) and term(i-1). However, don't get ahead of yourself -- you gain nothing by optimizing a program that doesn't work.

              Comment

              • whodgson
                Contributor
                • Jan 2007
                • 542

                #8
                Seeing the OP didn`t give us his solution I think the following provides.
                Code:
                for(int i=1;i<=k;i++)
                    {
                    n+=i*i*i;
                    }
                //n is the sum of k terms

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  No n is the kth term

                  Comment

                  • jkmyoung
                    Recognized Expert Top Contributor
                    • Mar 2006
                    • 2057

                    #10
                    you just need a m+=n in the loop.

                    n = Tk
                    m = Sk
                    Last edited by jkmyoung; Apr 22 '10, 03:13 PM. Reason: --edit for below

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      Originally posted by ashiela
                      ... find the sum of the following
                      1*1*1 + (1*1*1+2*2*2) + (1*1*1+2*2*2+3* 3*3) + …
                      The kth term of the series (Tk) is Sum[j=1:k](j^3).
                      The sum the OP asked for is Sum[i=1:n](Ti).
                      That is, ...
                      ... Sum[i=1:n](Sum[j=1:i](j^3))

                      Comment

                      • whodgson
                        Contributor
                        • Jan 2007
                        • 542

                        #12
                        @Banfa I don`t think so.....'n+=' results in a running total.

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          n is a running total but it is the running total for the current term not the running total for the actual result of the series.

                          Comment

                          • whodgson
                            Contributor
                            • Jan 2007
                            • 542

                            #14
                            Yes my 12th thread is garbage as you say Banfa it is the kth term. now all I have to do is find my missing neurones

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              I think I saw them bouncing on the floor and rolling under the skirting board :D

                              Comment

                              Working...