(14) Fortran To C-Compatibility-Mode

(14) Fortran To C-Compatibility-Mode - 10/16/2008 1 From...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 10/16/2008 1 From Fortran to C Part 2 Nathan Friedman Quadratic Roots Revisited Here is an example we looked at last lecture 2008 From Fortran to C -- part 2 2 Roots of a Quadratic in C #include <stdio.h> #include <math.h> void main() { float a, b, c; float d; float root1 root2; 2008 From Fortran to C -- part 2 3 float root1, root2; scanf ("%f%f%f", &a, &b, &c); /* continued on next slide */ if (a == 0.0) if (b == 0.0) if (c == 0.0) printf ("All numbers are roots \n"); else printf ("Unsolvable equation"); else printf ("This is a linear form, root = %f\n", -c/b); else { d = b*b - 4.0*a*c ; if (d > 0.0) { d = sqrt (d); root1 = (-b + d)/(2.0 * a) ; 2008 From Fortran to C -- part 2 4 root2 = (-b - d)/(2.0 * a) ; printf ("Roots are %f and %f \n", root1, root2); } else if (d == 0.0) printf ("The repeated root is %f \n", -b/(2.0 * a)); else { printf ("There are no real roots \n"); printf ("The discriminant is %f \n", d); } } } Quadratic Roots Revisited Lets make one slight change to the program: 2008 From Fortran to C -- part 2 5 if (a = 0.0) if (b == 0.0) if (c == 0.0) printf ("All numbers are roots \n"); else printf ("Unsolvable equation"); else printf ("This is a linear form, root = %f\n", -c/b); else { d = b*b - 4.0*a*c ; if (d > 0.0) { d = sqrt (d); root1 = (-b + d)/(2.0 * a) ; 2008 From Fortran to C -- part 2 6 root2 = (-b - d)/(2.0 * a) ; printf ("Roots are %f and %f \n", root1, root2); } else if (d == 0.0) printf ("The repeated root is %f \n", -b/(2.0 * a)); else { printf ("There are no real roots \n"); printf ("The discriminant is %f \n", d); } } } 10/16/2008 2 Quadratic Roots Revisited Can you spot the change? What do you think the effect is? 2008 From Fortran to C -- part 2 7 if (a = 0.0) if (b == 0.0) if (c == 0.0) printf ("All numbers are roots \n"); else printf ("Unsolvable equation"); else printf ("This is a linear form, root = %f\n", -c/b); else { d = b*b - 4.0*a*c ; if (d > 0.0) { d = sqrt (d); root1 = (-b + d)/(2.0 * a) ; 2008 From Fortran to C -- part 2 8 root2 = (-b - d)/(2.0 * a) ; printf ("Roots are %f and %f \n", root1, root2); } else if (d == 0.0) printf ("The repeated root is %f \n", -b/(2.0 * a)); else { printf ("There are no real roots \n"); printf ("The discriminant is %f \n", d); } } } What Happens Here? The equivalent statement in Fortran would cause a syntax error The expression (a = 0 0) is not of type 2008 From Fortran to C -- part 2 9 The expression (a = 0.0) is not of type logical But C does not have a type logical What happens in C? The C Assignment Operator In Fortran, assignment is a statement. In C, assignment is an operator 2008 From Fortran to C -- part 2 10 The C Assignment Operator In Fortran, assignment is a statement....
View Full Document

Page1 / 8

(14) Fortran To C-Compatibility-Mode - 10/16/2008 1 From...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online