First a little introduction.
I have only a beginner background in Java and a long time ago I dabbled in BASIC but I find C++ to be a very desirable and fun language.
I decided to start from the beginning with Stroustrup's Programming Principles and Practice Using C++, then I am going to tackle Accelerated C++ and C++ Primer 4th Edition. I am very excited to finally be on this journey.
So far I have been doing well but I am only on Chapter 4. I was surprised to find that I had completed the previous drills and exorcizes similarly to what he was looking for. I like Stroustrup's straight forward approach and there is not a lot of hand-holding.
Anyway here are the Chapter 4 drills:
Quote
1. Write a program that consists of a while-loop that (each time around the
loop) reads in two ints and then prints them. Exit the program when a
terminating '||' is entered.
2. Change the program to write out "the smaller value is:" followed by the
smaller of the numbers and the larger value is: followed by the larger value.
3. Augment the program so that it writes the line the numbers are equal
(only) if they are equal.
4. Change the program so that it uses doubles instead of ints.
5. Change the program so that it writes out the numbers are almost equal
after writing out which is the larger and the smaller if the two numbers
differ by less than 1.0/10000000.
loop) reads in two ints and then prints them. Exit the program when a
terminating '||' is entered.
2. Change the program to write out "the smaller value is:" followed by the
smaller of the numbers and the larger value is: followed by the larger value.
3. Augment the program so that it writes the line the numbers are equal
(only) if they are equal.
4. Change the program so that it uses doubles instead of ints.
5. Change the program so that it writes out the numbers are almost equal
after writing out which is the larger and the smaller if the two numbers
differ by less than 1.0/10000000.
I am having trouble with number 5 and the "almost equal" part. Does 1.0/10000000 mean 0.0000001? If so wouldn't a double not be able to handle such a small figure?
Please don't make fun of me. My code skills are as I said very basic. Here is what I have so far:
#include "std_lib_facilities.h"
int main()
{
double val1=0;
double val2=0;
while (cin>>val1>>val2) { //read
cout<<"\nInput\n----------\n"<<val1<<" "<<val2<<"\n\n";
if (val1<val2)
cout<<"The smaller value is:"<<val1<<"\n\n"<<"The larger value is:"<<val2<<"\n\n";
else if (val1==val2)
cout<<"The numbers are equal.\n\n";
else if (val1-val2<1.0/10000000)
cout<<"The numbers are almost equal.\n\n";
else
cout<<"The smaller value is:"<<val2<<"\n"<<"The larger value is:"<<val1<<"\n\n";
}
}
Obviously my attempt to check for "almost equal" is way off but everything we have covered so far in the book doesn't help. Usually he will reference a later chapter to help me find the solution.
Thank you for your attention and for any pointers that you could give me.


Sign In
Create Account

Back to top









