Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
I think I found that same answer. here is what I wrote

Code:
FUNCTION AVSQRSUM(A,B,C,D:integer):integer;
	begin
		AVSQRSUM := ((A*A) + (B*B) + (C*C) + (D*D) DIV 4);
	end;
 
I think I found that same answer. here is what I wrote

Code:
FUNCTION AVSQRSUM(A,B,C,D:integer):integer;
	begin
		AVSQRSUM := ((A*A) + (B*B) + (C*C) + (D*D) DIV 4);
	end;

What is the operator precedence in Pascal? Does the DIV apply to all four or only D?

Test it for something simple. For A=B=C=D=2 the square of any one is 4 and the average would thus still be 4. Is this what your code gives?

B
 
DIV applies to integers
/ applies to real (float) numbers

I did a test with your numbers

Code:
program test;

var
 a, b, c, d, result : integer;
 
begin

a := 2;
b := 2;
c := 2;
d := 2;

result := (((a*a)+(b*b)+(c*c)+(d*d)) DIV 4);
writeln(result);

end.

When I wrote it like this the RESULT was 4
 
DIV applies to integers
/ applies to real (float) numbers

When I wrote it like this the RESULT was 4

DIV has higher precedence than + http://www.hkbu.edu.hk/~bba_ism/ISM2110/pas039.htm which is why you need the extra parentheses.

I suspect the average is probably supposed to be a real/float. Take A=B=C=2 D=3 for example. You'd get 21 for the sum of squares. Divide by 4 and you'd get 5.25. In general the sum of squares won't be a multiple of 4 so the average won't be a good integer.

B
 
Good point. I will convert it to a REAL. Also I finished the assignment and it compiles and works! I am looking into what was mentioned earlier about cleaning up my check INRANGE Function.

Is the IF statement legal bellow? It compiles without issue but gives the wrong result. Those numbers are in the valid range.
Code:
program test;

var
 a, b, c, d, result, min, max : integer;
 
begin

a := 12;
b := 18;
c := 18;
d := 18;
min := 10;
max := 20;

if (((a) OR (b) OR (c) OR (d)) > max)
	then writeln ('not in range ');	
else writeln ('INRANGE');
	
end.

After writing this I am guessing not since it is one or the other.
 
Last edited:
Is the IF statement legal bellow?

It may be legal, but isn't what you want. Again it's operator precedence. You want the OR to act on the booleans generated by the comparisons.

The two sides of the OR need to evaluate to TRUE or FALSE.

B
 
If you really want to impress your instructor, try two things to make your program more flexible:

Put an entry at the top of the program that says
Code:
PROGRAM
    CONST
         MYRANGEMIN = 10;
         MYRANGMAX = 20;
Then use these names in place of the 10 and 20 when you are range testing. This will allow you to alter the program's behavior in one small place instead of several (a very important lesson in programming - adopt the attitude that constant value, like "10" and "20" look ugly).

Make your summing and averaging function more flexible: at the top of the program, put in these variables:
Code:
PROGRAM
    VAR
        CUBESUM, SQUARESUM, SQUARECOUNT : INTEGER;
Now write a PROCEDURE that adds to the _SUM variables and increments the count variable. This will allow you to sum any number of squares and cubes. Then you can write FUNCTIONs to retrieve the results. (Hint: you should also write and use a procedure to reset all the values to zero, like CLEARSUMS, and a test in the average function to make sure the count is not zero).
 
Sydde - I will try the const idea.

balamw - Click! (I think) I can not evaluate A against B to get true or false. but I can evaluate each variable against the MAX to get a true or false

Code:
if ((a > max) or (b > max) or (c > max) or (d > max))
	then writeln ('not in range ')	
else writeln ('INRANGE');
	
end.

This code works now! Thanks for the push in the right direction without giving me the answer!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.