CS1101C Lab 2 (Even Week)

Simultaneous Equations

The deadline for this lab question is Wednesday 09 March 2005, 23:59:59 hours.

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

Background

Simultaneous equations with two variables are rather easy to solve. We will look at solving simultaneous equations in the following format:

a1x + b1y = c1
a2x + b2y = c2

For simplicity, we will assume that the coefficients a1, b1, a2, and b2 are always non-zero values. The coefficients c1 and c2 may take zero values. You may assume that all six coefficients are integers. Note that x and y (the solutions to the equations) may not be integers.

The main aim of your program is to solve simultaneous equations. We know that when given a simultaneous equation to solve, there are three possible outcomes:

In addition, we would like to split outcome (1) above into the following two separate outcomes:

Suppose we generate all six coefficients randomly. What is the probability of each of the above four outcomes happening? Since this is difficult to calculate using theoretical formula, we will use computer simulations to determine the simulated probability for each of the four possible outcomes. Allow the user to enter the number of simulations; assume that the number of simulations will never exceed 100,000.

Each simulation consists of generating six random integers which represent the six random coefficients. Allow the user to enter the lower limit and upper limit for the random integer coefficients. Allow the user to also enter the random number seed. Remember that the coefficients a1, b1, a2, and b2 are always non-zero values, while coefficients c1 and c2 may take zero values.

For example, if the lower limit is -2 and the upper limit is 3, then c1 and c2 may take any of the values {-2,-1,0,1,2,3}, but the remaining four coefficients a1, b1, a2, and b2 may only take the values {-2,-1,1,2,3} (exclude the value zero). It is possible that the user may enter both limits as positive, or that the user may enter both limits as negative; in both cases all the six coefficients take the same range of values as specified by the user. Assume that lower limit <= upper limit.

For each simulation, you must solve the random simultaneous equation (if possible) i.e. find values for x and y that solve the random simultaneous equation. We will use the following integer codes for each of the four possible outcomes for each random simultaneous equation:

File Output

Each line of your output file contains every single simulation which contains the six coefficients, the values for x and y, and an integer code which shows the four possible outcomes. The name of your output file must be called eqnout1.txt. The following shows some sample lines in the output file:
0 -2.000 -18.000 -12.000 16.000 15.000 -1.000 -0.767 0.752
1 -5.000 -2.000 -17.000 10.000 5.000 -15.000 23.000 -49.000
-1 4.000 -6.000 7.000 8.000 -12.000 -18.000 0.000 0.000
-2 1.000 2.000 3.000 -6.000 -12.000 -18.000 0.000 0.000

Each line contains nine numbers. The first number (an integer) is the outcome of trying to solve the simultaneous equation (-2, -1, 0, or 1 according to our outcome codes). The next six floating-point numbers (print to three decimal places) represent the coefficients a1, b1, c1, a2, b2, and c2 respectively. The last two numbers (print to three decimal places) are the values of x and y respectively. If the equation has no solutions or has infinitely many solutions (outcome codes -1 and -2), x and y should be set to zero.

The explanation is given for each of the four lines above:

Sample Run

The following shows a sample run, where $ indicates the Unix prompt. It shows that for the seed 4405 with lower limit -20, upper limit 20, and 100000 simulations, there were 97257 simultaneous equations with at least one non-integer solution which occurred 97.257% of the time, 2317 simultaneous equations with both x and y as integer solutions which occurred 2.317% of the time, 417 simultaneous equations with no solutions which occurred 0.417% of the time, and 9 simultaneous equations with infinitely many solutions which occurred 0.009% of the time.

Note that 97257 + 2317 + 417 + 9 = 100000. Print the percentages to three decimal places.

Your result may differ since random numbers generated are system dependent.

You are reminded to follow the sample outcome exactly, else marks will be deducted.

$ gcc -Wall -lm simul.c -o simul
$ ./simul
Enter seed:
4405
Enter lower limit and upper limit:
-20 20
Enter number of simulations:
100000
Non-integer solutions: 97257, 97.257%
Integer solutions: 2317, 2.317%
No solutions: 417, 0.417%
Infinite solutions: 9, 0.009%
$

Tips


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

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

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