[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Averages Program

Posted on 1999-11-18
8
Medium Priority
?
256 Views
Last Modified: 2010-04-16
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
         readln(numbers[i]);
         total:=total+numbers[i];
         average:=total/10;
         difference:=numbers[i]-average;
         End;

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


End.

Please please could someone, anyone help me?!?!
Thanks.
0
Comment
Question by:emmarichardson
  • 5
  • 3
8 Comments
 
LVL 1

Author Comment

by:emmarichardson
ID: 2218010
Adjusted points to 100
0
 
LVL 12

Expert Comment

by:Hypo
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
  readln(Number);
  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);
  readkey;
  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;
  readkey;
 end;
end.
0
 
LVL 1

Author Comment

by:emmarichardson
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Accepted Solution

by:
Hypo earned 200 total points
ID: 2222976
Well, you can replace all Readkey with Readln without a problem.

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

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

Expert Comment

by:Hypo
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

by:emmarichardson
ID: 2223672
Thanks for your help!
0
 
LVL 1

Author Comment

by:emmarichardson
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question