February 3rd, 2006, 09:48 PM
HELP!! Dev-Pascal First Program...
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.
February 3rd, 2006, 11:12 PM
February 4th, 2006, 06:00 AM
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...
- 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:
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:
... variable declaration ...
function myFunctionName1(myFunctionArguments): functionReturnType;
... my function code ...
... my procedure code ...
... my main program code ...
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:
Where myvar is the name you want to give your variables, and vartypes are the type of variable (strings, integers, reals, etc).
myvar1, myvar2, myvar3: varType1;
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:
Procedures don't need that, since they don't return values. However, they can change the arguments you've passed on to them.
myVar := myFunction(myArgument1, myArgument2);
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:
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.
procedure ChangeThisNumber(var myInt: Integer);
myInt := 2006
// Commented line: this is the main program now.
x := 5;
Writeln(x); // This will now print 2006
When you pass several arguments, after "var myInt: Integer", you insert a semicolon and then another variable. Something along the lines of:
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.
procedure ChangeTheseVariables(var myInt: Integer; const myNumber: Integer; var myString: String);
myInt := 2000 + myNumber;
myString := "The year is ";
// Commented line: this is the main program now.
x := 5;
s := "Lalalala";
ChangeTheseVariables(x, 6, s);
Writeln(s, x); // This will now print "The year is 2006", I *think*
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:
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.
function myFunction(myArguments): varType;
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:
Notice how IncreaseString is the function name. That's why the function is returning a value. When you call the function, you'd get:
function IncreaseString(const myString: String): String;
IncreaseString := myString + " is happy.";
You didn't really change anything inside the function, you created something "new", which was assigned to s2.
s := "A pink elephant"
s2 := IncreaseString(s);
Writeln(s2); // This would now return "A pink elephant is happy."
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*