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
7 7976
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 are computing 1 + 4 + 9 + ... = \sum n^2, try this:
<Laharl is right. Code removed  MODERATOR>
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?
Well that was a waste of time
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.
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.
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.
Look up how to add fractions.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...

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...

by: aW 
last post by:
I have an interesting dilemma.
I have a table with the following records:
===================================================
Box  Series Start  Series End...

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...

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...

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()

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...

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...

by: Rina0 
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...

by: erikbower65 
last post by:
Using CodiumAI's pragent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install g codiumai' in the terminal.
2. Connect to...

by: linyimin 
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...

by: erikbower65 
last post by:
Here's a concise stepbystep guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...

by: kcodez 
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game  Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...

by: Taofi 
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...

by: DJRhino1175 
last post by:
When I run this code I get an error, its Runtime error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this 
If...

by: Rina0 
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...

by: lllomh 
last post by:
How does React native implement an English player?
 