CS133 Lab 3


Track 1

Write a program that reads three integers, then outputs the integers in ascending order. Here is a sample run:

Enter three numbers: 25 14 20
the numbers, in ascending order, are: 14 20 25
 

Be sure to test your code thoroughly and try all possible combinations. To give you a start, here's the first half of the logic.

printf("The numbers, in ascending order, are: ");
if (a < b)
    if (a < c)
        // a is the smallest
        if (b < c)
            printf("%d %d %d\n", a, b, c);
        else
            printf("%d %d %d\n", a, c, b);
    else
        // c is the smallest, and a < b
        printf("%d %d %d\n", c, a, b);
else
    ...

Track 2

Write a program that determines the roots of a quadratic equation. Assume the coefficients are integers, but display each root as a floating point number with two decimal places. To take the square root of a number, use the sqrt function in the standard library.

 #include <math.h>
 float a, b;
 a = (float)sqrt(b);
 
This code takes the square root of b and assigns it to a. The sqrt function returns the square root as a double-precision floating point number. Cast it to a float to convert it to single precision and eliminate compiler warnings. Your output should be similar to the following:

Input coefficients to a quadratic equation: 2 1 -6
The roots of 2x^2 +1x -6 are 1.50 and -2.00
   

Here's another run that illustrates imaginary roots:

Input coefficients to a quadratic equation: 4 8 5
The roots of 4x^2 +8x +5 are (-1.00 +0.50i) and (-1.00 -0.50i)
   

And another run that illustrates equal roots:

Input coefficients to a quadratic equation: 1 10 25
The roots of 1x^2 +10x +25 is -5.00
   

You can use the "+" format specification to force the sign to print. For example:

printf("%d", 5);         // prints 5
printf("%+d", 5);        // prints +5
printf("%+d", -5);       // prints -5

Be sure your code follows the suggestions in the C Style Guide.