Share This

Comp116 Lab Exercise Series : Lab 2

This is the lab exercise solution of lab 2 of Comp116 labsheet. The code is written in a simple way. But, it could have been written in a better way to actually take the equations instead of values only.

Question #1

Write a C++ program using function (pass by reference) that calculates the values of x and y from the two linear equations.
ax + by = m
cx + dy = n

The solutions are given as
x = (md - bn)/(ad - cb)
y = (na - mc)/(ad - cb)

The function should take eight arguments and return nothing.


#include <iostream>

using namespace std;

void solvelinear(float &x, float &y, int &a, int &b, int &c, int &d, int &m, int &n)
{
 x = ((m * d) - (b * n))/(float)((a * d) - (c * b));
 y = ((n * a) - (m * c))/(float)((a * d) - (c * b));
}

int main()
{
 float x, y;
 int a, b, c, d, m, n;
 cout << "Enter values for first equation(a b m): ";
 cin >> a >> b >> m;
 cout << "Enter values for second equation(c d n): ";
 cin >> c >> d >> n;
 solvelinear(x, y, a, b, c, d, m, n);
 cout << "Value of X: " << x << "\nValue of Y: " << y << endl;
 return 0;
}

0 comments:

Post a Comment

Followers