Some remarks before we get started… Last time we learned about header files – in particular the math.h header file. You know that if you include math.h you will have access to various built-in functions like cos(x), sin(x), acos(x), pow(x), floor(x), exp(x), etc…
Some remarks before we get started… What you may not know... Is that the functions may operate on a different data type than you pass in. For example: float x = 0.0; /* declare float */ float y = 0.0; /* declare as float */ y = cos(x); /* casts x as double to do cosine */ Even though you pass in a float, x, the function cos will “cast” up the float to a double (higher precision float) to perform the cosine function and return a double. After the function call, y is still remains a float, unless its declaration is changed to a double.
Some remarks before we get started… If you are interested in what data types a function reads in, type at the Linux command prompt: man cos The man command (manual) gives a bunch of information on (in this case) the cos function.
One quick question… Which one is the correct way to read in a double? #include int main(void) { double x; printf (“Input a number”); scanf(“%f”, &x); return 0; } #include int main(void) { double x; printf (“Input a number”); scanf(“%lf”, &x); return 0; } #include int main(void) { double x; printf (“Input a number”); scanf(“%d”, &x); return 0; }
Debugging… int main(void) { int x=5; int y, div, mult, sub; y = 1; mult = x * y; mult = mult/100; div = x/mult; sub = div - mult; } 1. Call Professor Garvin This code gave me a weird error of “Floating point exception”. What is a good way of determining where the problem is? 2. Call Professor Garvin’s wife (who is also Prof. Garvin) – everyone knows she is the brains in the family 3. Put printf statements writing out y, mult, div, sub, in between each of the lines 4. Call Professor Garvin’s dad – he has a Masters in Computer Science
Debugging…continued #include int main(void) { int x=5; int y, div, mult, sub; y = 1; mult = x * y; printf(“mult is %d\n”, mult); mult = mult/100; printf(“mult #2 is %d\n”, mult); div = x/mult; printf(“div is %d\n”, div); sub = div - mult; printf(“sub is %d\n”, sub); } The output is: mult is 5 mult #2 is 0 Floating point exception So, the problem begins with the div = x/mult You are dividing by 0
Will these print the same value? #include int getLength(); int computeSQR(int); int main(void) { printf("%d\n", computeSQR(getLength())); return 0; } int getLength() { return 5; } int computeSQR(int a) { return a*a; } #include int getLength(); int computeSQR(int a); int main(void) { int length, squareLength; length = getLength(); squareLength = computeSQR(length); printf("%d\n", squareLength); return 0; } int getLength() { return 5; } int computeSQR(int a) { return a*a; } YES – it prints out 25
What is the output? #include int doSomething(int, int); int main(void) { int a = 5, b =2, c; c = doSomething(a,b); printf(“In Main: a=%d, b=%d, c=%d\n", a, b, c); return 0; } int doSomething(int a, int b) { a = 10; b = 12; printf("In Function: a=%d, b=%d\n", a, b); return a+b; } 1. In Function: a=10, b=12 In Main: a=5, b=2, c=22 2. In Function: a=10, b=12 In Main: a=10, b=12, c=22 3. In Function: a=5, b=2 In Main: a=5, b=2, c=22 4. In Function: a=10, b=12 In Main: a=5, b=2, c=7