By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

Harmonic series

P: 5
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
Mar 1 '08 #1
Share this Question
Share on Google+
7 Replies


P: 28
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.



Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main (void)
  4.  
  5. {
  6. int  n;
  7.     printf("Enter a number:");
  8.     scanf("%d", &n);
  9.  
  10. int i, sum = 0;
  11. for (i=1; i<=n; i=i+1)
  12.     {
  13.         sum = sum + (i * i);
  14.     }
  15.  
  16. printf ("The intermediate partial sums are:\n", sum);
  17.  
  18. return(0);
  19. }
thank you in advance
You are computing 1 + 4 + 9 + ... = \sum n^2, try this:
<Laharl is right. Code removed - MODERATOR>
Mar 2 '08 #2

Expert 100+
P: 849
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?
Mar 2 '08 #3

P: 28
Well that was a waste of time
Mar 2 '08 #4

P: 2
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.
Mar 2 '08 #5

P: 5
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.
Mar 2 '08 #6

Expert 100+
P: 849
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.
Mar 3 '08 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
Look up how to add fractions.
Mar 3 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.