[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# Averages Program

Posted on 1999-11-18
Medium Priority
256 Views
I am having a bit of a problem completing this task and i was hoping someone could help me! I'm only using simple pascal language. Heres the question:

Design a program that accepts numbers (ending with -999 as a rogue value) and the prints out the average and also each number input. Beside each number is the difference from the average (obtained by subtracting the average from it).

So theres the question! And heres the program i have done so far...except its not very good and doesn't work yet!! Also i have to use an array with VERY SIMPLE LANGUAGE!!! Heres my program:

Program Averages;
Uses Crt;

VAR
numbers:array[1..100] of integer;
number,total,difference,i:integer;
average:real;

Procedure Main;
Begin
Clrscr;
writeln('Averages Program');
Total:=0;

For i:=1 to 10 {amount in list} do
Begin
total:=total+numbers[i];
average:=total/10;
difference:=numbers[i]-average;
End;

writeln('',number,' ',difference);
writeln('The Average is ',average);

End.

Thanks.
0
Question by:emmarichardson
• 5
• 3

LVL 1

Author Comment

ID: 2218010
0

LVL 12

Expert Comment

ID: 2218417
Simple enough? Is this what you want?

Program Averages;

uses crt;

var Numbers : array[1..100] of integer;
Number  : integer;
Sum     : longint;
i,total : integer;
Average : real;

begin
clrscr;
writeln('Average program   Enter -999 to exit');
total := 0;
i := 1;
repeat
if Number <> -999 then begin
Numbers[i] := Number;
inc(Sum,Number);
inc(total);
inc(i);
end;
until (Number = -999) or (i > 100);

if total > 0 then begin
Average := Sum / total;
Writeln('Average is : ',Average :0:2);
For i := 1 to total do begin
writeln(Numbers[i]:6,' - ',Average:0:2,' = ',(Numbers[i]-Average):9:2);
if i mod 24 = 0 then readkey;
end;
end;
end.
0

LVL 1

Author Comment

ID: 2221999
well, that program is ok, but the thing is that  i need to use simpler language.for example i don't use 'longint' or 'readkey'.
If you could leave these bits out and just stick to simple 'readln' etc.
0

LVL 12

Accepted Solution

Hypo earned 200 total points
ID: 2222976

Longint can be replaced with integer... however, there might be an overflow so that the average value will not be correct. An Integer variable can hold any value from -32768..32767
Longint ,is a Long INTeger, wich can hold the values from -2147483648..2147483647,
So, as long as the total sum is less than 32767 you can use integer instead of longint without any problems.

you could also replace: writeln(Numbers[i]:6,' - ',Average:0:2,' = ',(Numbers[i]-Average):9:2); with
writeln(Numbers[i],' - ',Average,' = ',Numbers[i]-Average);
But If you do, the real values will be written as exponetial values, 1.00000000E+1 for instance.

This line can be removed :
If i mod 24 = 0 then readkey;

Other than that I don't know how to make the code any simplier...
0

LVL 1

Author Comment

ID: 2222982
Thanks, but what does the  (for example) 6 represent ?  (Numbers[i]:6,)
0

LVL 12

Expert Comment

ID: 2223331
Numbers[i]:6 makes the write or writeln procedure to make the size of the variable at least 6 digits.
e.g. if Numbers[i] = 123 it has 3 digits, but since we pass :6 after the variable, it'll be considered to have 6 characters by write/writeln nomatterwhat the actual size is.

So if
Writeln(Numbers[i],'!'); would give the output:
123!
Writeln(Numbers[i]:6,'!'); would give:
123   !

if you use :0:2 as we do when we write the average variable, :0 = the minimum number of characters in output, :2 = number of visible decimals in output!
0

LVL 1

Author Comment

ID: 2223672
0

LVL 1

Author Comment

ID: 2229110
I've just tried out the program which you suggested and i have found that it does not work! At the last 'end' of the program it says its not complete! WHY?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Can you run Linux on a Windows system?  Yep.  Here's how.
A recent study by Google illustrates that almost 84% of patients go for both online and offline sources for hospital research. What do you think, “Is the Healthcare Industry the next to be disrupted by Digital Marketing?”
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
###### Suggested Courses
Course of the Month11 days, 8 hours left to enroll