Program which calculate a sum of series [C++]

In summary, the conversation discusses the speaker's attempt to write a program that calculates the sum of a given series, where the user inputs the number of terms and a value for x. The program uses nested for loops and the pow() function, but the speaker is unsure if this is the most efficient approach. The expert suggests using a Horner scheme for polynomial calculation instead, which eliminates the need for the pow() function.
  • #1
nodek
4
0
Hi ! I`m trying to write a program which will calculate the sum:
[tex]x+(x+x^{2})+(x+x^{2}+x{3})+...[/tex]

Homework Statement



Hi ! I`m trying to write a program which will calculate the sum:
[tex]x+(x+x^{2})+(x+x^{2}+x{3})+...[/tex]
I want the user to type in number of terms [tex]n[/tex], as well as a value of x. Here is my attempt:
#include <iostream>
#include <string>
#include <cmath>

using namespace std;int main() {

int n, x, sum=0 ;
cout<<"Enter the value for x: "<<endl;
cin>>x;

cout<<"Enter the number of terms : " <<endl;
cin>>n;

for( int i =1; 1<=n; i++) {
for (int j=1; j<=i; j++)
{
sum+=pow(x, i);
}
}

cout<<"The sum is"<<sum<<endl;
return 0;

cin.get();
cin.get();
}Is it a good way of doing that? One of mistakes that I made was : "error C2668: 'pow' : ambiguous call to overloaded function". What should I do about it?

Regards
Nodek
 
Physics news on Phys.org
  • #2
Why do you use ints for everything? What are types of expected arguments of pow()?

Also, your approach is about as inefficient as possible. Note that

[tex]x + (x + x^2) + (x + x^2 + x^3) = 3x + 2x^2 + x^3[/tex]

and using Horner scheme for polynomial calculation:

[tex]3x + 2x^2 + x^3 = x(3+x(2+x))[/tex]

That means you don't need pow() function - which is numerically heavy - at all.
 

1. How does the program calculate the sum of a series?

The program uses a loop structure to add each term in the series. It starts with the first term and adds it to the sum variable. Then, it moves on to the next term and adds it to the sum, and so on until it reaches the desired number of terms.

2. Can the program handle different types of series?

Yes, the program can handle different types of series as long as the user inputs the correct formula for calculating the next term in the series. It can handle arithmetic, geometric, and other types of series.

3. How does the program determine the number of terms to add?

The program asks the user to input the number of terms they want to add in the series. It then uses this number to set the limit for the loop and calculate the sum of the series.

4. Is there a limit to the number of terms the program can handle?

Technically, there is no limit to the number of terms the program can handle. However, for very large numbers, the program may take longer to calculate the sum, and there is a possibility of encountering overflow errors.

5. Can the program handle negative numbers in the series?

Yes, the program can handle negative numbers in the series. As long as the user inputs the correct formula for calculating the next term, the program will be able to calculate the sum correctly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
725
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
803
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
Back
Top