| | |
Pascal Average Program Question
![]() |
•
•
Join Date: Sep 2007
Posts: 1
Reputation:
Solved Threads: 0
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)
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.
Last edited by lietuva; Sep 24th, 2007 at 7:58 pm.
•
•
Join Date: Sep 2007
Posts: 6
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- factorial using a for loop (C++)
- pascal triangle program (C++)
- Jumps into code on program end (Pascal and Delphi)
- Wierd Numbers appear when trying to run a running product in a C++ program (C++)
- Program security (When Launched) (C)
- help with pascal! (Pascal and Delphi)
- simple program tokenizer problem (Java)
- How do I create a program using an Array ? (C++)
- Program Question...listboxes (Visual Basic 4 / 5 / 6)
- Interpretation of an instructors C++ program... (C++)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: I'm pretty new and need some help with classes
- Next Thread: Delphi Code Examples
| Thread Tools | Search this Thread |





