My first algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
using namespace std;
int main()
{
	cout.precision(36);
cout << "Anmol's Square Root Calculator!" << endl;
cout << "" <<endl;
cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm!\n";
cout <<""<<endl;
cout <<"Only positive numbers work with this algorithm!"<<endl;
cout <<""<<endl;
cout <<""<<endl;
cout <<"The algorithm just repeats the following formula as many times as needed\n""to get to the desired accuracy."<<endl;
cout <<""<<endl;
cout <<"x = (x + (n/x))/2"<<endl;
cout <<""<<endl;
cout <<"Where x is any estimate number of the sqrt\n""and n is the number you need the sqrt of."<<endl;
cout <<""<<endl;	
cout <<"The program is programmed to always use 1 as the very first estimate."<<endl;
cout <<"The very first estimate can be any number but the closer it is to\n""the actual square root the algorithm takes less iterations in order to find the actual square root."<<endl;
cout <<""<<endl;	
cout <<""<<endl;	
cout <<""<<endl;
char playAgain='y';
while (playAgain !='n') 
{
int count, cycle(1);
long double guess, sqrt, num; 
cout << "Please enter the number you would like to compute the square root of: ";
cin >> num;
cout << "" << endl;
do{
if (num <= 0)
{
	cout<<""<<endl;
cout<<"Invalid input. Please re-enter a valid number: ";
cin >> num;
cout<<""<<endl;
}
}
while (num <= 0);
cout<< "Please enter the number of times you would like to iterate the algorithm: ";
cin>>count;
do{
if (count <= 0)
{
	cout<<""<<endl;
cout<<"Invalid input. Please re-enter a valid number: ";
cin >> count;
cout<<""<<endl;
}
}
while (count <= 0);
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"Iteration #1 Approximate: 1"<<endl;
	cycle++;
for (guess=1; count!=1; count--)
{cout<<""<<endl;
cout<<"Approximate #"<<cycle<<" = ("<<guess<<" + ("<<num<<" / "<<guess<<")) / 2."<<endl;
guess =(guess + (num/guess))/2;
cout<<""<<endl;
cout<<"Iteration #"<<cycle<<" Approximate: "<<guess<<""<<endl;
sqrt = guess;

cycle++;
}
cout<<""<<endl;
cout <<""<< endl;

do { cout <<""<<endl;
		cout << "Would you like to calculate again? (y/n): ";
		cin >> playAgain;
		cout <<" "<<endl;
		do { cout <<""<<endl;
		if ((playAgain !='y' && playAgain !='n'))
		{
		cout << "Invalid input. Only y/n: ";
		cin >> playAgain;
		cout <<" "<<endl;
		
		} }while (playAgain !='y' && playAgain !='n');
		
		} while (playAgain !='y' && playAgain !='n');
		 
	}
cout<<"Thank you for using a program made by Anmol Sethi!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"Hope you use it again!"<<endl;
cout<<""<<endl;
cin.get();
return 0;
}

Thats my algorithm program :D. It uses the babylonian algorithm to find the approximate square root of the number you enter. Can I get any feed back on what to improve and how?
Last edited on
First impression:

Use consistent indentation.
Use whitespace to increase readability.
Using std::endl should only be used when you need to flush the output stream.
Refactoring code into functions would be good.
Move root calculation to separate function: something like: long double bsqrt(long double num, unsigned int count)
How do I move it into a separate function?
What do you mean flush the output stream?
Ill try using indentation and whitespace
What do you mean flush the output stream?


http://www.cplusplus.com/reference/ios/endl/

For most situations, inserting a newline into the output stream is more appropriate (and a much less expensive operation on buffered streams.)

One could rewrite:
1
2
3
4
5
cout<<"Thank you for using a program made by Anmol Sethi!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"Hope you use it again!"<<endl;
cout<<""<<endl;


as:
1
2
cout << "Thank you for using a program made by Anmol Sethi!\n\n\n";
cout << "Hope you use it again!\n\n" ;


Or even as:
1
2
cout << "Thank you for using a program made by Anmol Sethi!\n\n\n"
        "Hope you use it again!\n\n" ;
Oooo ok

So using escape sequences instead of endl?

Ill do that in my next code after this depending on what we learn in math next xD
Topic archived. No new replies allowed.