Computer Science Canada

Binary multiplication...

Author:  Pa|2a|)oX [ 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 Smile

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

    lnum1 := length (snum1)
    lnum2 := length (snum2)
    maxn := max (lnum1, lnum2)

    glnum1Add

    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




Author:  ssr [ Sat Feb 19, 2005 8:21 pm ]
Post 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 Very Happy

Author:  Pa|2a|)oX [ Sat Feb 19, 2005 9:48 pm ]
Post 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.

Author:  ssr [ Sun Feb 20, 2005 8:19 am ]
Post subject: 

lol ya, schools....
anyway, I think it is a pretty good program. Very Happy

Author:  person [ Sun Feb 20, 2005 12:01 pm ]
Post subject: 

this is cool...but its almost the same as ur other progs...shouldnt u have just posted them in the same thread?

Author:  Pa|2a|)oX [ Sun Feb 20, 2005 12:32 pm ]
Post subject: 

I didn't finish them all at one time so I've been posting them seperately.

Sorry.

Author:  Andy [ Sun Feb 20, 2005 4:39 pm ]
Post subject: 

umm, you do realize that the samething can be achieved in less than 20 lines by using strint and intstr right?

Author:  Pa|2a|)oX [ Sun Feb 20, 2005 5:59 pm ]
Post 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.

Author:  Andy [ Sun Feb 20, 2005 6:24 pm ]
Post subject: 

and ur if else structure can be replaced by a simple index and a bit of gr 10 math

Author:  Pa|2a|)oX [ Sun Feb 20, 2005 6:36 pm ]
Post subject: 

Why so negative towards me?

Author:  Andy [ Sun Feb 20, 2005 6:41 pm ]
Post 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

Author:  Pa|2a|)oX [ Sun Feb 20, 2005 7:26 pm ]
Post 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.

Author:  ssr [ Sun Feb 20, 2005 9:15 pm ]
Post 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 Very Happy Very Happy

Author:  Pa|2a|)oX [ Sun Feb 20, 2005 9:24 pm ]
Post 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..."

Author:  ssr [ Sun Feb 20, 2005 9:32 pm ]
Post 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 Laughing Laughing Laughing

Author:  Andy [ Mon Feb 21, 2005 3:41 pm ]
Post subject: 

if i came off sounding insulting then i appologize, ssr is right, im not here to make fun of people. i was simply suggesting that your program can be done in a more efficient manner

Author:  Pa|2a|)oX [ Mon Feb 21, 2005 7:16 pm ]
Post subject: 

Thank you for the apology.

I totally agree that the program can be snipped to at least half its size by doing a general case where I switch variables around sintead of having a case by case structure. It's the same with my other programs, I just made these programs in about 5 hours each and by the end I just wanted to get it over with.

Thanks, right now I'm working on the division, something tells me I'm not going to be able to finish it. Oh well.

Author:  Andy [ Tue Feb 22, 2005 4:11 pm ]
Post subject: 

for your program, do you have to everything in base 2? cant you convert it to base 10, do your stuff then convert back? also 5 hrs is a long time... i never spend more than 4 hrs on my compsci projects (except finals) ... even 1000 line ones... also, u ever heard of recursion?

Author:  Pa|2a|)oX [ Tue Feb 22, 2005 5:55 pm ]
Post subject: 

Yes, it has to be in base 2. I haven't heard of recursion no, care to elaborate?

Author:  Andy [ Tue Feb 22, 2005 6:13 pm ]
Post subject: 

basicly its a function or program that recalls it self each time until the final value is computed... you can use it to do your binary addition/multiplication/subraction/division easily since you basicly do the same procedure each time... go look up on it in the tutorials... then post some attempts and i'll help you do the rest


: