Open Side Menu Go to the Top
Register
First Programming Class: C++ First Programming Class: C++

04-13-2013 , 12:03 PM
Why is the following NOT true?

"An input statement can store the value of an arbitrary expression into a variable."

I would have thought that if you prompt user for input that you've programmed to store to a variable it could be anything from "fjkdfjdokas" to "5" and all points in between.
First Programming Class: C++ Quote
04-13-2013 , 01:43 PM
Quote:
Originally Posted by Acemanhattan
Why is the following NOT true?

"An input statement can store the value of an arbitrary expression into a variable."

I would have thought that if you prompt user for input that you've programmed to store to a variable it could be anything from "fjkdfjdokas" to "5" and all points in between.
Most types can't store arbitrary values. Example. If your code looks like this:

Code:
char x;
std::cin >> x;
std::cout << x;
And you input "fjkdfjdokas", what happens? What if x is a double instead of a char? What if x is a std::string?
First Programming Class: C++ Quote
04-13-2013 , 02:42 PM
Quote:
Originally Posted by meekrab
Most types can't store arbitrary values. Example. If your code looks like this:

Code:
char x;
std::cin >> x;
std::cout << x;
And you input "fjkdfjdokas", what happens? What if x is a double instead of a char? What if x is a std::string?
Okay, that makes sense. It's not arbitrary in the sense that they can only store values consistent with their type.

Thanks.
First Programming Class: C++ Quote
04-13-2013 , 04:39 PM
There's also a max and min value for each variable type based on the size of the variable in memory.
First Programming Class: C++ Quote
04-13-2013 , 05:22 PM
Quote:
Originally Posted by WishICouldLAG
You're dividing by zero somewhere.

See this link: http://stackoverflow.com/questions/8...error-1-j-mean
From that link:

Quote:
Quote:
Interesting... I wonder why it ends up with a 'J' when truncating the infinity/NaN indicators?
The J is the result of rounding the "digits" IN to one less place.
First Programming Class: C++ Quote
04-17-2013 , 12:17 AM
I am writing a simultaneous solver that will solve for x and y when given two equations in the form of Ax+By=C. The problem I am having is the case when the two equations are dependent equations. For example if I input

1x + 2y =3
and
2x +4y =6

It evaluates to the condition where the lines are parallel. Would someone take a look at my conditional statements there and direct me how I might make this work as I'd like it to?

HTML Code:
// This is the Simultaneous Equation Solver program (SimultaneousSolver.cpp)
// This program will solve a pair of linear equations in two variables x and y
// Written by: Chase Banta
// Date: 11 April 2013
// Sources: None

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main() 
{
	
	// Here we declare our variables.  I am declaring everything
	// doubles so that my final x and y values will display with
	// decimals. Code will be used to make sure the doubles appear
	// as ints where the assignment instructions indicate they should.
	double a1; 
	double b1;
	double c1;
	double a2; 
	double b2; 
	double c2;
	double x; 
	double y;
	bool a;
	bool b;
	bool c;

	// This section fixes the float value of our 
	//...numbers to the value in 'setprecision'
	cout << fixed << setprecision(2);
	
	// This section creates dialog between the user and the computer
	//...where the user inputs values for the solver to work on
	cout << "Welcome to Chase's Simultaneous Equation Solver." << endl;
	cout << "Please enter a1, b1, c1 (separated by spaces): ";
	cin >> a1 >> b1 >> c1; 
	cout << "Please enter a2, b2, c2 (separated by spaces): ";
	cin >> a2 >> b2 >> c2;
	cout << "The solution of the equations:" << endl;
	
	// Using static_cast below allows us to have an output that is appropriate for
	//...integers per the examples on the instructions.
	cout << static_cast<int>(a1) << "x + " << static_cast<int>(b1) << "y = " << static_cast<int>(c1) << endl; 
	cout << static_cast<int>(a2) << "x + " << static_cast<int>(b2) << "y = " << static_cast<int>(c2) << endl;

	// Arithmetic for Cramer's rule
	x = (c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1) ;  
	y = (a1 * c2 - a2 * c1) / (a1 * b2 - a2 * b1) ;

	// Below I have created conditional statements and assigned
	// them to the values of a b c. I opted to do this because it
	// made it easier for me to organize the last "if" statement

	// a is the case where at least one of the users string of inputs is 0 0 0 and two lines do not exist. 
	a = ((a1 == b1) && (b1 == c1) && (a1 == 0) || (a2 == b2) && (b2 == c2) && (a2 == 0));
	// b is the case where the lines are parallel.
	b = ((a1 * b2 - a2 * b1) == 0) && (a == false) && (((a1 == a2) && (b1 == b2) && (c1 == c2)) == false);
	//c is the case where the lines are the same.
	c = (a1 == a2) && (b1 == b2) && (c1 == c2) && (a1 != 0) && (b1 != 0) && (c1 != 0);
	
	// appropriate outputs based on how the conditional statments evaluate
	if (a)
	{
		cout << "is nonexistent because you have not entered values that create two lines." << endl;
	}
	if  (b)
	{
		cout << "is nonexistent because the lines are parallel." << endl;
	}
	if (c)
	{
		cout << "exists at infinitely many points because the lines are the same." << endl;
	}
	if ( (a == false) && (b == false ) && (c == false))  
	{ 
	cout << "is x = " << x << " and y = " << y << endl;
	}
	cout << "Thank you for using Chase's Simultaneous Equation Solver.";

	// This section ensures the program doesn't exit until the user is ready
	getchar();
	getchar();
	return 0;
}
First Programming Class: C++ Quote
04-17-2013 , 12:30 AM
Since this is always 2 equations 2 unknowns, I'd just start with checking if the determinant = 0 (this is both a necessary and sufficient condition for dependent equations) and make that a guard clause. What are you supposed to output in that case, given that there are infinite solutions?
First Programming Class: C++ Quote
04-17-2013 , 12:32 AM
Did you paste all the code?
First Programming Class: C++ Quote
04-17-2013 , 12:34 AM
Never mind phone app doesn't display right
First Programming Class: C++ Quote
04-17-2013 , 12:35 AM
I am supposed to output something to the effect that there are an infinite number of solutions.

I can read the wiki, though it looks a little complex given that I've never taken linear algebra, but is there a simple explanation to what the determinant is?

edit: just saw that you linked the determinant page. Is my case the 2 x 2 matrices case? so if my inputs for x and y are a1 b1 and a2 b2 then my determinant is a1b2-b1a2 ?
First Programming Class: C++ Quote
04-17-2013 , 12:42 AM
So if my determinant is what I think it is, won't it also be zero in the case that my lines are parallel?
First Programming Class: C++ Quote
04-17-2013 , 12:44 AM
If you haven't done any linear algebra, and the very first paragraph on that wikipedia article doesn't either make sense or make you curious enough to read more, don't worry about it. It's one of those math things that comes in very handy for a lot of things but doesn't have much intuitive meaning outside of math. Kind of like a factorial.

Calculating it in the specific case of a 2x2 matrix is actually brain-dead simple. In fact, you're already doing it in the denominator of your x and y expressions (EDIT: which you've noticed, I realize after rereading your post). This means you're currently dividing by zero in these cases, if you haven't already noticed!

Also, you should be aware it's also possible for there to be no solutions. Those instances will also fall under the determinant = 0 case. EDIT: I see we simulposted, but yes, it's also 0 if the lines are parallel.

EDIT2: I just realized that finding the difference between 0 solutions and infinite solutions is actually not as trivial as I thought given that dividing can result in some floating point rounding error. Luckily, it's entirely possible to keep everything as integers and get the right answer using multiplication and comparison only. The idea is to get the equations in the form:

ax + by = c1
ax + by = c2

Last edited by Xhad; 04-17-2013 at 12:57 AM.
First Programming Class: C++ Quote
04-17-2013 , 12:57 AM
Quote:
Originally Posted by Xhad
If you haven't done any linear algebra, and the very first paragraph on that wikipedia article doesn't either make sense or make you curious enough to read more, don't worry about it. It's one of those math things that comes in very handy for a lot of things but doesn't have much intuitive meaning outside of math. Kind of like a factorial.

Calculating it in the specific case of a 2x2 matrix is actually brain-dead simple. In fact, you're already doing it in the denominator of your x and y expressions. This means you're currently dividing by zero in these cases, if you haven't already noticed!

Also, you should be aware it's also possible for there to be no solutions. Those instances will also fall under the determinant = 0 case. EDIT: I see we simulposted, but yes, it's also 0 if the lines are parallel.
So would the following adequately differentiate the two cases

Quote:
if (determinant == 0 && ( b1/a1) == (b2/a2) )
{
cout << "the lines are the same"
}
if (determinant == 0)
{
cout<< "lines are parallel"
edit: no, that wont do it, they would have to have the same slope AND same intercept. Not just slope.
First Programming Class: C++ Quote
04-17-2013 , 01:02 AM
Actually the float thing deserves its own post now that i look at it again: does the assignment specify that you have to take floats as arguments? That makes it a lot harder to do this at all accurately, to the point that the mere existence of this assignment annoys me.

If you're allowed to assume integer input, I'd do so and only cast to floats once I have to start dividing. Otherwise you're going to get determinant values of "really tiny" when they should be zero, and craziness results.

EDIT: Here's an example of what i mean. This is in python but C/C++ is no better:

Code:
>> print('{:.3}'.format(7/3-5/3-2/3)
1.1e-6
http://docs.oracle.com/cd/E19957-01/..._goldberg.html
First Programming Class: C++ Quote
04-17-2013 , 01:05 AM
http://www.algebra.com/algebra/homew...on.169955.html

The second answer goes over finding our the difference between same and parallel; without determinant.
First Programming Class: C++ Quote
04-17-2013 , 01:07 AM
Quote:
Originally Posted by Xhad
Actually the float thing deserves its own post now that i look at it again: does the assignment specify that you have to take floats as arguments? That makes it a lot harder to do this at all accurately, to the point that the mere existence of this assignment annoys me.

If you're allowed to assume integer input, I'd do so and only cast to floats once I have to start dividing. Otherwise you're going to get determinant values of "really tiny" when they should be zero, and craziness results.
Luckily we are able to assume integer inputs but the outputs must be displayed with precision up to two decimal points.
First Programming Class: C++ Quote
04-17-2013 , 01:15 AM
ok, good. Then my advice stands; take integer input, keep them as integers for as long as possible, cast to float/double only when you absolutely have to.
First Programming Class: C++ Quote
04-17-2013 , 01:49 AM
Quote:
Originally Posted by kwackbars
http://www.algebra.com/algebra/homew...on.169955.html

The second answer goes over finding our the difference between same and parallel; without determinant.
This is a good explanation for solving this problem by hand, but when using a program it doesn't help as much as you'd like; finding a determinant of 0 already proves the slopes are the same. In fact, trying to calculate the slope just gets another opportunity for a divide by zero error when the user inputs vertical lines. As to seeing if the intercepts match, this doesn't really suggest an algorithm to do so.
First Programming Class: C++ Quote
04-17-2013 , 02:26 AM
Quote:
Originally Posted by Xhad
This is a good explanation for solving this problem by hand, but when using a program it doesn't help as much as you'd like; finding a determinant of 0 already proves the slopes are the same. In fact, trying to calculate the slope just gets another opportunity for a divide by zero error when the user inputs vertical lines. As to seeing if the intercepts match, this doesn't really suggest an algorithm to do so.
The part below solves the standard form and does not use the slope. It suggests comparing a/b and c/b. I would add the zero checking after getting this to work first.
First Programming Class: C++ Quote
04-17-2013 , 04:48 AM
Quote:
Originally Posted by Xhad
If you haven't done any linear algebra, and the very first paragraph on that wikipedia article doesn't either make sense or make you curious enough to read more, don't worry about it. It's one of those math things that comes in very handy for a lot of things but doesn't have much intuitive meaning outside of math. Kind of like a factorial.
n! is the number of ways that you can put n things in order. Doesn't get much more natural than that in math.
First Programming Class: C++ Quote
04-17-2013 , 08:21 AM
Quote:
Originally Posted by kwackbars
The part below solves the standard form and does not use the slope. It suggests comparing a/b and c/b. I would add the zero checking after getting this to work first.
Still unnecessarily risks division by zero. I've already hinted at a straightforward way to do it without any dividing.

Quote:
n! is the number of ways that you can put n things in order. Doesn't get much more natural than that in math.
Blast. I was even thinking specifically of combinatorics when I typed that and was apparently sleepy enough to forget that particular case. I had several other examples come to mind but they're all complicated enough not to make the point any clearer. The original advice to forget about it for now and take a linear algebra course later still stands.
First Programming Class: C++ Quote
04-17-2013 , 08:23 AM
Quote:
Originally Posted by kwackbars
The part below solves the standard form and does not use the slope. It suggests comparing a/b and c/b. I would add the zero checking after getting this to work first.
So before zero checking, something like:

HTML Code:
if (a1/b1 == a2/b2 && c1/b1 == c2/b2) {
     cout << "something about the same lines";
}
if (a1/b1 == a2/b2 && c1/b1 != c2/b2) {
     cout "something about parallel lines"
}
So then I suppose I have to think about what to do in the case when either b1 or b2 is 0 ?
First Programming Class: C++ Quote
04-17-2013 , 08:30 AM
Quote:
Originally Posted by Xhad
Still unnecessarily risks division by zero. I've already hinted at a straightforward way to do it without any dividing.
Do you mean when you stated this:

Quote:
The idea is to get the equations in the form:

ax + by = c1
ax + by = c2
I'm not sure what that was hinting at.

My current code makes use of the scenario where the determinant is zero, but I found I had to add a lot more "catches" to make sure that it returned the right output, and I'm still left with the dependent solution problem.

I just woke up though, and last night I was tired, so I could just be being mentally lazy. I'll think about it a bit more. Though, again, I didn't catch what you are hinting at above since I'm already working with the equation in standard form. Also I re-read and didn't see where you suggested a method that avoids division.
First Programming Class: C++ Quote
04-17-2013 , 08:32 AM
Quote:
Originally Posted by Xhad
Blast. I was even thinking specifically of combinatorics when I typed that and was apparently sleepy enough to forget that particular case. I had several other examples come to mind but they're all complicated enough not to make the point any clearer. The original advice to forget about it for now and take a linear algebra course later still stands.
Linear algebra in 9 weeks
First Programming Class: C++ Quote
04-17-2013 , 12:14 PM
Quote:
Originally Posted by Xhad
Blast. I was even thinking specifically of combinatorics when I typed that and was apparently sleepy enough to forget that particular case. I had several other examples come to mind but they're all complicated enough not to make the point any clearer. The original advice to forget about it for now and take a linear algebra course later still stands.
Maybe complex numbers? Useful, but no easy layman's interpretation.

Reminds me of the quote: "God made the integers; all else is the work of man"
First Programming Class: C++ Quote

      
m