Sep 30, 2012 at 12:06am UTC
My program works fine, but I am struggling developing a user-defined function for the for loop. Everything I try won't work, any help would be greatly appreciated.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int counter;
double firstTerm = ' ';
double commRatio = ' ';
double total, numTerms = ' ';
double sum;
cout << "Program will compute terms in a Geometric Series"<<endl<<endl;
cout << "Enter the first term in the series: ";
cin >> firstTerm;
cout<<endl;
cout<<"Enter the common ratio for the series: ";
cin >> commRatio;
cout<<endl;
cout<<"Enter the number of terms to use: ";
cin>>numTerms;
//while (numTerms < 2)
cout<<endl;
cout << fixed << showpoint;
cout <<setprecision(4)<< "Terms in series for Common Ration
of "<<commRatio<<" are:"<<endl;
cout << setprecision(9);
for (int counter = 1; counter <= numTerms; counter++ )
{
sum = firstTerm * pow(commRatio,(counter-1));
total += sum;
cout << setw(20) <<sum<< endl;
}
cout<<endl;
system ("pause");
return 0;
}
Sep 30, 2012 at 2:01am UTC
Not sure why the pow function is used.
In a geometric series, each term can be derived from the previous one by a simple multiplication by the common ratio.
A question: are you trying to list all of the terms, or just calculate the sum of all the terms?
Oct 1, 2012 at 1:51pm UTC
Okay I got rid of the ' '; and initialized to 0;
I am trying to list the terms.
My delema is now this, any value returned from the function to main is 0, how do I take the input value from the function to main?
double startTerm (double firstTerm, string str1);
double startTerm (double ft, string str1)
{
double newTerm;
cout<<"Enter first term: ";
cin>>newTerm;
cout<<endl;
while (newTerm > 1)
{
return newTerm;
}
if (newTerm <= 0)
{
cout<< str1 << " " <<endl;