

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


CS 1713, Assignment 1, Tue Jan 06 1998, Page 1 of 2
The assignment: Copy the program, compile and run it, and turn in a listing and results of the run. /* Name: Insert your name here
void main(int argc, char argv[]) { double a0, a1, a2; / coefficients in a2 x^2 + a1 x + a0 = 0 / double discriminant; / a1a1 - 4a2a0 / double root1, root2; / the two roots of the equation / / prompt for the coefficients, if not on command line / if (argc == 1) { fprintf(stdout, "Solving a2 x^2 + a1 x + a0 = 0\n"); fprintf(stdout, "Enter a2, a1, a0 (in order): "); scanf("%lf %lf %lf", &a2, &a1, &a0); } else if (argc == 4) { / get coefficients from command line / a2 = atof(argv[1]); a1 = atof(argv[2]); a0 = atof(argv[3]); } else { fprintf(stderr, "%s requires 0 or 3 arguments\n", argv[0]); exit(1); } fprintf(stdout, "Solving %5.3f x^2 + %5.3f x + %5.3f = 0\n", a2, a1, a0); if (a2 == 0.0) { / this is a linear equation (no quadratic term) / fprintf(stderr, "a2 must not be 0 in a quadratic equation\n"); exit(1); } discriminant = a1a1 - 4.0a0a2; if (discriminant < 0.0) { fprintf(stderr, "Roots are complex numbers\n"); exit(1); } / calculate roots and output them / root1 = (-a1 + sqrt(discriminant))/(2.0a2); root2 = (-a1 - sqrt(discriminant))/(2.0*a2); fprintf(stdout, "Roots: %5.3f, %5.3f\n", root1, root2); exit(0); } What to do: ( In the text that follows, boldface is what you should enter at the keyboard.)
CS 1713, Assignment 1, Tue Jan 06 1998, Page 2 of 2
New password: YourNewPassword Re-enter new password: YourNewPassword (Blanks and upper or lower-case letters count in these passwords and elsewhere. Do not forget your new password.) You are now on the system and should get the system prompt: runner%