473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
Mar 1 '08 #1
7 8021
fual
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
Laharl
849 Expert 512MB
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
fual
28
Well that was a waste of time
Mar 2 '08 #4
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
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
Laharl
849 Expert 512MB
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
9,208 Expert Mod 8TB
Look up how to add fractions.
Mar 3 '08 #8

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

Similar topics

1
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
6
by: Daniel Kabs | last post by:
Hello there, I have a Nokia 6630 phone that is based on the Series 60 Software Platform. -> http://www.series60.com/ These "smartphones" are said to support HTML 4.01 and "a subset of...
4
by: aW | last post by:
I have an interesting dilemma. I have a table with the following records: =================================================== Box | Series Start | Series End...
1
by: Sriram Krishnan [at] gmx [dot] net | last post by:
Given an arbitrary number series, how do I identify the series and find the Nth number... for e.g 1,3,5,7 is an AP and the next number will be 9 1,8,27,64 is the series of cubes and the next will...
4
by: geoffp | last post by:
I need to generate reproducible random number series. I've done the obvious - use mt_srand with the same seed. This supposedly will create the same series every time. Is this true? Its not...
4
by: keirnus | last post by:
Hello, I've been coding in Access VBA to create an Excel Graph and it was good. Until I got this error: Please check the code below: Private Sub TestGraph3()
1
by: Scholar | last post by:
Hi,i am working on a c++ project which will find the nth term of a given series.The series would be given in the form of first 4 or 5 terms.I have the basic alogorithm which can find the nth term of...
4
by: Dave067 | last post by:
Hi Folks! I was trying to write some Python script to calculate resistor values for electronics projects, and wanted to generate lists of the preferred resistor values in each standard...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.