Click to See Complete Forum and Search --> : Qbasic Help
Ryuk
October 27th, 2008, 07:31 PM
I'm fairly new to programming. I have this problem where AX + B = C and I have to solve for X. I am using QBASIC 4.5 to write this program.
Here is my code.
OPEN "SCRN:" FOR OUTPUT AS #1
COLOR 2, 0
CLS
PRINT #1, TAB(30); "Solving for X"
PRINT #1,
PRINT #1, TAB(20); "Equation"; TAB(55); "X="
PRINT #1, STRING$(80, 207)
EquationProgram:
READ A, B, C
IF A = 9999 THEN END
IF X = (C - B) / A THEN
PRINT #1, TAB(20); A * X + B = C; TAB(55); X
END IF
GOTO EquationProgram
DATA 9999,9999,9999
DATA 3,6,1
DATA 7,10,9
DATA 8,12,11
DATA 9,15,14
DATA 14,19,15
DATA 19,17,14
When I run the program it does not display the equation or the X variable. What am I doing wrong?
TheCPUWizard
October 27th, 2008, 07:38 PM
For starters, look at your FIRST data statement, and also the logic...
Why are you opening "SCRN", instead of just using Print?
Ryuk
October 27th, 2008, 08:12 PM
For starters, look at your FIRST data statement, and also the logic...
Why are you opening "SCRN", instead of just using Print?
I'll be changing it to LPT1 once I'm ready to physically print it.
Ryuk
October 27th, 2008, 08:35 PM
I can't quite figure out what's wrong with my logic. :-(
dglienna
October 27th, 2008, 11:29 PM
You typed this:
A * X + B = C;
and the PC sees it as a calculation, and prints the answer.
You want something like this
"Var A=" & A & ", Var X=" & X
to print out a line with headings
wkiess
October 28th, 2008, 12:20 AM
Your logic problem is that you've placed the end of execution marker data 9999 at the begining of your data stack. Therefore, the first time your code reads the data it finds the end run marker and promptly ends, therefor you never get to see any output.
HTH
Walter
Ryuk
October 28th, 2008, 06:37 PM
You typed this:
A * X + B = C;
and the PC sees it as a calculation, and prints the answer.
You want something like this
"Var A=" & A & ", Var X=" & X
to print out a line with headings
I'm a bit confused with that. Just to clarify, what I'm trying to do is have it display the equation with the data numbers in place of the variables on one side. I want it to list all the equations with their data sets and on the other side I want the X answers displayed in a list.
wkiess
November 2nd, 2008, 07:55 PM
Hi dglienna,
I've had a closer look at your program and have found there to be at least 2 major problems that will result in the program NOT returning any result at all.
The first problem is that you are using a data value of 9999 in variable A to end program execution. As this is the first value that the program reads as it executes, it will end immediately without producing any results.
Next, you test the X variable for a value although you haven't even assigned it a value as yet. I haven't programmed in QBASIC for many years and can't remember what will happen, but I'm assuming that X will have a default value of zero, therefore the comparison of X with (C - B) / A will never return a true result and hence you'll never see any output. You will need to move the line DATA 9999,9999,9999 to the end of the data statements.
Assuming that you wish to solve the equation for the value of X, you'll have to rewrite the equation so that the program can work out what X should be before trying to print the results. This means the equation to solve for X becomes:
X = (C - B) / A
Using the above formula, your program should now calculate the value of X which you can show in your equation:
PRINT #1, TAB(20);"A * X + B = C"; TAB(55); X
NOTE the use of quotation marks around the formula "A * X + B = C". these are intentional. This instructs the program to print the literal text rather than try to evaluate it an expression.
So with all the changes, your program should look something like this:
OPEN "SCRN:" FOR OUTPUT AS #1
COLOR 2, 0
CLS
PRINT #1, TAB(30); "Solving for X"
PRINT #1,
PRINT #1, TAB(20); "Equation"; TAB(55); "X="
PRINT #1, STRING$(80, 207)
EquationProgram:
READ A, B, C
IF A = 9999 THEN END
X = (C - B) / A
PRINT #1, TAB(20); A * X + B = C; TAB(55); X
GOTO EquationProgram
DATA 3,6,1
DATA 7,10,9
DATA 8,12,11
DATA 9,15,14
DATA 14,19,15
DATA 19,17,14
DATA 9999,9999,9999
I hope this makes sense to you.
Regards,
Walter
wkiess
November 2nd, 2008, 07:58 PM
So with all the changes, your program should look something like this:
OPEN "SCRN:" FOR OUTPUT AS #1
COLOR 2, 0
CLS
PRINT #1, TAB(30); "Solving for X"
PRINT #1,
PRINT #1, TAB(20); "Equation"; TAB(55); "X="
PRINT #1, STRING$(80, 207)
EquationProgram:
READ A, B, C
IF A = 9999 THEN END
X = (C - B) / A
PRINT #1, TAB(20); A * X + B = C; TAB(55); X
GOTO EquationProgram
DATA 3,6,1
DATA 7,10,9
DATA 8,12,11
DATA 9,15,14
DATA 14,19,15
DATA 19,17,14
DATA 9999,9999,9999
Sorry, I forgot to add the quotation marks to the PRINT statement. New code below:
OPEN "SCRN:" FOR OUTPUT AS #1
COLOR 2, 0
CLS
PRINT #1, TAB(30); "Solving for X"
PRINT #1,
PRINT #1, TAB(20); "Equation"; TAB(55); "X="
PRINT #1, STRING$(80, 207)
EquationProgram:
READ A, B, C
IF A = 9999 THEN END
X = (C - B) / A
PRINT #1, TAB(20); "A * X + B = C"; TAB(55); X
GOTO EquationProgram
DATA 3,6,1
DATA 7,10,9
DATA 8,12,11
DATA 9,15,14
DATA 14,19,15
DATA 19,17,14
DATA 9999,9999,9999
Cheers,
Walter
dglienna
November 3rd, 2008, 12:53 AM
It's Ryuk's thread, and looked like homework to me, even if it's a few decades too late.
Ryuk
November 7th, 2008, 08:28 PM
Hi dglienna,
I've had a closer look at your program and have found there to be at least 2 major problems that will result in the program NOT returning any result at all.
The first problem is that you are using a data value of 9999 in variable A to end program execution. As this is the first value that the program reads as it executes, it will end immediately without producing any results.
Next, you test the X variable for a value although you haven't even assigned it a value as yet. I haven't programmed in QBASIC for many years and can't remember what will happen, but I'm assuming that X will have a default value of zero, therefore the comparison of X with (C - B) / A will never return a true result and hence you'll never see any output. You will need to move the line DATA 9999,9999,9999 to the end of the data statements.
Assuming that you wish to solve the equation for the value of X, you'll have to rewrite the equation so that the program can work out what X should be before trying to print the results. This means the equation to solve for X becomes:
X = (C - B) / A
Using the above formula, your program should now calculate the value of X which you can show in your equation:
PRINT #1, TAB(20);"A * X + B = C"; TAB(55); X
NOTE the use of quotation marks around the formula "A * X + B = C". these are intentional. This instructs the program to print the literal text rather than try to evaluate it an expression.
So with all the changes, your program should look something like this:
OPEN "SCRN:" FOR OUTPUT AS #1
COLOR 2, 0
CLS
PRINT #1, TAB(30); "Solving for X"
PRINT #1,
PRINT #1, TAB(20); "Equation"; TAB(55); "X="
PRINT #1, STRING$(80, 207)
EquationProgram:
READ A, B, C
IF A = 9999 THEN END
X = (C - B) / A
PRINT #1, TAB(20); A * X + B = C; TAB(55); X
GOTO EquationProgram
DATA 3,6,1
DATA 7,10,9
DATA 8,12,11
DATA 9,15,14
DATA 14,19,15
DATA 19,17,14
DATA 9999,9999,9999
I hope this makes sense to you.
Regards,
Walter
Thanks for posting. I just have one problem. The quotes will have it display the same equation over and over but I'm trying to display the equations with the data sets automatically filling in A, B, and C.
wkiess
November 16th, 2008, 05:09 PM
Hello again,
I'm glad it's starting to make sense for you. If you want to print the actual values for the variable you'll have to change the PRINT statement a little bit. Remember that whatever you put inside the quotes is literal text and will be printed "as is". To print the contents of a variable you just have to specify the name of the variable itself.
Consider this statement:
PRINT "A="; A
This will print the literal text A= followed by the contents of A. In the case of your 1st DATA statement, this would print A=3.
You already know how to string things together to print mixed data, so your print statement is simply a combination of literal text and variables. The formula is A * X + B = C, so all you need to do is write this formula as an expression that will print the contents of the variables instead of the literal text. So the expression derived fromt he formula should look something like this: A;"*";X;"+";B;"=";C. Note how each variable is specified by its name and each operand of the equation is specified as literal text. You can now place the expression into your PRINT statement to print the formula using the values of the variables, right after the point where the X is calculated.
You can, of course, get all fancy in your data print out by using Tabs and Columns and such, but I'll leave that up to you.
HTH
Walter
PS I don't have QBASIC anymore and can't remember the exact syntax, so the concatenation symbol ";" may be incorrect, it could be something like "&" or "+" instead. Check your reference manual.
Ryuk
November 16th, 2008, 06:23 PM
Hello again,
I'm glad it's starting to make sense for you. If you want to print the actual values for the variable you'll have to change the PRINT statement a little bit. Remember that whatever you put inside the quotes is literal text and will be printed "as is". To print the contents of a variable you just have to specify the name of the variable itself.
Consider this statement:
PRINT "A="; A
This will print the literal text A= followed by the contents of A. In the case of your 1st DATA statement, this would print A=3.
You already know how to string things together to print mixed data, so your print statement is simply a combination of literal text and variables. The formula is A * X + B = C, so all you need to do is write this formula as an expression that will print the contents of the variables instead of the literal text. So the expression derived fromt he formula should look something like this: A;"*";X;"+";B;"=";C. Note how each variable is specified by its name and each operand of the equation is specified as literal text. You can now place the expression into your PRINT statement to print the formula using the values of the variables, right after the point where the X is calculated.
You can, of course, get all fancy in your data print out by using Tabs and Columns and such, but I'll leave that up to you.
HTH
Walter
PS I don't have QBASIC anymore and can't remember the exact syntax, so the concatenation symbol ";" may be incorrect, it could be something like "&" or "+" instead. Check your reference manual.
I can't believe I didn't think of that myself. I need to start thinking more when I'm programming. Your advice has helped me a lot. Thanks a lot. :-) Cheers.
wkiess
November 16th, 2008, 06:31 PM
You're very welcome ryuk.
I do wonder though why u r using this ancient form of BASIC. Like I said, I don't have it anymore and can't remember all the syntax specifics. I work in VB6 and FlashBASIC these days which are a little more modern.
Anyway, I hope you're able to complete your project now.
Best Regards, :wave:
Walter
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.