CS1101C Lab 4

The deadline for this lab is Monday 06 October 2008, 23:59:59 hours.

For all of these questions, you are not allowed to create your own functions in your solution. No credit will be given if this rule is violated.

Question 1 (Finding Roots of Quadratic Equations I)

The name of your C program file must be called lab4q1.c, files with any other name will not be marked.

We wish to solve equations of the form ax2 + bx + c = 0 where a, b, c are integers and a is non-zero.

We know that there are three possibilities for the roots; there are two distinct real roots, only one real root, or two distinct complex roots. Determine the correct case and display the roots for the quadratic equation.

Text File

The coefficients of a quadratic equation are stored in a text file. The text file is always called quad1.txt and always consists of a single line of three integers which contain the coefficients a, b, c, in that order. For example, if the text file quad1.txt contains the single line:

-2 7 15

This represents the quadratic equation -2x2 + 7x + 15 = 0 which you are to solve.

Sample Runs

Assuming that the name of the executable file is lab4q1, sample run(s) of the program are shown below. User input is denoted in bold.

Suppose that the text file quad1.txt contains:

-2 7 15

Then the sample run is:

$ ./lab4q1

There are two real roots.
The real roots of (-2)x^2 + (7)x + (15) are:
    -1.500 and 5.000

Suppose that the text file quad1.txt now contains:

1 -2 1

Then the sample run is:

$ ./lab4q1

There is one real root.
The real root of (1)x^2 + (-2)x + (1) is:
    1.000

Suppose finally that the text file quad1.txt now contains:

1 3 3

Then the sample run is:

$ ./lab4q1

There are two complex roots.
The complex roots to (1)x^2 + (3)x + (3) are:
    (-1.500) + (0.866)i and (-1.500) + (-0.866)i

Question 2 (Finding Roots of Quadratic Equations II)

The name of your C program file must be called lab4q2.c, files with any other name will not be marked.

In Question 1, clearly it is very inconvenient to have to edit the text file quad1.txt each time you want to solve a different quadratic equation. Could we put all the quadratic equations we wish to solve into a single file and get our program to solve all of them?

This question is identical to Question 1, except that:

  1. The name of the text file is always called quad2.txt
  2. This text file contains multiple lines (the number of lines is unknown, but there is always at least one line in the file).
  3. Each line contains the coefficients of a quadratic equation to solve.

Sample Runs

Assuming that the name of the executable file is lab4q2, sample run(s) of the program are shown below. User input is denoted in bold.

Suppose that the text file quad2.txt contains:

-2 7 15
1 -2 1
1 3 3

Then the sample run is:

$ ./lab4q2

There are two real roots.
The real roots of (-2)x^2 + (7)x + (15) are:
    -1.500 and 5.000

There is one real root.
The real root of (1)x^2 + (-2)x + (1) is:
    1.000

There are two complex roots.
The complex roots to (1)x^2 + (3)x + (3) are:
    (-1.500) + (0.866)i and (-1.500) + (-0.866)i

$

Note that the text file can contain many more lines (not just three), so your program must work for all text files.

Note and Ponder

  1. Assume that all user input is valid unless otherwise stated.

  2. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  3. You may use vim or nano to create the text files, for example: vim quad1.txt

  4. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 24 times since 25-Jun-24 11:57:13 +08. This is the 2nd time it has been accessed today.

A total of 18 different hosts have accessed this document in the last 444 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 5 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.