Cramer's Rule variable locations

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 861 Views - Last Post: 08 February 2013 - 05:47 PM Rate Topic: -----

#1 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Cramer's Rule variable locations

Posted 07 February 2013 - 08:05 AM

My assignment is to use the Cramer's Rule to understand basic C++ layout for objects and classes. I have a class created and data fields for the variables, constructor for arguments of data fields, and functions for getX, getY, and isSolvable.

My questions is: where does e = (ax + by) and f = (cx + dy) go? In the function getX or getY or in the constructor for e/f or even in main?

double getX()
{
	double x =((e*d)-(b*f)/(a*d)-(b*c));
return x;
}
double getY()
{
	double y = ((a*f)-(e*c)/(a*d)-(b-c));
	return y;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Cramer's Rule variable locations

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • Joined: 25-December 09

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 08:34 AM

Quote

My questions is: where does e = (ax + by) and f = (cx + dy) go? In the function getX or getY or in the constructor for e/f or even in main?


That really depends on how you have designed your class and your program. Without seeing more code, preferably a complete program, there is no way I could tell you where to place your variables and calculations.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 09:09 AM

Please over look the mess, still under construction and my contractor is REALLY new to this. I am currently researching on how this code is written.

class LinearEquation
{
	private:
	double a, b, c, d, e, f;

LinearEquation(double a, double b, double c, double d, double e, double f)
	{
}
bool isSolvable()
{
	if((a*d)-(b*c) != 0){

return true;
	}
	else {
		return false;
} 
}
double getX()
{
	double x =((e*d)-(b*f)/(a*d)-(b*c));
return x;
}
double getY()
{
	double y = ((a*f)-(e*c)/(a*d)-(b-c));
	return y;
}



Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • Joined: 25-December 09

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 09:46 AM

First you may want to consider using vectors or arrays to hold your "Matrix". Cramer's Rule should work with more than just one size of matrix. You may even want to consider starting simple, maybe start with a Matrix with a size of 2. Then once you have the basic problem solved start working on making the program work for any size matrix.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 10:50 AM

Please don't think that I'm not trying. I read both tutorials for 2 weeks now and still have questions.
We have been working with very basic programs that only involve functions and main. This week he introduced classes and objects but has not covered pointers or vectors.
The assignment wants us to "build a class for a 2x2 system of linear equations" to contain constructors with the arguments for each variable and 6 get functions for each variable, one for isSolvable, getX, and getY.
Is it not redundant to do constructors then functions for variables?

Previously, I would have used a "for" loop to get cin>>variable information from the user (in main) then send it,by function prototype, to a function that would hold the Cramer's Rule equation.
Is this the job of the functions isSolvable, getX, and getY?

I don't understand multi-dimentional arrays well and not sure how to put the equation into an array or if it should go into a class, function, or main??
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • Joined: 25-December 09

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 11:32 AM

Quote

I read both tutorials for 2 weeks now and still have questions.

Two tutorials on what subject?

Quote

We have been working with very basic programs that only involve functions and main

Maybe you could post this program, to use as a starting point. Using this as a starting point will probably make creating a class easier.

Quote

The assignment wants us to "build a class for a 2x2 system of linear equations" to contain constructors with the arguments for each variable and 6 get functions for each variable, one for isSolvable, getX, and getY.

Can you post the actual assignment?

Quote

I don't understand multi-dimentional arrays well and not sure how to put the equation into an array or if it should go into a class, function, or main??

My suggestion would be to look for more tutorials on arrays. Here is a link to start: Arrays.


Jim
Was This Post Helpful? 0
  • +
  • -

#7 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 12:58 PM

Under your signature refers to tutorials: Functions in C and C++ parts 1 and 2. In addition to msdn.microsoft.com.(which can be very confusing)

Warning, I have only been programming a couple of months and using C++ a couple of weeks.
#include <iostream>
#include <cmath>
using namespace std;

class LinearEquation
{
	private:
	double a, b, c, d, e, f;

LinearEquation(double a, double b, double c, double d, double e, double f)
	{

}
double getA()
{
	return a;
	
}
double getB()
{
	return b;
}
double getC()
{
	return c;
}
double getD()
{
	return d;
}
double getE()
{
	return e;
}
double getF()
{
	return f;
}
bool isSolvable()
{
	if((a*d)-(b*c) != 0){

return true;
	}
	else {
		return false;
} 
}
double getX()
{
	double x =((e*d)-(b*f)/(a*d)-(b*c));
return x;
}
double getY()
{
	double y = ((a*f)-(e*c)/(a*d)-(b-c));
	return y;
}


int main()
{
	
	double variableArray[6] = {0};

	cout<<"Enter a, b, c, d, e, f: ";
	    for(int i = 0; i < 6; i++){
	       cin >> variableArray[i];

		   LinearEquation (variableArray);

		   if(isSolvable){
			   cout <<"x is "<< x <<" and y is " << y;
		   }
		   else{
			   cout <<"The equation has no solution.";
		   }
		}
			   return 0;
		   }

	
			




The assignment reads:
"Design a class named LinearEquations for a 2x2 system of linear equations: (Cramer's Rule here)
The class contains:
Private data fields a, b, c, d, e, and f.
A constructor with the arguments for a, b, c, d, e, and f.
Six "get" functions for a, b, c, d, e, and f.
A function name isSolvable that returns true if ad - bc is not 0.
Functions getX and getY that return the solution for the equation."

Is it not redundant to use a constructor and then functions just to get the variables a - f ??
Why do I need 2 functions(getX and getY) to get 1 solution?
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • Joined: 25-December 09

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 02:20 PM

Quote

Is it not redundant to use a constructor and then functions just to get the variables a - f ??

Not really, the constructor will "set" the values. The getters will return the value held in the member variables.

Quote

Why do I need 2 functions(getX and getY) to get 1 solution?


Probably so you can solve for either X or Y. You may want to review your documentation for Cramer's Rule and for a step by step explanation with pictures: Cramer's rule.

Also you may want to study the following links about Classes. Classes I and Classes II.


Jim
Was This Post Helpful? 0
  • +
  • -

#9 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 02:58 PM

AAAAHHHH! I think I broke it!

I was just trying to get the variableArray[] into the functions.

Is this fixable?? Never had "symbol" problem before but I would like to give this program a "symbol".
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
error LNK1120: 1 unresolved externals


	double getE()
	{
		e =((a*x) + (b*y));
		return e;
	}
	double getF()
	{
		f = ((c*x) + (d*y));
		return f;
	}
	bool isSolvable(double a, double b, double c, double d)
	{
		if((a*d)-(b*c) != 0){
			return true;
		}
		else {
			return false;
		} 
	}
	double getX()
	{
		double x =((e*d)-(b*f)/(a*d)-(b*c));
		return x;
	}
	double getY()
	{
		double y = ((a*f)-(e*c)/(a*d)-(b-c));
		return y;
	};
	int main()
	{
		LinearEquation;

		double variableArray[6] = {0};

		cout<<"Enter a, b, c, d, e, f: ";
		for(int i = 0; i < 6; i++){
			cin >> variableArray[i];

			LinearEquation linear;
			linear.getA();
			linear.getB();
			linear.getC();
			linear.getD();
			linear.getE();
			linear.getF();


			if(isSolvable(a, b, c, d)){
				cout <<"x is "<< (&LinearEquation::getX) <<" and y is " << (&LinearEquation::getY);
			}
			else{(&LinearEquation::isSolvable);{
				cout <<"The equation has no solution.";
			}
			}
			return 0;
		}
	}
};


Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • Joined: 25-December 09

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 03:14 PM

Did you perhaps put your main() inside your class?

Jim
Was This Post Helpful? 0
  • +
  • -

#11 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 03:33 PM

HaHaHa... Sorry, been at this a while and have been rearranging things. Didn't see that. Can't seem to see how to but the user info from array to the functions??
Am I over looking something there??
Currently going through the Classes I and Classes II but not able to fit it into the instructions to have 6 functions? Suggestions?
Was This Post Helpful? 0
  • +
  • -

#12 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 03:59 PM

Made a lot of changes, every time I get more information I try to adjust to code. Problem is that every site or tutorial I search has different samples (even if the logic is the same) they all lay it out different.
How do I know which way to do it especially if there is not an explanation on how to get user input into your class/functions? Some talk about it in a sentence or two, but they don't put it in action.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • Joined: 25-December 09

Re: Cramer's Rule variable locations

Posted 07 February 2013 - 08:57 PM

I normally will get user input outside my classes and pass this information into the class thru functions or as parameters to the constructor. Since your assignment states:

Quote

The assignment wants us to "build a class for a 2x2 system of linear equations" to contain constructors with the arguments for each variable and 6 get functions for each variable, one for isSolvable, getX, and getY.

You will need to get the user input from the user so you can pass this information to your class in the constructor. Also with the way your assignment is worded I doubt you are expected to use any type of array or vector to accomplish the objectives.

It looks like your class should contain four member variables for your "Matrix". You will need a constructor that has four parameters to construct your class with these four values. You will need a function that returns each of these variables to the calling function and you will need a function that will solve for x and one to solve for y. And finally you need one function that will return a bool signifying whether or not these functions can be solved.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 codescout  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-October 12

Re: Cramer's Rule variable locations

Posted 08 February 2013 - 07:47 AM

I built a function for each step and a friend helped me see that in my "for" loop to bring in the variables from the user that I could use cin>>a>>b>>..... I did not see that. Thought I had to use "get" for that, but "get" was already in my functions &LinearEquations::getA();.

My variables in my class are not being read in my main function. Do I need to re-declare them in both main and the class I made?

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • Joined: 25-December 09

Re: Cramer's Rule variable locations

Posted 08 February 2013 - 08:32 AM

Post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2