Stopping program at five significant figures.

So my last post ended so well I decided to ask another question. My next project requires me to write a square root function using z = (x + S / x) / 2 as a base. I am required to make the program run until it is correct to five significant figures and then stop immediately. Such as 0.000059999 or 232450000.0. As this is homework, I would prefer that you not edit the code directly so I can figure it out. Could you help explain what I should do and perhaps a line of code I should add? If I still cant figure it out I might change like someone to help me with the code. Thanks again, this is an extremely helpful site!

By the way, this code has a lot of useless lines in it. Ill clean it up after I know what to do.

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
/*
Jacob Taylor
Jacob Chesley
Started on 1-28-2015
Last Modification on 1-28-2015
Switch
*/

#include <iostream>
using namespace std;


// x =   function for square root

int main()
{

	double S,
		x,
		z,
		loop,
		b;
	loop = 9999999;
	x = 1;

	std::cout << "Enter a value that you want the root of" << endl;
	std::cin >> S;
	z = (x + S / x) / 2;
	do
	{
		b = (z + S / z) / 2;
		z = (b + S / b) / 2;
		loop--;
	} while (loop > 1);




	std::cout << b << endl;




	return 0;
}
Hi,

Always initialise your variables to something, preferably at declaration and 1 variable per line of code, plus a comment if necessary.

Always specify the type of variables: on lines 23 & 24, they look like ints.

Is variable x even necessary? It doesn't change.

Some basic math: (x + S / x) is 1 + S where x =1. BEDMAS (Brackets, Exponents, Division, Multiplication, Addition, Subtraction) is that what you wanted to do?

Try to give meaningful names to your variables, they are a form of self documentation and aid understanding a great deal. Although, if your documentation (wiki say), uses certain chars as variables in math formulae, then stick with that, but comment them all, so readers will know what they mean. I like to copy formulae, then paste them as a comment: Lots of IDE editors support wide characters, so one can have all kinds of symbols. I also put web addresses as comments if relevant or helpful.

Line 10 is an example of a line you can get rid of.

You could use a while loop instead of a do while, with the end condition being as follows:

Say you were doing sqrt(101) : that is roughly 10.0, so 5sf would be what fraction of that?

Consider writing your own function to put your code into, make sure it returns a double.

Hope all is well at your end, and we all look forward to helping out more in the future :+)
Thanks for the information. I changed it a bit to become more streamlined. I'de like to take out line 22 all together and just have the code run until its needed to stop. Is there any way to do that?

Here is my new code. http://cpp.sh/2yxgl

Any recommendations you make, could you reference the line number your talking about? It's easier to understand.
Hi,

Line 26 is the same as (1+S)/2 - are you sure that is right?

I'de like to take out line 22 all together and just have the code run until its needed to stop. Is there any way to do that?


TheideasMan wrote:
Say you were doing sqrt(101) : that is roughly 10.0, so 5sf would be what fraction of that?


Line 27:
So you need to work out what that value is and store it in a variable. Then use it to make an end condition for the while loop. Is z the variable that you need to compare to in your end condition?

18
19
20
21
22
                double S = -1.0;       // purposely made these negative, so they 
		double z = -100.0;    //  are obviously wrong, if you get negative answers
		loop,               // then you didn't assign a proper value to these
                double StepSize = 0.0 // How much the values change after each loop
   		double b = -1000.0;  // put comments for all these variables, so we know what they are 


It's late at my end, work on Sunday - I look forward to seeing how you got on. :+) ZZZZZZZzzzzzzzzzz........
My teacher wants me to use his original code so I dont feel comfortable changing it.

I dont quite understand what your asking about fractions for sqrt(101) I put it into my code and I came out with 10.0499. So would it be roughly 201/200?

When it comes to line 27, I've never gone over the StepSize function. Im guessing it means when b and z have a 0.0 difference the code will stop. However this wont stop the code at exactly five significant figures every time. I would doubt it would ever stop it because it would calculate it as an ever decreasing decimal. Never as 0.0. But to answer, just how my code is made, z will be the final function, though thats not concrete.

I also need to have a contingency for my code that replies with "Nan" if anything entered below 0 or a letter is entered. But Ill get to that after my current issue is done.
Hi,

I dont quite understand what your asking about fractions for sqrt(101) I put it into my code and I came out with 10.0499. So would it be roughly 201/200?


So, if sqrt(101) is about 10.0, your answer was 10.0499, so your loop needs to stop when the difference between successive loops is less than 0.001 to achieve 5sf. 10.0 / 0.001 is equal to what ? Use that value to calculate the 0.001 to start with and use it for your end condition in the while loop.

I've never gone over the StepSize function


It's not a function - it's a value. In other words, it is the difference between successive loops. The 0.0 was just an initial value upon declaration - you need to set that to something. In the case of sqrt(101) it would be 0.001

Hope everything is clear now :+)
I apologize if im being difficult. Im a very visual learner. It would be so much easier if I just saw what you were talking about, but ide feel like that would be cheating.

When it comes to sqrt(101) the answer my code gives is 10.0499. However according to my teacher, that would be wrong because that is six significant figures. I need to tell my code to stop at 10.049 and immediately stop there. However I cant just tell it to stop when b and z reach a difference of 0.001 because if i used the code to find the sqrt(10-E10) It would be magnitudes less. Just as finding the sqrt(232E10) would be too many significant figures.

Thank you for taking the time on me. I really appreciate it.
Hi,

Here is another way to think about it: look for a pattern in the values.

We had sqrt(101) which is sqrt(10e2), the answer is about 10e1, which is a number that has 2 sf before the decimal place. We want 5 sf, so a loop has to stop at 10e-3.

Now consider sqrt(10e4), the answer is about 10e2 - which is a number that has 3 digits before the decimal place, so a loop would have to stop at 10e-2 to get 5 sf.

Now consider sqrt(10 e6)), the answer is about 10e3 - which is a number that has 4 digits before the decimal place, so a loop would have to stop at 10e-1 to get 5 sf.

Can you see the pattern forming here?

To find the exponent of a given number, count how many times you need to divide it by 10 until it is between 1 and 10 (use a while loop to do that, and put it in a function), or find a log base 10 function in a math library somewhere (maybe that is cheating a little ?)

The other thing is you can set the width of the output so it only displays so many decimal places, and you can set it to scientific format as well.

Thank you for taking the time on me. I really appreciate it.


No worries, everyone has to start somewhere :+)

The endof my morning tea has arrived, so I will have to go now. Cheers
Okay! I get what your saying! So ill have a variable based on how high above 1 a number is and another for how far below one is based on its multiples of ten.

This is what I have so far. http://cpp.sh/4wyt

Ill be working on finding something that I can use these two variables with until I hear back from you. Am I on the right track?
I would name your variables differently - positive & negative is not really meaningful. They are to do with exponents, so try to include that in the name.

I wish you would pay attention to the advice I have given so far - especially do do with the declaration of the variables.

Any way see how you go.
Im sorry. I really am paying attention. I just dont understand many of the things you say. I try and figure out the things you tell me and work from there. I dont mean for you to feel like your wasting you time.

Ill try re reading the things you have told me and Ill work on those things. If I dont understand then ill be sure to ask you. Right now I have a lot of projects on my plate and I'm trying to balance my time wisely.
Last edited on
Okay so im going to read through this whole post and Ill just list off my questions. Again, I apologize for making this hard on you.

“Always initialise your variables to something, preferably at declaration and 1 variable per line of code, plus a comment if necessary.” – What do you mean by this? You told me to do this and I thought I did by doing “Double S, x, z, etc…” Is that not initializing?

I honestly don’t understand what you mean by “Consider writing your own function to put your code into, make sure it returns a double.”

“Line 27:
So you need to work out what that value is and store it in a variable. Then use it to make an end condition for thewhile loop. Is z the variable that you need to compare to in your end condition?” I thought that by setting my loop = 9999999 I had an end condition. Im guessing your telling me I need to use another end condition so it will end exactly when I want it to. You told me to find what fraction I need to find what the end condition would be. However I honestly don’t know how I would put that in code. For example the sqrt(101) is 10.0499 but I need it to stop at 10.049. I know that 10.049 is roughly 1/10 of 101 or 10^2, but how would I use that information to put that in an end variable. >I’m not aloud to use the log base function.<

“The other thing is you can set the width of the output so it only displays so many decimal places, and you can set it to scientific format as well.” I’ve heard of using setprecision but I don’t think that’s what your talking about since that would break under small numbers. And I might be able to use scientific format but I believe my teacher would tell me to change it.

These are my thoughts. I believe that I'm at the point that I would learn better from seeing through example. If your still willing. I dont think my teacher minds how I learn, so long as I learn.
“Double S, x, z, etc…” Is that not initializing?


This bit :


18
19
20
21
22
                double S = -1.0;       // purposely made these negative, so they 
		double z = -100.0;    //  are obviously wrong, if you get negative answers
		loop,               // then you didn't assign a proper value to these
                double StepSize = 0.0 // How much the values change after each loop
   		double b = -1000.0;  // put comments for all these variables, so we know what they are 


Realise the difference between declaring & initialising. You had declarations, I was showing declaration & initialising by assigning a value to the variable at the same time. Notice I specified the type (double) for each one.

I honestly don’t understand what you mean by “Consider writing your own function to put your code into, make sure it returns a double.”


Instead of having all the code in main(), declare & define your own function (MySqrt say) which does your implementation of sqrt. That way you can call it multiple times (like in a loop) without having to repeat code.

I thought that by setting my loop = 9999999 I had an end condition.


Yes, it is an end condition, and yes I am trying to get you to use a different end condition. This was the explanation of how to do that:

TheIdeasMan wrote:
We had sqrt(101) which is sqrt(10e2), the answer is about 10e1, which is a number that has 2 sf before the decimal place. We want 5 sf, so a loop has to stop at 10e-3.

Now consider sqrt(10e4), the answer is about 10e2 - which is a number that has 3 digits before the decimal place, so a loop would have to stop at 10e-2 to get 5 sf.

Now consider sqrt(10 e6)), the answer is about 10e3 - which is a number that has 4 digits before the decimal place, so a loop would have to stop at 10e-1 to get 5 sf.

Can you see the pattern forming here?


I thought you said you understood that, but do you really?

I know that 10.049 is roughly 1/10 of 101 or 10^2, but how would I use that information to put that in an end variable. >I’m not aloud to use the log base function.<


It's the value of the exponent you need to look at - there is a pattern relating the exponent and achieving 5sf.

TheIdeasMan wrote:
To find the exponent of a given number, count how many times you need to divide it by 10 until it is between 1 and 10 (use a while loop to do that, and put it in a function), .....


So you can't use a log function, I said it may be cheating a little.

I’ve heard of using setprecision but I don’t think that’s what your talking about since that would break under small numbers. And I might be able to use scientific format but I believe my teacher would tell me to change it.


Yes, setprecision and setw is what I meant. If your number is 12345000, then you can work out how digits that is, assign it to a variable, then use that variable as an argument to the setw function.

http://www.cplusplus.com/reference/iomanip/setw/


I probably forgot to mention you should read the tutorials, articles, information & reference sections at the top left of this page - there is a wealth of information there - with examples for each function. I should have said that right at the start.

Hope this helps much more.
Topic archived. No new replies allowed.