Harmonic series

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BelleBaby
    New Member
    • Mar 2008
    • 5

    #1

    Harmonic series

    Hello. I am having trouble with one of my labs in my programming class. It seems to be simple and to the point, but I'm not sure if I understand it all. This is the question:

    Write a program to compute the partial sum of harmonic series

    1 + 1/2 +1/3 + ... + 1/n

    and display the intermediate partial sums. Hint: use a for loop.

    This is what I came up with. I know it's not 100% correct, but I got myself stated, I just need a push in the right direction.



    #include <stdio.h>

    int main (void)

    {
    int n;
    printf("Enter a number:");
    scanf("%d", &n);

    int i, sum = 0;
    for (i=1; i<=n; i=i+1)
    {
    sum = sum + (i * i);
    }

    printf ("The intermediate partial sums are:\n", sum);

    return(0);
    }




    thank you in advance
  • fual
    New Member
    • Feb 2008
    • 28

    #2
    Originally posted by BelleBaby
    Hello. I am having trouble with one of my labs in my programming class. It seems to be simple and to the point, but I'm not sure if I understand it all. This is the question:

    Write a program to compute the partial sum of harmonic series

    1 + 1/2 +1/3 + ... + 1/n

    and display the intermediate partial sums. Hint: use a for loop.

    This is what I came up with. I know it's not 100% correct, but I got myself stated, I just need a push in the right direction.



    Code:
    #include <stdio.h>
    
    int main (void)
    
    {
    int  n;
    	printf("Enter a number:");
    	scanf("%d", &n);
    
    int i, sum = 0;
    for (i=1; i<=n; i=i+1)
    	{
    		sum = sum + (i * i);
    	}
    
    printf ("The intermediate partial sums are:\n", sum);
    
    return(0);
    }
    thank you in advance
    You are computing 1 + 4 + 9 + ... = \sum n^2, try this:
    <Laharl is right. Code removed - MODERATOR>

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      Originally posted by fual
      You are computing 1 + 4 + 9 + ... = \sum n^2, try this:
      *snip*
      First, we don't spoonfeed code here.

      I think he has a good reason to use the C headers, given that he's using printf and scanf, he's probably using C, not C++.

      @OP: You're on the right general track, but you're performing the wrong operation on i before you add it to sum. Also, to print each intermediate step. where would you need a printf call and what would you print out?

      Comment

      • fual
        New Member
        • Feb 2008
        • 28

        #4
        Well that was a waste of time

        Comment

        • russellelly
          New Member
          • Mar 2008
          • 2

          #5
          Originally posted by BelleBaby
          Hello. I am having trouble with one of my labs in my programming class. It seems to be simple and to the point, but I'm not sure if I understand it all. This is the question:

          Write a program to compute the partial sum of harmonic series

          1 + 1/2 +1/3 + ... + 1/n

          and display the intermediate partial sums. Hint: use a for loop.

          This is what I came up with. I know it's not 100% correct, but I got myself stated, I just need a push in the right direction.



          #include <stdio.h>

          int main (void)

          {
          int n;
          printf("Enter a number:");
          scanf("%d", &n);

          int i, sum = 0;
          for (i=1; i<=n; i=i+1)
          {
          sum = sum + (i * i);
          }

          printf ("The intermediate partial sums are:\n", sum);

          return(0);
          }




          thank you in advance
          You've got the gist of it , but there's some fairly fundamental problems:

          When is your printf going to be called? Is it really going to show intermediate sums? And how does you printf know how you want variable sum display as?
          If sum is representing fractions is int an appropriate type?
          In your scanf why is the address of memory location 'n' useful (hint: it isn't!)
          Is your formula in the loop reflecting the one in the question (I think you're missing the 'one over' part).

          Hope that gives you some things to think about!

          EDIT - sorry, thought there were no replies before I posted? A fair few of my points have already been covered.

          Comment

          • BelleBaby
            New Member
            • Mar 2008
            • 5

            #6
            Hey...thanks for the help, but I still don't understand it. Your questions made me think more about the problem, but I don't know what to change still. Thanks anyways.

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              You're really not that far off. You've got the loop right, you just need to change the statement inside and add another one. What would you need to add inside the loop to print the value of sum at each step? Printf, but where do you add the call and what parameters do you need to call it with?

              As to the mathematical operation, should sum really be an integer? Is 1/2 an integer? Or 1/3? What datatype would better support the need for decimals? Also, a hint. Many basic mathematical operations look the same in C as they do on a piece of paper. Division is one of them.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Look up how to add fractions.

                Comment

                Working...