A Brief Note On Fractions

Submitted By hopewithmlll
Words: 820
Pages: 4

Lecture 4
Wednesday, April 10, 2013
2:00 PM double: can hold fraction * 10 to the -308 up to 10 to the 308 * about 15 significant digits * ex. -0.10, .123, 12.34, 56. int: holding only integers, generally limited to about -2 billion to +2 billion * ex. 123, -45 * drops fraction if entered double ex. int k - 123.8898; // k will be 123, drops the fraction. BE CAREFUL compiler warning, maybe legal but not what author intended
DON'T IGNORE WARNINGS compiler can convert int to double opeartions +-*/ * both operands double, double * both operands int, int * 14.3 / 5.0 = 2.86 * 14.3 / 5 = 2.86 * 14/ 5.0 = 2.8 * 14/5 = 2 // fraction dropped % is the remainder / modulus operator * 14 %[mod] 5 ==> 4 * useful for page number [pg #] % 2 ==> 0 for even, 1 for odd * calendar applications (feb. 29) double x = 3.1 + 14/5; // x is 5.1 * 3.1 + 2 <== opearted in int., thus x = 5.1 double y = 2/3 * x; // y is 0.0 * 2.0/3 * x;

int a = 10; int b = a*a; int c = 25/(b-100); undefined behavior double d; double e = 2 * d; //Error, d is uninitialized, but we're trying to use its value cout << e; likely to be runtime error int f = 1000; int g = f * f * f; int h = f * g; // technically undefined, but practically, h gets a weird value h = 1 trillion , too big to be an int take lower of 32 bits, like odometer 99999 ==> 00000 after 1 more mile

What is your name? Sir Robin
How old are you? 32
What is your quest? To seek the Holy Grail
Hello, brave Sir Robin!
You want To seek the Holy Grail
If you live, next year you will be 33
=========================================

#include <iostream> using namespace std; int main ()
{
cout << "What is your name? "; string personsName; getline(cin, personsName); // getline function in the library, cin input source
// (input, string) arguments for getline function cin >> personsName; //Not what we want cout << "How old are you? "; int age; cin >> age; // never consumes newline character from 32 [enter]
//...do something to discard the rest of the line … cin.ignore(10000, '\n'); // MAKE SURE IT'S A BACKSLASH // "/n" is wrong
// consume and throw away 10000 characters until you reach a newline character
// don't go over 1billion, or weird things happen cout << "What is your quest? "; string quest; getline(cin, quest); cout << "Hello, brave " << personsName <<" ,, endl; cout << "You want " << quest << endl; cout << "If you live, next year you will be " << (age+1) << endl;
}

What is yoru name? Sir Robin
How old are you? 32
What is your quest? Hello, brave Sir Robin!
You want
If you live, next year you will be 33
// problem because read in cin then getline

personsName: (empty string) persons Name: =====> Sir Robin quest: To seek the Holy Grail

What is your name? Sir
What is your quest? Hello, brave Sir!
You want Robin // read the first word, Sir, left "Robin" for later input to consume. Program does not wait for you to input "What is your quest"

personsName: Sir quest: [space]Robin

How many hours did you work? 40 You typed 40