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.
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.
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.
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).