Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 E=Mc2 Program
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
evogre3n




PostPosted: Sun Apr 17, 2005 9:00 am   Post subject: E=Mc2 Program

Hey everyone.

This is the most recent assignement ivehad. Im just a begginer, starting out in turing.. and the assignment was on if structures and error traps etc.

Here is the code.

Check it out, give some feedback thanks a lot Smile

code:

%Nick
%April 15th 2005
%Ms. Dyke
%E=MC2 Program. This program will calculate either energy or mass based on the users input.
import GUI

var variableType, unitType, missingUnit, units : string
var answer, givenVariable : real

setscreen ("graphics: 500,600")
GUI.SetBackgroundColor (7)
colorback (7)

%Title
locate (1, 24)
color (0)
put "The E=MC2 Calculator" ..

%Introduction
locate (3, 1)
color (0)
put "Welcome to the E=MC2 Program. This program will calculate the missing variable in the equation E=MC2 based on your input foreither energy (e) or mass (m)"


%User Input

for x : 1 .. 99
    locate (8, 1)
    color (0)
    put "Please enter your input variable."
    put "Either 'e' for energy or 'm' for mass: " ..
    get variableType
    if variableType = "m" or variableType = "M" then
        locate (10, 1)
        color (0)
        put "You have chosen mass, please continue"
        unitType := "mass"
        missingUnit := "energy"
        units := "Joules"
        locate (12, 1)
        color (0)
        put "Please enter the mass in Kg: " ..
        get givenVariable
        if givenVariable >= 0 then
            locate (13, 1)
            put "Thank you, please wait while your request is processed. "
            exit
        else
            locate (7, 1)
            color (12)
            put "Your input for ", unitType, " was invalid, try again."
            locate (12, 1)
            put "                                             "
            locate (9, 40)
            put "                            "
        end if
    elsif variableType = "e" or variableType = "E" then
        locate (10, 1)
        color (0)
        put "You have chosen energy, please continue"
        unitType := "energy"
        missingUnit := "mass"
        units := "Kg"
        locate (12, 1)
        color (0)
        put "Please enter the energy in Joules: " ..
        get givenVariable
        if givenVariable >= 0 then
            locate (13, 1)
            put "Thank you, please wait while your request is processed. "
            exit
        else
            locate (7, 1)
            color (12)
            put "Your input for ", unitType, " was invalid, try again."
            locate (12, 1)
            put "                                             "
            locate (9, 40)
            put "                            "
        end if
    else
        locate (7, 1)
        color (12)
        put "You have chosen incorrectly, please try again"
        locate (9, 39)
        put "                             "
    end if
end for

%Processing
if variableType = "m" or variableType = "M" then
    answer := givenVariable / (3 * (10 ** 8))
else
    answer := givenVariable * (3 * (10 ** 8))
end if

%Output
locate (17, 1)
color (0)
put "Based on your input, the value of your missing variable, wich is ", missingUnit,", is equal to: " ..
color (14)
put answer , " ",units

%Ending Notes
locate (20, 1)
color (brightgreen)
put "Thank you for using The E=MC2 Calculator"
put "Look for our first public release in summer of 2005"

%End Program
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Sun Apr 17, 2005 9:51 am   Post subject: (No subject)

This is interesting...you realize that by importing GUI, you're slowing your start up significantly. You only ever use GUI. commands once, and that's to set the GUI background colour...
You seeing the problem here?

Error trapping: you *really* need to work on this. All of the old favourites work. Input string where integers are asked for, excessive string length, Ctrl+Z...time to start trapping...

Now that you've pretty much got the hang of linear programming, it's time you moved onto something a little more object-oriented. No classes or pointers for you just yet...just spend some time looking at the Tutorials on Procedures, Functions, and Modules.
evogre3n




PostPosted: Sun Apr 17, 2005 1:29 pm   Post subject: (No subject)

Im looking into everything you have mentioned

The thing is, i asked my teacher, she says she doesnt want me using any of that, no procedures or anything, im not even allowed to use the "loop" command.. :S

THanks for the comments

Btw. does anyone know what this error could mean?

this line

code:
    answer := givenVariable / (3 * (10 ** 8) ** 2)


this error:

code:
Overflow in "**" operation.
Cervantes




PostPosted: Sun Apr 17, 2005 1:56 pm   Post subject: (No subject)

It means your numbers are too big. Use real numbers.
Also, you got the equations wrong. First, the speed of light is 3e5, and we want to square that. So that's (3 x 10^5)^2. That is not the same as 3*10^8. Rather, it's the same as 9 x 10^10. So let's just use that number instead of doing these calculations over and over.
The second thing is that, I think, you've mixed up the equations. You've got m = ec^2. It should be this:
e = mc^2
m = e/c^2

So the code looks like this:
code:

%Processing
if variableType = "m" or variableType = "M" then
    answer := givenVariable * 90000000000.0
else
    answer := givenVariable / 90000000000.0
end if
Andy




PostPosted: Sun Apr 17, 2005 4:16 pm   Post subject: (No subject)

this program isnt all that useful... if you however added calculations for wavelengths of matters, or momentum of photons wit electron problems, then it would be better. also, mass starts off with negative energy, because the strong nulcear force binds the matter together and a certain amount of energy has to be provided inorder to liberate the nucleons
Delos




PostPosted: Sun Apr 17, 2005 5:33 pm   Post subject: (No subject)

Hehe, quite true Andy...[memories of grade 12 physics rush back in]...
Ouch. That hurt.
evogre3n




PostPosted: Tue Apr 19, 2005 8:37 am   Post subject: (No subject)

if your ramblin on about the usefullness of the program, you obviously missed the point.

The point of this was to have me learn about if structures etc. Confused not to make use of the fomula
jamonathin




PostPosted: Tue Apr 19, 2005 11:30 am   Post subject: (No subject)

So you made this program so you could learn about if's, and your program's not that useful, so why did you submit it then, if you didn't want people to criticize it? To show off that you know how to use 'if' statements? Confused
Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: Tue Apr 19, 2005 9:46 pm   Post subject: (No subject)

the speed of light is 3e5 km/s but in this eq you should use 3e8 m/s
hacker101




PostPosted: Fri May 06, 2005 8:57 pm   Post subject: (No subject)

Nick:

Ms. **** would be proud. Nice program.

Mine was i=q/t
Andy




PostPosted: Sun May 08, 2005 4:43 pm   Post subject: (No subject)

ever heard of pm?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: