Hi, I'm completely new to C++, which I'm doing as part of a numerical methods course. I have experience with IDL, so I clearly like languages with training wheels :) I'm running the GNU C++ compiler under Ubuntu 8.04 on a Dell XPS.
I've made my first program run, but I find it ugly, and would like some help figuring out why it looks like it does. I'm trying to calculate partial sums of a series:
[TEX] $S_{N} = \sum_{i=1}^{N} (-1)^{i-1} * i^{-3}[/TEX]
Then some other algorithms for the same thing. The code below works, but I don't like the explosion of variables I've had to resort to. I'm storing -1 to a named variable. I mean, really? However, using commands like pow(-1,i-1) gives me an error that the pow command is overloaded and doesn't know whether to use float pow or double pow. So I'm wondering how I could write this program without using so many different variables. Thanks a lot,

Dave

// hmwrk11.cpp

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int N;
	float Sa=0;
	float i=1.0;
	float term_a1;
	float term_a2;
	float exp_a1;
	float m3 = -3.0;
	float m1 = -1.0;
	float base_a2;
	cout << "How many terms in the partial sum?: ";
	cin >> N;
// Method A, direct summation.
	for (i;i<N;i++) {
		exp_a1 = i - 1;
		base_a2 = i;
		term_a1 = pow(m1,i-1);
		term_a2 = pow(base_a2,m3);
		Sa = Sa + term_a1 * term_a2;
	}
	cout << "The partial sum from Method 1 is: " << Sa << endl;
// Method B for even N
	float k = 1.0;
	float Sb = 0.0;
	float term_b1;	
	float term_b2;
	float exp_b1;	
	float exp_b2;
	float tk;
	float tk1;
	if (N % 2 == 0) {
		for (k;k<N/2.0;k++) {
			tk = 2.0*k;			
			tk1 = tk--;			
			term_b1 = pow(m1,tk) * pow(tk1,m3);	
			term_b2 = pow(m1,tk1) * pow(tk,m3);
			Sb = Sb + term_b1 + term_b2;
			}
		cout << "The partial sum from Method 2 is: " << Sb << endl;
		float j=1.0;
		float m = 1;		
		float Sc=0;
		float term_c1 = 0;
		float term_c2 = 0;
		float tj = 0;
		float tj1 = 0;
		float tm = 0;		
		float temp = 0;
		for (j;j<N/2.0;j++) {
			tj = 2*j;
			tj1 = tj - 1;
			temp = pow(tj1,m3);
			term_c1 = term_c1 + term_c2;
		}
		temp = 0;		
		for (m;m<N/2.0;m++) {
			tm = 2*m;
			temp = pow(tm,m3);
			term_c2 = term_c2 + temp;
		}
		Sc = term_c1 - term_c2;
		cout << "The Partial sum from Method 3 is: " << Sc << endl;
		}
	else cout << "N isn't even!!!!" << endl;
return 0;
}

The first term of your sum is always zero, so start with i=2.
Use an if-statement for the first factor of your product. If i is even then it is -1 else it is 1. For the second factor I should use 1/(i*i*i).
So trash the power function and the rest of your variables, but as you might guess, leave your sum-variable in.

Ddanbe,

Thanks for the suggestions. As an alternative (which I post for others), I've also now discovered that you can "re-classify" numbers on the fly. I've saved variables by using:
S = S + pow(float(-1),i-1) * pow(float(i),-3);
as a replacement for:
exp_a1 = i - 1;
base_a2 = i;
term_a1 = pow(m1,i-1);
term_a2 = pow(base_a2,m3);
Sa = Sa + term_a1 * term_a2;
Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.