0

I'm writing a basic piece of code to perform a successive approximation algorithm, however, when it runs the approximation loop, it seems unable to distinguish between a positive and a negative number in that it runs both if statements. The code is shown below, and if someone could point out where im going wrong, it would be really helpful.

#include <iostream.h>


int main(void)
{
float num1, num2, num3, num4, num5, num6, est, calcest, calcnum, calcdif, adj;
	
	est = 5.0;

	cout << "Please input first number: ";
	cin >> num1;
	cout <<num1;

	cout << "\nPlease input second number: ";
	cin >> num2;

	cout << "\nPlease input third number: ";
	cin >> num3;

	cout << "\nPlease input fourth number: ";
	cin >> num4;

	cout << "\nPlease input fifth number: " ;
	cin >> num5;

	cout << "\nPlease input sixth number: ";
	cin >> num6;
	
	adj = est / 2;
	cout << adj;

	calcest = est * est * est * est * est * est;
	cout << "\n" <<calcest;

	calcnum = (num1 * num2 * num3 * num4 * num5 * num6);
	cout << "\n" << calcnum;

	calcdif = calcnum - calcest;
	cout << "\n" << calcdif;
	
	while ((calcdif >(-100)) || (calcdif >100));
	{	
		if (calcdif < (-100));
		{
			est = est - adj;
			adj = adj / 2;
		}

		else if (calcdif > 100);
		{
			est = est + adj;
			adj = adj / 2;
		}

	calcest = est * est * est * est * est * est;
	calcdif = calcnum - calcest;
	}
	cout << "\nThe geometric mean is" << est;
	return 0;
}

<< moderator edit: added [code][/code] tags >>

Thanks
Kevin

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Dave Sinkula
0

As posted, I couldn't compile it. You seem to have misplaced several semicolons.

while ((calcdif >(-100)) || (calcdif >100));
	{	
		if (calcdif < (-100));
		{
			est = est - adj;
			adj = adj / 2;
		}

		else if (calcdif > 100);
		{
			est = est + adj;
			adj = adj / 2;
		}

You may want to double-check your loop condition.

Could you post sample input and expected output as well?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.