An absolute beginners question 
Author Message
 An absolute beginners question

Could anyone of you Pascal experts out there tell me why 'totalattenbonus'
gives me the wrong total when results for this program are printed, (*see
below*)
I have spent hours trying out other methods but to no avail..... I just get
results that are quite far out or are just the result for the last figures
entered into the attendance array.. any ideas or suggestions would be
gratefully received:

(*beginning of program*)
FOR count:= 1 TO numshifts[arraypoint] DO
  BEGIN  (*3*)

WRITELN('How many hours were worked on this shift');
READLN(hoursattend);

totalhoursattend[arraypoint]:= totalhoursattend[arraypoint] + hoursattend;
(*correct- this works*)
attendance[arraypoint]:= bonusattend*numshifts[arraypoint];
(*correct- this works*)
totalattendbonus:= totalattendbonus + attendance[arraypoint];
(*this does not - arrgghh!*)

(*program continues*)

Any ideas?,

Cheers :-)



Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question
: Could anyone of you Pascal experts out there tell me why 'totalattenbonus'
: gives me the wrong total when results for this program are printed, (*see
: below*)
: I have spent hours trying out other methods but to no avail..... I just get
: results that are quite far out or are just the result for the last figures
: entered into the attendance array.. any ideas or suggestions would be
: gratefully received:

: (*beginning of program*)

{could this be the problem?}  totalattendbonus := 0;

: FOR count:= 1 TO numshifts[arraypoint] DO
:   BEGIN  (*3*)

: WRITELN('How many hours were worked on this shift');
: READLN(hoursattend);

: totalhoursattend[arraypoint]:= totalhoursattend[arraypoint] + hoursattend;
: (*correct- this works*)
: attendance[arraypoint]:= bonusattend*numshifts[arraypoint];
: (*correct- this works*)
: totalattendbonus:= totalattendbonus + attendance[arraypoint];
: (*this does not - arrgghh!*)

: (*program continues*)

: Any ideas?,

: Cheers :-)



Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question

Quote:

> Could anyone of you Pascal experts out there tell me why 'totalattenbonus'
> gives me the wrong total when results for this program are printed, (*see
> below*)
> I have spent hours trying out other methods but to no avail..... I just get
> results that are quite far out or are just the result for the last figures
> entered into the attendance array.. any ideas or suggestions would be
> gratefully received:

> (*beginning of program*)
> FOR count:= 1 TO numshifts[arraypoint] DO
>   BEGIN  (*3*)

> WRITELN('How many hours were worked on this shift');
> READLN(hoursattend);

> totalhoursattend[arraypoint]:= totalhoursattend[arraypoint] + hoursattend;
> (*correct- this works*)
> attendance[arraypoint]:= bonusattend*numshifts[arraypoint];
> (*correct- this works*)
> totalattendbonus:= totalattendbonus + attendance[arraypoint];
> (*this does not - arrgghh!*)

> (*program continues*)

> Any ideas?,

> Cheers :-)

How is totalattendbonus declared?
How is it initialized?


Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question

Quote:
> How is totalattendbonus declared?

Just after BEGIN
totalattendbonus:= 0

I was going to post the complete program, but I didn't think it would be
necessary - plus I didn't want anyone laughing at my code! :-)



Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question

Quote:
> Could anyone of you Pascal experts out there tell me why 'totalattenbonus'
> gives me the wrong total when results for this program are printed, (*see
> below*)
> I have spent hours trying out other methods but to no avail..... I just get
> results that are quite far out or are just the result for the last figures
> entered into the attendance array.. any ideas or suggestions would be
> gratefully received:
> (*beginning of program*)
> FOR count:= 1 TO numshifts[arraypoint] DO
>   BEGIN  (*3*)
> WRITELN('How many hours were worked on this shift');
> READLN(hoursattend);
> totalhoursattend[arraypoint]:= totalhoursattend[arraypoint] + hoursattend;
> (*correct- this works*)
> attendance[arraypoint]:= bonusattend*numshifts[arraypoint];
> (*correct- this works*)
> totalattendbonus:= totalattendbonus + attendance[arraypoint];
> (*this does not - arrgghh!*)
> (*program continues*)
> Any ideas?,

   Without seeing your data declarations, I can't be sure, but I suspect
it's "integer overflow" at work here.  That is, your data and computation
exceeds the integer range somewhere, and the result becomes a modulus of
32768.  It's an old problem and can easily be solved by casting one of
the operands to LongInt and having TotalAttendBonus be declared as a
LongInt.  Like this:
var TotalAttendBonus : LongInt;
...
  TotalAttendBonus := TotalAttendBonus+LongInt(Attendance[ArrayPoint])


Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question


Quote:

>> How is totalattendbonus declared?

>Just after BEGIN
>totalattendbonus:= 0

>I was going to post the complete program, but I didn't think it would be
>necessary - plus I didn't want anyone laughing at my code! :-)

If the error was in the parts you thought significant, you probably
would have found it; it is a waste of everybody's time to give a
question without adequate information.

The correct move is to reduce the program to the minimum which shows the
"error" (usually this reveals the cause of the error), and then to post
that, with data and results.

--

  Web <URL: http://www.merlyn.demon.co.uk/> TP/BP/D/&c. FAQqish topics & links.
  Timo's TurboPascal <A HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
  <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of c.l.p.b.



Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question
Thanks for the advice Doc....
I found the problem was that 'totalattendbonus' should have been placed just
before all the totals throughout my program were written to screen......


Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question

Quote:

> Could anyone of you Pascal experts out there tell me why
> 'totalattenbonus' gives me the wrong total when results for
> this program are printed, (*see below*) I have spent hours
> trying out other methods but to no avail..... I just get
> results that are quite far out or are just the result for the
> last figures entered into the attendance array.. any ideas or
> suggestions would be gratefully received:
> (*beginning of program*)
> FOR count:= 1 TO numshifts[arraypoint] DO
>   BEGIN  (*3*)
> WRITELN('How many hours were worked on this shift');
> READLN(hoursattend);
> totalhoursattend[arraypoint]:= totalhoursattend[arraypoint] + hoursattend;
> (*correct- this works*)
> attendance[arraypoint]:= bonusattend*numshifts[arraypoint];
> (*correct- this works*)
> totalattendbonus:= totalattendbonus + attendance[arraypoint];
> (*this does not - arrgghh!*)
> (*program continues*)

-----------------------------------------------------------------
All these statements are in a "FOR" loop.  Since you have both a
data input statement and an accumulation calculation, it is usual
that the data you're "READLN"ing is needed for the calculation.
However this is not the case here.  You have the line:

    attendance[arraypoint]:= bonusattend*numshifts[arraypoint];

What is "bonusattend" and how is it initialized?  Why are you
multiplying it by the number of shifts associated with
"arraypoint" (you realize that this makes totalattendbonus equal
to bonusattend * numshifts[arraypoint] SQUARED when the loop is
exited)?  Since totalattendbonus can be calculated directly, why
is it done iteratively?  I can't be positive without seeing the
whole program, but to me, this line doesn't quite fit in.
-----------------------------------------------------------------
Derek Asari



Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question
: Could anyone of you Pascal experts out there tell me why 'TotalAttenBonus'
: gives me the wrong total when results for this program are printed,

: FOR count:= 1 TO NumShifts[ArrayPoint] DO
:   BEGIN  (*3*)
:     WRITELN('How many hours were worked on this shift');
:     READLN(HoursAttend);
:     TotalHoursAttend[ArrayPoint] := TotalHoursAttend[ArrayPoint]
:                                     + HoursAttend;
:     (*correct- this works*)
:     Attendance[ArrayPoint]:= BonusAttend*NumShifts[ArrayPoint];
:     (*correct- this works*)
:     TotalAttendBonus:= TotalAttendBonus + Attendance[ArrayPoint];
:     (*this does not - arrgghh!*)

Since I haven't seen anyone else mention it:
Where is the END to match the BEGIN in your For loop?
I suspect it should be right after the:  TotalHoursAttend[...  line.
If, as it looks, it comes later, then the TotalAttendBonus will be
the sum of products:  NumShifts[ArrayPoint]*Attendance[ArrayPoint].
I suspect you want just the sum of the Attendance[ArrayPoint] values.

A useful debugging technique for beginners is to put a WriteLn right
after the suspect calculation:
      Attendance[ArrayPoint]:= BonusAttend*NumShifts[ArrayPoint];
      (*correct- this works*)
      TotalAttendBonus:= TotalAttendBonus + Attendance[ArrayPoint];
      WriteLn('TotalAttendBonus:  ',TotalAttendBonus,
          '  Attendance[',ArrayPoint,']:  ',Attendance[ArrayPoint]);
      (*this does not - arrgghh!*)
This will allow you to see if the calculation is improperly being done
over and over, or if the value is building up to an overflow (gets
bigger and bigger - then suddenly jumps negative).

  |\/|                          ||  Burnaby South Secondary School

Mathematics & Computer Science  ||             (Canada)



Wed, 18 Jun 1902 08:00:00 GMT  
 An absolute beginners question
Hey um, rather us {} for comments as it looks cooler.

(**) is ok but {} looks cool, well thats just an oppinion and well everyone
has one.

cheers
--DKmore



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. An absolute beginners question

2. an absolute beginner : HEX question

3. which pascal for an absolute beginner ?

4. Beginner Question

5. Help, beginner question....

6. Beginners Question - Filtering a Table

7. Beginner question

8. Beginners question....length of string.

9. Simple Beginner Question

10. Interbase beginner questions

11. Beginner Question. Calling Forms from Menus

12. Beginners Question

 

 
Powered by phpBB® Forum Software