Seems to me you can't actually solve the equation without some values. Therefore, ask your instructor for more details.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I find that C++ textbooks in general completely ignore the topic of command line arguments, so I won't tell you to look in the book.
Here are a few links that may help you to understand how to do this:
http://www.cprogramming.com/tutorial/lesson14.html
http://malun1.mala.bc.ca:8080/~wesselsd/csci161/notes/args.html
The short version is that argv[] is an array of C-style strings (char array based, null terminated) where the first (index 0) is the program name.
You need to check the next string to verify it is "-r" before your program continues to then read the a, b, c values from the remaining strings.
Val
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
It would help if you would post the code you have. As I understand what you're trying to do, you will have to treat your inputs from the command line as separate arguments ( argv[2], argv[3] and argv[4]). Each space delimited sequence on the command line is a separate argument.
And keep in mind, these inputs from the command line are strings. You will have to convert them to the numeric values they represent. If you are using only integer values, the the atoi( ) function will be handy.
Val
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
1. The problem I mentioned before about the values a, b and c.
2. Does the line if placed in bold make any sense?
1 - Consider what the argv array of strings looks like if I enter the following on the command line:
C:> find_roots -r 3 15 9
argv[0] = "find_roots"
argv[1] = "-r"
argv[2] = "3"
argv[3] = "15"
argv[4] = "9"
Each of these is a C-style string- an array of characters have have a NULL terminator ( '\0' ) after the content (that is, argv[1] has content '-' 'r' '\0' )
If you want to put the three number arguments into your coefficient array (integers) you will need to convert each of the strings to the numeric value they represent. The atoi( ) function can do this for you (look it up.) Once you've placed the values in the array, be sure that you pass it as a parameter to your function.
2 No, this line makes no sense. In the first place, you should not change what's in your argv[] array strings - you never know how big the arrays are that hold the data. And the line, as you wrote, doesn't seem like it would do anything approaching what you want. Rather, once you've put the argument values into the coefficient array, simply call your function, like root_quadratic( coefficient, r1, r2 ); Structure your function so that the roots you seek are placed in the r1 and r2 parameters.
And, for completeness sake, you might have the function return a value that indicates if it could successfully find roots. What happens if the value of A is 0? Or if the discriminant is negative?
When it comes to dealing with imaginary numbers, I usually leave that to my imaginary friend.
Val
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
do you mean the user is supposed to enter something like:
c:>root_finder -r 3x^2+5x-3=9
Oh wow, is that a problem of a different color. Sounds like something more advanced that where it appears you are in learning to program, so far.
Please clarify.
Val
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
This is an added level of complexity, an added step to your problem. However, everything we've discussed previously still applies. Your root finding function should still work as I proposed before.
Now you need a function to extract the a, b, c values from a string. Here we need first to ensure how the string is entered at the command line.
If the user enters the expression all packed together, as in 3x^2+9x-5, we will get the entire string as the argv[2]. If the user enters any spaces, you get more argv[] strings. If the user surrounds the expression with double quotes, we get one string regardless of spacing, such as "3x^2 + 9x - 5"
Your problem, in a separate string parsing function, is to read digits (and leading negative sign, if present) till you find the first x, convert them to the A value. Skip charaters till you find the + or -, then find digit characters again till x, convert and store to B value. Again skip characters till you find + or -, read final digit characters. Now convert to the C value. And away you go.
I'll leave it to someone more mathematically minded to help with coding the calculus.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
In a test program write this:
cout << argv[2] << endl;
and see what happens. argv[2] IS the equation in the form a C style null terminated string. You can copy this to another C style string using strcpy() or you could initialize another C style string with argv[2] if you don't like using that name for the equation. Alternatively, I believe you can use argv[2] to initialize an STL string if you want, and you may be able to assign argv[2] to an STL string, though I'm not as sure about that one.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
OK, my approach to setting up your program would be something like this:
if( argc == 3 ) //testing enough args were entered at command line
if( strcmp( argv[1], "-r" ) == 0 ) //we're looking for roots
{
extract_coeffs( coeff_arr, argv[2] ); //parse the string,put a, b, c into array
if( root_finder( coeff_array, r1, r2 ) ) //could real roots be found
display_roots( argv[2], r1, r2 );
else
//display some error message
}
where extract_coeffs( ) parses the argv[2] string to get the numeric values of a, b, c
and where root_finder solves for the roots, if the values a, b, c will provide for such solution. It returns true if successful, false if not.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
>>return false if the characters entered are not valid
Validating input in this setting is not straightforward.
>>How do I place the vector values into an array?
A loop to assign each char in the vector into the array one by one is the usual way. The big question is why bother doing this?
To parse out the a, b, and c from input like this:
ax^2+bx+c
I'd first declare three string variables, string1, string2, string3. Then I'd read the input string char by char putting each char read up to but not including the first alphabetical char into string1. Then I'd put all characters from the 3rd char after the first alphabetical char up to but not including the second alphabetical char into string2. Then I'd read the rest of the input string into string3. I'd then look at the first element of each string and if it was a + sign, I'd remove it. Finally, I'd convert string1, string2 and string3 into int variables called a, b, c.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
sounds pretty much the same as my description except using the Standard Template Library (with find_if, iterators, and vectors) instead of basic C/C++ style processes like if statements, [] operators and an array (if you don't want to use individual variables for the three strings developed). If it gets where you want to go without too many distractions, go for it.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396