Pascal Average Program Question

Reply

Join Date: Sep 2007
Posts: 1
Reputation: lietuva is an unknown quantity at this point 
Solved Threads: 0
lietuva lietuva is offline Offline
Newbie Poster

Pascal Average Program Question

 
0
  #1
Sep 24th, 2007
I have to create a program that uses 2 loops to calculate the average of any number of grades, and run the program any number of times. So far this is what I have, I'm sure it's at least 99% wrong, but if someone could please help me get in the right direction, that would be great.


Pascal and Delphi Syntax (Toggle Plain Text)
  1. Var num, sum, max, i, average : double, n:integer
  2.  
  3. Begin
  4. if (num>0) and (num<=max) then begin
  5. for i := 1 to num do begin
  6. write ('Enter grade');
  7. readln(grades);
  8. end;
  9. average := (sum/n);
  10. writeln ('The average is' , average);
  11. End.
Last edited by lietuva; Sep 24th, 2007 at 7:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: jackkenyon is an unknown quantity at this point 
Solved Threads: 0
jackkenyon jackkenyon is offline Offline
Newbie Poster

Re: Pascal Average Program Question

 
0
  #2
Sep 28th, 2007
Var num, sum, max, i, average : double, n:integer

Begin
if (num>0) and (num<=max) then begin
for i := 1 to num do begin
write ('Enter grade');
readln(grades);
end;
average := (sum/n);
writeln ('The average is' , average);
End.

Just some questions to point you

- where does "num" come from? Is this meant to be a program or a function where num is passed in as a parameter. If this is a program then num is undefined and you will crash & burn.

- you divide by n, i think you mean to divide by "num" as n is also undefined

- is it a good idea to use "num"? An alternative is to use a "repeat - until" loop so that the user does not have to specify what num is first, just repeat until, say, the user enters a blank line. Of course you then have to set "n:=0;" before starting and set "n:=n+1;" at each loop (except the last one).

- again "sum" is undefined, you need to set it before starting & you need to do somthing with the "grades" value that you read in. ie convert it to a number & add it to sum.

- watch your indentation it will save you from a lot of grief with begin-end. In the above you should have
if (num...etc) then
begin
for etc... do
begin
etc
end
dostuff
end
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC