#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    1
    Rep Power
    0

    HELP!! Dev-Pascal First Program...


    Hey guys,

    I need major help. I am in an intro to computer programming class in school - but it feels more like ADVANCED programming to me. I can't get the instructor to return emails and I'm panicking.

    Here is the program info...

    I need to convert the equation '(b*b) - 4ac' into Pascal expressions. Then I need to write a program that will read in the values of a, b, and c and use these values to solve the expression. My output should be in tabular format and look something like this:


    Mathematical Formula Pascal Expression Solution
    -----------------------------------------------------------
    b*b - 4ac b*b - 4ac 123.45


    I know this is pretty basic, but I don't have a book for the class and all I have to go on are some notes from class.

    Thanks for the help.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    http://www.xploiter.com/mirrors/pascal/default.htm

    This will help a lot.

    Comments on this post

    • CountVikernes agrees : Oops, it probably will better than my post. Just noticed. *laugh* Ah well. At least I did something for awhile! *chuckle*
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Lisbon, Portugal
    Posts
    154
    Rep Power
    12
    Heh, I just finished my first semestre where I had Pascal. I've pretty much forgotten the lot of it! *chuckle*

    Going through a book helps. I had had a fair bit of programming experience beforehand, so I got through alright, but I noticed some of the guys who'd never programmed before got really stuck.

    I can only give you a sort of introduction...

    Guidelines:
    - A function is not "allowed" to change any variables within itself.
    - A function is not "allowed" to write or read anything from/to the console.
    - A pr

    ocecure
    is allowed to change variables within itself, read and write from the console, but should not return any variable.

    A program should look like this:

    Code:
    program myProgramName;
    var
       ... variable declaration ...
    
    function myFunctionName1(myFunctionArguments): functionReturnType;
    begin
       ... my function code ...
    end;
    
    procedure myProcedureName1(myProcedureArguments);
    begin
       ... my procedure code ...
    end;
    
    
    begin
     ... my main program code ...
    end.
    Right, I won't code for you, but I'll try to give you all you need to know. Let's break down what you need to do:

    You have variables, so...
    1) Let the computer know you need to have variables.

    You need to read a, b and c from the console. Remember the guidelines up there?
    2) Need a procedure to read stuff from the console, namely, a b c.

    You need to use a formula... look at the guidelines again and ...
    3) Need a function which processes a, b and c and returns the result of the formula.

    Need to show the result to the user, so...
    4) You either do that in the main program, or you can do an output procedure.


    Four steps, that sounds about easy enough, right? So here's what you need to know codewise (you already sort of know the syntax).

    Variables come in different types. Since you're only calculating, I'm guessing your variables are going to be things like 1, 2, 3, 4, 5, 6, 2386 and so forth. Those are integers, so you need to allocate memory space for three integer values - a, b, and c.

    Up there in the var section, the syntax is:
    Code:
    var
       myvar1, myvar2, myvar3: varType1;
       myvar2: varType2
       myvar3: varType4
    Where myvar is the name you want to give your variables, and vartypes are the type of variable (strings, integers, reals, etc).

    You know what each procedure and function is going to do, so you can write the main program with each step in turn, and then move on to write the actual functions and procedures.

    Functions return values, so when you call a function, generally, you'll have something like:
    Code:
    myVar := myFunction(myArgument1, myArgument2);
    Procedures don't need that, since they don't return values. However, they can change the arguments you've passed on to them.

    When you define a function, all its arguments should be const, constants. That's because a function doesn't change any variables. A procedure might, so any variables you're planning on changing there should have var before them. I'll exemplify:

    Code:
    procedure ChangeThisNumber(var myInt: Integer);
    begin
       myInt := 2006
    end;
    
    // Commented line: this is the main program now.
    begin
       x := 5;
       ChangeThisNumber(x);
       Writeln(x); // This will now print 2006
    end.
    Pretty simple, huh? If you had written const instead of var, you'd get an error, because myInt was supposed to be constant and not variable.

    When you pass several arguments, after "var myInt: Integer", you insert a semicolon and then another variable. Something along the lines of:

    Code:
    procedure ChangeTheseVariables(var myInt: Integer; const myNumber: Integer; var myString: String);
    begin
       myInt := 2000 + myNumber;
       myString := "The year is ";
    end;
    
    // Commented line: this is the main program now.
    begin
       x := 5;
       s := "Lalalala";
       ChangeTheseVariables(x, 6, s);
       Writeln(s, x); // This will now print "The year is 2006", I *think*
    end.
    Notice the constant doesn't get changed, but that all the variables that are assigned as "var" within the argument section can be changed with no problems.

    Hopefully you know how to handle a procedure. There's input though. Well, that's simple. All you need is a Readln(myVar). Obviously, the user needs to know what the computer wants, so a "Write("Give me the number a: ");" and then a Readln(a); would probably work better.

    Now, a could be a var within a procedure called "getMyInput"... so could b, and c. They could all be within a single procedure.

    How about functions? Well, it's the same as procedures, but everything passed is a constant, and there's that thing near the end:
    Code:
    function myFunction(myArguments): varType;
    See that varType? Remember when I told you functions returned some value? Well, the computer's got to know what type of variable everything is, so in your example, you're probably returning an integer. Just write integer there and you'll be OK.

    Right. So, all you need now is a function with the expression, and you're off!

    To tell the function to return a variable, you assign the function name, like so:
    Code:
    function IncreaseString(const myString: String): String;
    begin
       IncreaseString := myString + " is happy.";
    end;
    Notice how IncreaseString is the function name. That's why the function is returning a value. When you call the function, you'd get:

    Code:
    s := "A pink elephant"
    s2 := IncreaseString(s);
    Writeln(s2); // This would now return "A pink elephant is happy."
    You didn't really change anything inside the function, you created something "new", which was assigned to s2.

    Now, knowing that * is multiplication, / is division, and + and - are just what they are, you'll might be able to do that program.

    Hmm, maybe you didn't even go as far as procedures. Ah well. You can tell I'm waiting for a download to finish, can't you? *chuckle*

    Anyway, try it out yourself. I hope I helped some... if you show us some code that doesn't work, we'll be able to help you better by pointing out where the problem is at, and how to solve it.

    It's better to understand it and do it yourself than to have the whole code written up for you.

    Stop by again, hope you'll enjoy the course. It's a whole world out there. *grin*
    "Get it hot! Hit it harder!!!"
    --- The tutor warcry

    ActivePython
    Mark Pilgrim's Dive into Python E-book, thank this guy for his contribution to the Python community!

IMN logo majestic logo threadwatch logo seochat tools logo