HELP!!! please help with program
Author |
Message |
Julian Jone #1 / 6
|
HELP!!! please help with program
I was hoping that someone could give me a tip as to what I am missing in the following program that gives me the following compile error. Our assignment is simple, solve a quadratic equation given the variable a, b and c. Here is my program: Program quadratic (input, output); {This program computes the answer, x, to the quadratic equation ax^2+bx+c=0} var a, b, c : real; const d = sqr(b)-(4*a*c); begin writeln ('Input the values of a, b, and c'); readln (a) ; readln (b) ; readln (c) ; if d > 0 then begin writeln ('the two roots are'); writeln ((-b + sqrt(d))/(2*a):20:5); writeln ((-b - sqrt(d))/(2*a):20:5); end else if d = 0 then begin writeln ('the root is'); writeln (-b/2*a); end else if d < 0 then begin writeln ('the roots are'); writeln ((-b + sqrt(abs(d))/2*a)); writeln ((-b - sqrt(abs(d))/2*a)); end else writeln ('I suck'); end. Program looks fine to me, but I get a compile error with my constant, d: here is the error message; $ pas hw2_3.pas 00009 0 0 d = sqr(b)-(4*a*c); 1 2 3 %Pascal-E-SYNERRCTE, (1) Error in compile-time expression %PASCAL-E-SYNERRCTE, (2) Error in compile-time expression %PASCAL-E-SYNERRCTE, (3) Error in compile-time expression %PASCAL-E-ENDDIAGS, PASCAL completed with 3 diagnostics Could someone please let me know what an error in compile-time expression means, and how to remedy that line in my program. I appreciate all input, Sincerely, Julian Jones
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Alan M. Evan #2 / 6
|
HELP!!! please help with program
Quote:
> var > a, b, c : real; > const > d = sqr(b)-(4*a*c);
BOOM! Here it is. You are trying to compute a constant based on unknown values. Variables a, b, and c are not initialized. CONSTANTS MUST BE KNOWN AT COMPILE TIME! Some versions of pascal won't allow expressions at all in a constant declaration. I think what you really want here is make a function out of d. Function d(a,b,c: Real); Begin d := sqr(b)-(4*a*c); End; Now the expression "if d > 0 then..." should do what you were expecting your constand d to do. AME
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Alan M. Evan #3 / 6
|
HELP!!! please help with program
Quote:
> var > a, b, c : real; > const > d = sqr(b)-(4*a*c);
BOOM! Here it is. You are trying to compute a constant based on unknown values. Variables a, b, and c are not initialized. CONSTANTS MUST BE KNOWN AT COMPILE TIME! Some versions of pascal won't allow expressions at all in a constant declaration. I think what you really want here is make a function out of d. Function d(a,b,c: Real); Begin d := sqr(b)-(4*a*c); End; Now the expression "if d(a,b,c) > 0 then..." should do what you were expecting your constand d to do. AME
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
Otto Stol #4 / 6
|
HELP!!! please help with program
Quote:
> Program quadratic (input, output); > {This program computes the answer, x, to the quadratic equation > ax^2+bx+c=0} > var > a, b, c : real; > const > d = sqr(b)-(4*a*c);
As has already been said, you cannot compute a compile-time constant based on run-time variables. I suggest, here you simply declare a variable to hold the discriminant, and compute it later, when you will have read the values needed: var d: real; Alas, standard Pascal does not allow you to express your thoughts lucidly. You cannot define run-time constants (i.e. constants calculated from values obtained at run-time). You cannot even define initial values for variables (which is the 1st work-around for your problem coming to my mind). Quote: > begin > writeln ('Input the values of a, b, and c'); > readln (a) ; > readln (b) ; > readln (c) ;
Here is the place where you can compute the discriminant: d = sqr(b) - 4*a*c; Quote: > if d > 0 then .. > else if d = 0 then > begin > writeln ('the root is'); > writeln (-b/2*a); > end
Here is an error that will lead to wrong output: as the multiplication operator does not bind closer than the division operator, you have to code either -b/(2*a) or -b/2/a The same error is made below, in the d<0 branch, while it is not made in the d>0 branch. Quote: > else if d < 0 then > begin > writeln ('the roots are'); > writeln ((-b + sqrt(abs(d))/2*a)); > writeln ((-b - sqrt(abs(d))/2*a)); > end
While this part will compile alright, it is still wrong! Your program will report two real solutions where the equation really has two complex ones. Replace the preceding lines with: begin writeln ('the two roots are'); writeln (-b/2/a, ' + ', sqrt(abs(d))/2/a, '*i'); writeln (-b/2/a, ' - ', sqrt(abs(d))/2/a, '*i'); end Quote: > else > writeln ('I suck');
This is entirely superfluous, as the preceding conditions, viz. Quote: > if d > 0 then . > else if d = 0 then .. > else if d < 0 then
exhaust all possibilities. In a language that allows for run-tim constants, the hole program would be more lucid; in particular the declaration of d's datatype and its value would not be separated by other stuff (which can be several hundred lines of code, in more realistic examples). In the following example, note that keyword delimiters, and types, are written in uppercase, while identifiers are in lower case. Note also that LOC REAL declares a real-typed variable, while REAL (without the LOC) declares a constant. In the language used below, everything declared in an IF part is visible throughout the remaining part of the IF clause (i.e. from the point of declaration through the IF, and THEN parts, down to the end of the ELSE part); same holds for an ELIF part, where ELIF is an abbreviation for ELSE IF. BEGIN # This program computes the answer, x, # # to the quadratic equation ax^2+bx+c=0 # LOC REAL a, b, c ; write (("Input the values of a, b, and c", newline)) ; read ((a, b, c)) ; IF REAL d = a*a - 4*a*c ; d = 0 THEN write (( "The root is ", -b/2/a, newline )) ELIF REAL s = sqrt (ABS d) ; d > 0 THEN write ( ( "The two roots are:", newline , (-b+s) / (2*a) , newline , (-b-s) / (2*a) , newline ) ) ELSE write ( ( "The two roots are:" , newline , -b/2/a, " + ", s/2/a, "*i", newline , -b/2/a, " - ", s/2/a, "*i*, newline ) ) FI END FI is IF spelled backwards; this ends the IF statement, and thus mends a syntactic ambiguity Pascal has inherited from Algol 60, known as "the Problem of the Dangling ELSE". You see, a well-designed language, such as Algol 68 (used in the example above) lets you express your thoughts more lucidly. Too bad that Niklaus Wirth impatiently has left the Algol 68 design team and rushed into defining and implementing Pascal. Best wishes, Otto Stolz
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
John R. Reag #5 / 6
|
HELP!!! please help with program
Quote:
>Alas, standard Pascal does not allow you to express your thoughts >lucidly. You cannot define run-time constants (i.e. constants >calculated from values obtained at run-time). You cannot even define >initial values for variables (which is the 1st work-around for your >problem coming to my mind).
Extended Pascal provides for initial values on variables or types. Run-time constants are not directly part of Extended Pascal. We had a simila feature during some of the public drafts, but allowing constants to have run-time values also allows constants to end up with run-time size also (ie, array constants). The issue of constants with run-time size bothered many people and caused some language issues that nobody on the committee felt strongly enough to fix. So, we removed the whole concept from the standard. You can indirectly make yourself a variable with an initial value that is readonly afterwards by using schema types and using the schema discrimants as your run-time constants. -- John Reagan DEC Pascal Project Leader Application Compilers and Environments Digital Equipment Corporation
Disclaimer: The opinions and statements expressed by me are not necessarily those of Digital Equipment Corporation. --
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
L #6 / 6
|
HELP!!! please help with program
Quote:
>> var >> a, b, c : real; >> const >> d = sqr(b)-(4*a*c); >BOOM! Here it is. You are trying to compute a constant based on unknown >values. Variables a, b, and c are not initialized. CONSTANTS MUST BE >KNOWN AT COMPILE TIME! Some versions of pascal won't allow expressions >at all in a constant declaration. >I think what you really want here is make a function out of d. > Function d(a,b,c: Real); > Begin > d := sqr(b)-(4*a*c); > End;
I think it's possible to do what he wanted by: {not tested yet} ... var d : real = (sqr(absolute (b)) - 4*absolute(a) * absolute(c)); used to do something like this...don't have manual here with me and haven't tested it....don't even know if the syntax is right or not ....play with it if you want....hope it works...hehehehe
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
|