Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 04-26-2009, 09:41 PM   #1
Folding For PCMech
 
Join Date: Jun 2003
Location: San Dimas, CA
Posts: 3,136
Visual Basic program help

I am taking a class on computer programming that is required in the Civil Engineering department at my school. So far, we have only been learning Visual Basic, and I am working on a project to solve Quadratic Equations. The basic logic of the program is to take a quadratic equation typed in by the user in the form: ax^2 + bx + c and when the enter key is hit, to retrieve the a, b, and c values and then calculate the roots when the calculate button is pressed.

Here is a visual of the program form.

I have already got this part down, but the professor is offering "bonus" points for a few things that I am having some trouble with.

The first one being: an equation in which b^2 - 4ac < 0

Code:
...
dim i as double
i = math.sqrt(-1)
chk = ((val_b) ^ 2) - (4 * val_a * val_c)
...
ElseIf chk < 0 Then
            x1val.Text = (-val_b / (2 * val_a)) + ((Math.Abs(chk) * (i) / (2 * val_a)))
            x2val.Text = (-val_b / (2 * val_a)) - ((Math.Abs(chk) * (i) / (2 * val_a)))
...
But I have obviously either made a mistake or Visual Studio cannot handle the square root of -1.

The other is to make the program able to hand either x or X when typing in the equation. I have tried using If-Then statements and Select Case statements, but I cannot get it to work.

Here is what I have for getting the a, b, c values from the entered equation:

Code:
        Dim funcexp As String, pos1 As Integer, pos2 As Integer, pos3 As Integer
        funcexp = quadval.Text
        If Asc(e.KeyChar) = 13 Then
            pos1 = InStr(funcexp, "x^2")
            aval.Text = Mid(funcexp, 1, (pos1) - 1)
            pos2 = InStr((pos1) + 3, funcexp, "x")
            bval.Text = Mid(funcexp, (pos1) + 3, (((pos2)) - ((pos1) + 3)))
            pos3 = InStr((pos2) + 1, funcexp)
            cval.Text = Mid(funcexp, (pos2) + 1)
        End If
Any help would be great, and if you notice any problems with my code so far, let me know as well.

Thanks,
Andrew
bigandy is offline   Reply With Quote
Old 04-27-2009, 02:29 AM   #2
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,806
I'm not familiar with VB specifically, but in most other languages, there is a common function you need to use to raise a number by a power--the carrot symbol is not recognized as a math symbol

Usually it's pow(baseval, exponentval)

In VB, it might be: math.pow(baseval, exponentval)
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
Force Flow is offline   Reply With Quote
Old 04-27-2009, 09:27 AM   #3
Folding For PCMech
 
Join Date: Jun 2003
Location: San Dimas, CA
Posts: 3,136
We have been using the caret to denote exponents so far in the class, so I don't think that's the problem.
bigandy is offline   Reply With Quote
Old 04-27-2009, 12:09 PM   #4
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,806
Looks like you're right. This is yet another reason why I think VB should fade out of existance


I just punched sqrt(-1) on my scientific calculator, and yes, it throws a math error. Why were you trying to get the square root of -1 anyway? I'm not seeing how that makes sense in your else statement

[edit]: now that I think of it, doesn't the square root of any negative number become an imaginary number?
Force Flow is offline   Reply With Quote
Old 04-27-2009, 01:10 PM   #5
glc
Forum Administrator
Staff
Premium Member
 
glc's Avatar
 
Join Date: May 2000
Location: Joplin MO
Posts: 41,189
Square root of a negative number is mathematically impossible. Perhaps the professor is leading you on a wild goose chase to make you think.
glc is offline   Reply With Quote
Old 04-27-2009, 01:35 PM   #6
Folding For PCMech
 
Join Date: Jun 2003
Location: San Dimas, CA
Posts: 3,136
I know, but when dealing with complex/imaginary numbers, i is the square root of -1 (x^2 +1 = 0).

I was seeing if it was possible for the computer to do it, though.

However, I think I have figured out a solution. I will simply have the computer do the math (-b/2a +/- sqrt(abs(b^2-4ac))) and convert the results to a string and then tag "i" on to the end of it when it outputs the answer to the textbox. It will then be responsibility of "the user" to figure it out.


Do you guys (or anyone else) have any suggestions as far as the x vs. X situatioin goes? I would like to be able to have both the lower case and upper case x be available for input.
bigandy is offline   Reply With Quote
Old 04-27-2009, 02:11 PM   #7
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,806
Quote:
I was seeing if it was possible for the computer to do it, though.
It is, but not with the simple math being used here.


Very simply, you can notate if the number is imanginary by a simple if-statement (or whatever the syntax is in VB) and evaluate whatever you are trying to find the square root of:
Code:
if((b^2-4ac)<0){
  //the number is imaginary
}

[edit]: As for capital letters vs lower case letters, there's usually a function in most languages that allows you to take strings and convert them to upper case, lower case (plus few other capitalization functions). For example, tolowercase(mystring) or touppercase(mystring). You'll have to see what the equivalent for VB is.

Last edited by Force Flow; 04-27-2009 at 02:15 PM.
Force Flow is offline   Reply With Quote
Old 04-30-2009, 12:40 PM   #8
Folding For PCMech
 
Join Date: Jun 2003
Location: San Dimas, CA
Posts: 3,136
I forgot to post back the other day. I was able to convert the upper case to lower case by using LCase()--in case anyone every references this thread.

Thanks for your help, guys.
bigandy is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are On



All times are GMT -5. The time now is 07:57 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.1