a rather simple calculation problem

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
spyroware
Newbie
Posts: 1
Joined: Mon Aug 07, 2006 10:37 am

a rather simple calculation problem

Post by spyroware » Mon Aug 07, 2006 10:48 am

I am very newbie to qbasic and I have a tiny programme to make
I want a simple calculation, the type of:

input a
input b
a/(b*12)=c
print c

but I cant work it out when I try with the print commands before the inputs

can any kind sould out there help me a bit?
actually the answer to this is giving me the source code but yeah.. _-_"
any help would be very appreciated ^^

User avatar
Quibbler
Coder
Posts: 16
Joined: Tue Jan 24, 2006 7:29 am
Location: Trinidad and Tobago

Post by Quibbler » Mon Aug 07, 2006 11:20 am

You are assigning a value to c so the c comes first

c=....

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Mon Aug 07, 2006 8:01 pm

Quibbler is right, the statement should be:
c=a/(b*12)

When you put someting into a variable, it's called a "variable assignment". You are "assigning" a value to the variable.

All variable assignments are coded with the resultant variable first and then the value that is being assigned to it, like this:
a=1
b=99
c=a*b
d=a/b+100

Get it?

When doing calculations, you should be aware of the variable "types" that you're using. In your sample program, the variables a, b, and c will all be of the type SINGLE, which is single precision floating point, which, in addition to handling larger numbers, allows you to have decimals. The reason they are SINGLE is because you did not specifically define them, and QB automatically will define them as SINGLE by default.

A good practice is to specifically define your varibale types at the top of the program. Example:
DIM a as integer
DIM b as single
DIM c as double

Also, beware of using literal constants in your calculations. Example:
x=365*100

This looks hamless, but it won't give the correct result, because QB will treat 365 as an integer and 100 as an integer. Then, since both of them are integers, it will try to give a result that fits into an integer, which it won't. 365*100 is 36500 which does NOT fit into an integer since the maximum for a positive integer is 32,767. So, the result will be garbage.

Don't kid yourself, this is tricky, and lot's of programmers make the same mistake.

There are many ways to avoid the above problem. One of them is:
dim k as single
k=1
x=365*100*k

When QB see that the k is single, it will produce a result as single. Another way is to define some variable as single, and put the 365 or the 100 into it.

Do some testing on your own to see how this works.

Good luck,
Moneo

nkk_kan
Veteran
Posts: 57
Joined: Thu Jun 01, 2006 10:45 am
Location: Gujrat,India,Asia
Contact:

Post by nkk_kan » Tue Aug 08, 2006 9:10 am

and one more thing...
if you don't define a variable then it's default value will be zero
that is if you do,

print c

then it will input 0...

and one more thing
integers are the fastest variables.So use them whenever you are sure that you know that the value is not going to be more than 32767...
It is helpful when you are using them to put graphics and optimizing the program(speeding it up) is really useful

And you don't always have to DIM them,You can use symbols for them
for example

cool% is an integer and you can tell it just by seeing it due to that % sign..
cool! is single precision number..
cool& long integer..
and its all described in help

one *last* thing :D

you can also use SIN(),COS(),TAN() functions in your programs
look in help -> index
they are helpful...

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo » Wed Aug 09, 2006 8:18 pm

nkk_kan wrote:......
And you don't always have to DIM them,You can use symbols for them
for example

cool% is an integer and you can tell it just by seeing it due to that % sign..
cool! is single precision number..
cool& long integer..
and its all described in help
What you call symbols are actually called Type-Declaration Suffixes.
Yes, they work, but they are poor programming practice. It's too easy to code a mistaken suffix, and be referring to a different variable. Example: I have a variable total&, but later in the program I code total%. Good 'ol QB will use total% even if it was never defined, giving me zero.

Also, variable names with suffixes make the code harder to read.

*****

Post Reply