Thursday, February 17, 2011

Decision Making and Branching

A set of two linear equations with two unknown x1 and x2 is given below:
ax1 + bx2 = m
cx1 + dx2 = n
The set has a unique solution
X1 = (md - bn) / (ad - cb)
X2 = (na - mc) / (ad - cb)
provided the denominator ad - cb is not equal to zero.
Write a program that will read the values of constants a,b,c,d,m and n and compute the values of X1 and X2. An appropriate message should be printed if ad - cb = 0.

#include <stdio.h>
void main()
{
float a,b,c,d,m,n,ad_cd;
double x1,x2;
clrscr();
printf("Enter the value of a,b,c,d,m and n to get the value of x1 and x2:");
scanf("%f %f %f %f %f %f",&a,&b,&c,&d,&m,&n);
ad_cd=(a*d)-(c*b);
if(ad_cd==0)
printf("Solution is not available");
else
x1=(m*d-b*n)/ad_cd;
x2=(n*a-m*c)/ad_cd;
printf("x1 = %.2lf\nx2 = %.2lf",x1,x2);
getch();
}

No comments:

Post a Comment