Posted: Sat Feb 19, 2005 7:33 pm Post subject: Binary multiplication...
Another program done, this is the multiplication of binary numbers. Once again there are no shortcuts taken, I had to ouput to a text file and retrieve the data to do it properly. This stuff is so rewarding
Once again it is not error trapped. Any input other than binary digits will crash it. Also the maximum number to multiply by is 10110, i can add more, i just stopped at 22.
code:
%%%%%%%%%%%%%%%%%%%%%%%%%
%Multiplication %
%Kostia Palanski %
%Mr. Wong %
%Multiplication Program %
%Done: Feb. 19th 2005 %
%%%%%%%%%%%%%%%%%%%%%%%%%
%Set the screen size, colour and font
View.Set ("graphics:580;200,position:bottom;left,nobuttonbar")
drawfillbox (0, 0, maxx, maxy, black)
colorback (black)
color (yellow)
%Variable Declaration
var snum1, snum2, query : string
var streamout, streamin : int
var lnum1, lnum2, maxn : int
var tloop : int := 0
%Procedure - tloop value of the multiplicant
procedure glnum1
loop
get snum2
if snum2 = "0" then
tloop := -1
exit
elsif snum2 = "1" then
tloop := 0
exit
elsif snum2 = "10" then
tloop := 1
exit
elsif snum2 = "11" then
tloop := 2
exit
elsif snum2 = "100" then
tloop := 3
exit
elsif snum2 = "101" then
tloop := 4
exit
elsif snum2 = "110" then
tloop := 5
exit
elsif snum2 = "111" then
tloop := 6
exit
elsif snum2 = "1000" then
tloop := 7
exit
elsif snum2 = "1001" then
tloop := 8
exit
elsif snum2 = "1010" then
tloop := 9
exit
elsif snum2 = "1011" then
tloop := 10
exit
elsif snum2 = "1100" then
tloop := 11
exit
elsif snum2 = "1101" then
tloop := 12
exit
elsif snum2 = "1110" then
tloop := 13
exit
elsif snum2 = "1111" then
tloop := 14
exit
elsif snum2 = "10000" then
tloop := 15
exit
elsif snum2 = "10001" then
tloop := 16
exit
elsif snum2 = "10010" then
tloop := 17
exit
elsif snum2 = "10011" then
tloop := 18
exit
elsif snum2 = "10100" then
tloop := 19
exit
elsif snum2 = "10101" then
tloop := 20
exit
elsif snum2 = "10110" then
tloop := 21
exit
else
put "Multiplicant is too big - Enter another value (max of 10110).."
end if
end loop
end glnum1
%%%%%%%%%%%%%
%Procedure - Addition of the numbers
procedure glnum1Add
%Variables
var add1 : array 1 .. 30 of int
var final : array 1 .. 30 of int
var ofinal : string
var lofinal : int := 0
var fdigit : int := 0
var carry : int := 0
var dif : int := 0
var leadd1 : string := snum1
%If the multlicand is 0 or 1 then skip the loop
if tloop = -1 then
fdigit := 0
elsif tloop = 0 then
fdigit := strint (snum1)
end if
%Addition Section
for x : 1 .. tloop
%Gets value from text file
open : streamin, "output.txt", get
get : streamin, ofinal : *
close : streamin
%Resets certain values
carry := 0
leadd1 := snum1
%Length of received data
lofinal := length (ofinal)
%Difference of received vs original
dif := lofinal - lnum1
%If the different is greater than 0 then add zeros to the front
if dif > 0 then
for y : 1 .. dif
leadd1 := "0" + leadd1
end for
end if
%Variables transfered to arrays
for j : 1 .. lofinal
final (j) := strint (ofinal (j))
end for
for i : 1 .. lofinal
add1 (i) := strint (leadd1 (i))
end for
%%%%%%%%%%%%%%%%%%%%%%%%
for decreasing k : lofinal .. 1
if k = 1 and final (k) + add1 (k) = 0 and carry = 0 then
fdigit := 0
elsif k = 1 and final (k) + add1 (k) = 1 and carry = 0 then
fdigit := 1
elsif k = 1 and final (k) + add1 (k) = 1 and carry = 1 then
fdigit := 10
elsif k = 1 and final (k) + add1 (k) = 2 and carry = 0 then
fdigit := 10
elsif k = 1 and final (k) + add1 (k) = 2 and carry = 1 then
fdigit := 11
end if
%%%%%%%%%%%%%%%%%%%%%%%%
if final (k) + add1 (k) = 0 and carry = 0 then
final (k) := 0
carry := 0
elsif final (k) + add1 (k) = 0 and carry = 1 then
final (k) := 1
carry := 0
elsif final (k) + add1 (k) = 1 and carry = 0 then
final (k) := 1
carry := 0
elsif final (k) + add1 (k) = 1 and carry = 1 then
final (k) := 0
carry := 1
elsif final (k) + add1 (k) = 2 and carry = 0 then
final (k) := 0
carry := 1
elsif final (k) + add1 (k) = 2 and carry = 1 then
final (k) := 1
carry := 1
end if
end for
%Output to text file for next round of addition
open : streamout, "output.txt", put
put : streamout, fdigit ..
if lofinal > 1 and tloop not= -1 and tloop not= 0 then
for t : 2 .. lofinal
put : streamout, final (t) ..
end for
end if
close : streamout
end for
%Put final answer
put "The multiplication these two values produces.."
put fdigit ..
if lofinal > 1 and tloop not= -1 and tloop not= 0 then
for t : 2 .. lofinal
put final (t) ..
end for
end if
end glnum1Add
%%%%%%%%%%%%%
%Beggining of Program
put "Binary Multiplication Program - By Kostia"
put ""
%Get Loop
loop
put "Enter the binary number you want to multiply.."
get snum1
put "Enter the binary number you would like to multiply by (max 10110).."
glnum1
open : streamout, "output.txt", put %this will open a connection to send data
put : streamout, snum1
close : streamout
put ""
put ""
%Repeat program or exit
put "Would you like to calculate another value?"
get query
if query = "y" or query = "yes" or query = "Yes" or query = "Y" then
delay (300)
cls
elsif query = "n" or query = "N" or query = "No" or query = "no" then
exit
else
exit
end if
end loop
%End Program
Posted: Sat Feb 19, 2005 8:21 pm Post subject: (No subject)
I just looked at ur code, seems nice, but is definitely reduceable
liek the last if, u just have to put
code:
else
exit
put nice program
Pa|2a|)oX
Posted: Sat Feb 19, 2005 9:48 pm Post subject: (No subject)
Ya I wasn't looking to reduce, just make it functional. This wasn't an easy thing to do in Turing. Took me a while for both the addition and the multiplication.
Most of the other students in the class had the ability to do it in java, but seeing as I never took that class I don't know Java, so I was stuck with Turing.
Teachers in previous years didn't teach us any of the stuff about arrays or file outputs, and up until a few days ago, I thought there would be no way for me to do these programs in Turing. Luckily I started looking through the Help files and found both the array function and the file output stuff.
Couldn't have done it without that. I am currently in the midst of making a subtraction program for binary, using twos compliment as the backbone, and the addition as usual.
ssr
Posted: Sun Feb 20, 2005 8:19 am Post subject: (No subject)
lol ya, schools....
anyway, I think it is a pretty good program.
person
Posted: Sun Feb 20, 2005 12:01 pm Post subject: (No subject)
this is cool...but its almost the same as ur other progs...shouldnt u have just posted them in the same thread?
Pa|2a|)oX
Posted: Sun Feb 20, 2005 12:32 pm Post subject: (No subject)
I didn't finish them all at one time so I've been posting them seperately.
Sorry.
Andy
Posted: Sun Feb 20, 2005 4:39 pm Post subject: (No subject)
umm, you do realize that the samething can be achieved in less than 20 lines by using strint and intstr right?
Pa|2a|)oX
Posted: Sun Feb 20, 2005 5:59 pm Post subject: (No subject)
This was not meant to use built in shortcuts, but instead to get a full algorithm to understand the complexity of such a task.
It was an assignment, for Grade 12 engineering. We're going to be doing all this crap in assembler next.
Sponsor Sponsor
Andy
Posted: Sun Feb 20, 2005 6:24 pm Post subject: (No subject)
and ur if else structure can be replaced by a simple index and a bit of gr 10 math
Pa|2a|)oX
Posted: Sun Feb 20, 2005 6:36 pm Post subject: (No subject)
Why so negative towards me?
Andy
Posted: Sun Feb 20, 2005 6:41 pm Post subject: (No subject)
what makes you think that? im just saying, for a gr 12, you should be able to program somewhat intelligently, ie make a general case using string manipulation instead of doing it case by case
Pa|2a|)oX
Posted: Sun Feb 20, 2005 7:26 pm Post subject: (No subject)
That's what I mean. Maybe programming isn't my forte, and maybe this is the reason I am using turing instead of a higher programming language.
I came here to get help, I got it, and I made a program that works. I greatly appreciate the help I recieved from both Tony, and Cervantes, among other board members. I am not marketting this program, I am submitting it for marks that I will most certainly obtain.
The fact that I cannot "intelligently" program, by your standards, does not interfere with the fact that this program is functional and serves its purpose to a point.
Who said I was a full fledged programmer? And who stated that every grade 12 should have the ability to program up to YOUR standards?
Nice to have a board representative such as yourself around.
ssr
Posted: Sun Feb 20, 2005 9:15 pm Post subject: (No subject)
Quote:
That's what I mean. Maybe programming isn't my forte, and maybe this is the reason I am using turing instead of a higher programming language.
I came here to get help, I got it, and I made a program that works. I greatly appreciate the help I recieved from both Tony, and Cervantes, among other board members. I am not marketting this program, I am submitting it for marks that I will most certainly obtain.
The fact that I cannot "intelligently" program, by your standards, does not interfere with the fact that this program is functional and serves its purpose to a point.
Who said I was a full fledged programmer? And who stated that every grade 12 should have the ability to program up to YOUR standards?
Nice to have a board representative such as yourself around.
ooo may be u should be a writer...
but what Andy was saying is that ur program can be reduced, but if this is what u want, its ok
Pa|2a|)oX
Posted: Sun Feb 20, 2005 9:24 pm Post subject: (No subject)
I have no problem with what he's saying, just how he's saying it.
"...a bit of gr 10 math."
"...im just saying, for a gr 12, you should be able to program somewhat intelligently..."
ssr
Posted: Sun Feb 20, 2005 9:32 pm Post subject: (No subject)
oo well
we all should calm down and be happy in this forum
everyone should help each other...
so...
be happy and I dont mean "gay"
***** I know this is off topic here
but I just like to point out, 'cause there were some insults happening in this forum, I dont refer to "Andy", there were some posts last few days that were pretty bad...
so, lets make this forum more helpful