I'm new to C++ and here's a simple assignment. I'm not sure how to pull this off... ANY help will be appreciated . Here's the code for the assignment...

You have to implement a function called approachPi that takes no parameters, and has the return type double. This function returns the next element of a series, i.e. on the first call of the first element is returned, on the second call the second element is returned and so forth. The specific series that your function must use is the Gregory series (Difficult to write the formula here). I attached the formula.

This means on the first call to the function e1 = 4 will be returned, on second call e2 = 4(1 + -1/3) will be returned, and so on. This goes into infinity.

** The main file **

#include <iostream>
#include "Functions.h"
using namespace std;

int main()
{
	for(int i=0;i<3;++i)
		cout<<"Step "<<i+1<<":"<<approachPi()<<endl;
	
	return 0;
}

** The Functions.h file **

#ifndef FUN
#define FUN
double approachPi();
#endif

** My Functions.C file **

#include <iostream>
#include <cmath>
#include "Functions.h"

using namespace std;

double approachPi()
{
    double num;
    double sum;
    int size;
    
    for(int k = 1; k < size; k++)
    {
      cout << "Please enter a number: ";
      cin >> num;
      
      sum = num * pow(-1,(k+1))/((2*k)-1);
      cout << sum << endl;
    }
        
    return sum;
}

** Makefile **

main: Functions.o Prac1t1.o
	g++ -static Prac1t1.o Functions.o -o main

Prac1t1.o : Prac1t1.C 
	g++ -c Prac1t1.C 
	
Functions.o : Functions.C 
	g++ -c Functions.C

Dani AI

Generated

This task needs a stateful generator: each call to approachPi() must return the next partial sum of the Gregory series (first call => 4, second => 4 - 4/3, third => 4 - 4/3 + 4/5, ...). The function should not prompt for input or take the multiplier from cin; 4 is constant and any required I/O belongs in main.

Several concrete problems appear in the posted Functions.C: size is never initialized, sum isn't initialized properly, and reading from cin inside approachPi() is unnecessary. was right that a static value preserves state between calls, but a second static counter is also needed to remember which term comes next. correctly noted that asking for the number 4 repeatedly is wrong. ’s version works but recomputes the entire sum on each call; an incremental update is simpler and faster.

A minimal, clear approach is to keep a static index and a static running sum, compute the next alternating term from the index, add it to the running sum, then increment the index. Example implementation:

double approachPi() {
    static unsigned long idx = 0;     // which term to add next (0-based)
    static double partial = 0.0;      // running partial sum
    double sign = (idx % 2 == 0) ? 1.0 : -1.0;
    partial += 4.0 * sign / (2.0 * idx + 1.0);
    ++idx;
    return partial;
}

Notes and troubleshooting: keep all I/O (prompts, cout, cin) in main, not inside approachPi(). Initialize statics explicitly to avoid surprises. To restart the sequence, the program must be restarted or a separate reset function added; local statics are not reset between calls. For multithreaded code, local statics introduce race conditions, so protect them or avoid stateful globals.

Recommended Answers

All 7 Replies

And your immediate problem is what, exactly? You neglected to actually ask a question.

commented: indeed +17

And your immediate problem is what, exactly? You neglected to actually ask a question.

I am not sure what I'm doing wrong in my Functions.C file. For example my for loop may be incorrect... or the my code is wrong towards the formula... I'm not sure... :/

In approachPI(), make the sum static . That will remember the last value from the previous call. You can static any other values you need to remember. Be sure to set them to their initial values.

Ok. I changed the sum to static and yet it does not work exaaaaactly the way I want it to...

I understand what you meant by changing it to static but how do I add the previous sum to the "new" sum in the for loop?

Here's my code. When compiling the code it asks to input a number, you must insert 4 everytime. Then the output should be 4; 2,666666667; etc.

#include <iostream>
#include <cmath>
#include "Functions.h"

using namespace std;

double approachPi()
{
    double num;
    static double sumT;
    static double sum;
    int size;
    
    for(int k = 1; k < size; k++)
    {
      cout << "Please enter a number: ";
      cin >> num;
      
      
      sumT = pow(-1,(k+1))/((2*k)-1);
      
      sum = num*(sumT /*+ ?*/);   //I am not sure how to add the
                                  //previous sumT value to the new sumT value
         
      cout << sum << endl;
    }
        
    return sum;
}

Why cant you use something like this? The only variables in the formula are 'k' and how many times it loops, not sure why you ask for input when the number 4 is constant.

#include <iostream>
#include <cmath>

using namespace std;

double approachPi()
{
	double sum = 0;
	int size = 1000;
	for( int k = 1; k < size; k++ )
		sum += pow(-1,(k+1))/((2*k)-1);

	return 4*sum; //return the sum of the series * 4
}

int main()
{
	cout << approachPi() << endl;
	return 0;
}

I understand what you meant by changing it to static but how do I add the previous sum to the "new" sum in the for loop?

Because of static, the old sum is already in sum, or whatever. Static values have the previous value in them each time the function is called.

Here's my code. When compiling the code it asks to input a number, you must insert 4 everytime. Then the output should be 4; 2,666666667; etc.

So why is it asking? Is there a reason to have the function ask for a number?

And when compiling, the code asks for nothing. When running it asks for the number.

#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

#define Tellalca GOD;

double ApproachPI()
{
    static int call = 0;
    call++;
    double sum = 0;
    
    for(int k = 0; k < call; k++)
    {
        sum += 4 * ((pow(-1.0, k)) / (2 * k + 1));
    }
    
    return sum;
}

int main(int argc, char** argv) 
{
    for(int i = 0; i < 100; i++)
    {
        cout << "PI(" << i << ") = " << ApproachPI() << endl;
    }
    return 0;
}
commented: You've been here long enought to know we don't do homework for others. -4
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.