Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

output average number of cigarettes and average weight

Posted on 2000-03-26
4
Medium Priority
?
301 Views
Last Modified: 2010-04-16
This program needs to output the average number of cigarettes and the average weight for the Accepted applicants.  The way I've done it here shows the averages for each individual accepted applicant. If I var the averages as anything else, then I get an error on these lines: (numb+...)/4;
(wt..)/4, however, if I leave them vared as real, then my results come out in exponential form(Example: 006 + 000002+E07)   Here it is:

var name: string;
    numb: integer;
    wt: integer;
    AppFile: text;
    avgnumb: real;
    avgwt: real;
    posit: string;

procedure GETDATA (var localAppFile: text;
                   var localname: string;
                   var localnumb: integer;
                   var localwt: integer);

  begin
    readln (localAppFile, localname);
    readln (localAppFile, localnumb);
    readln (localAppFile, localwt);
  end; {GETDATA}

procedure POSITION(var localnumb: integer;
                   localwt: integer;
                   posit: string);

    begin
      if (numb < 10) and (wt < 180) then
        writeln ('Accepted ')
      else
        begin
           writeln ('Rejected ');
           writeln ('Your weight is more than the given weight ');
           writeln ('The number of cigarettes you smoke is over the limit ');
        end;
    end; {POSITION}

procedure ACCEPTED (var avgnumb: real;
                    var avgwt: real;
                    posit: string);
   begin
      if (posit = 'Accepted') then
         avgnumb := (numb + numb + numb + numb) / 4;
         avgwt := (wt + wt + wt + wt) / 4;
           writeln ('Average number of cigarettes for accepted applicants: ' , avgnumb);
           writeln ('Average weight of accepted applicants: ' , avgwt);
   end;{ACCEPTED}


begin {main}
  assign (AppFile, 'A:\AppFile2.dat');
  reset (AppFile);
  while not seekeof (AppFile) do
     begin
        GETDATA (AppFile, name, numb, wt);
        POSITION (numb, wt);
        ACCEPTED (avgnumb, avgwt, posit);
     end;
  close (Appfile)
end.


0
Comment
Question by:kazooie21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
dbrunton earned 20 total points
ID: 2659897
Not tested but compiled correctly for me.  I hope that you can see the errors that you are making.

program form;

var
  name : string;
  numb : integer;
  totalnumb : integer;
  wt : integer;
  totalwt : integer;
  applicants : integer;
  AppFile : text;
  avgnumb : real;
  avgwt : real;
  posit : string;

procedure GETDATA (var localAppFile : text;
var localname : string;
var localnumb : integer;
var localwt : integer);

begin
  readln(localAppFile, localname);
  readln(localAppFile, localnumb);
  readln(localAppFile, localwt);
end; {GETDATA}

procedure POSITION(localnumb: integer;
localwt : integer;
var posit : string);

begin
if (localnumb < 10) and (localwt < 180) then
  begin
    writeln ('Accepted ');
    posit := 'Accepted';
  end
else
  begin
    writeln('Rejected ');
    writeln('Your weight is more than the given weight ');
    writeln('The number of cigarettes you smoke is over the limit ');
    posit := 'Not accepted';
  end;
end; {POSITION}

procedure ACCEPTED(posit : string; localwt : integer; localnum : integer);
begin
  if (posit = 'Accepted') then
    begin
      totalwt := localwt + totalwt;
      totalnumb := totalnumb + localnum;
      inc(applicants);
    end;
end; {ACCEPTED}

begin {main}
  totalnumb := 0;
  totalwt := 0;
  applicants := 0;
  assign(AppFile, 'A:\AppFile2.dat');
  reset(AppFile);
  while not seekeof(AppFile) do
    begin
      GETDATA(AppFile, name, numb, wt);
      POSITION(numb, wt, posit);
      ACCEPTED(posit, wt, numb);
    end;
  close(Appfile);
  avgnumb := totalnumb/applicants;
  avgwt := totalwt/applicants;
  writeln('Average weight for accepted applicants is ', avgwt:6:2);
  writeln('Average number of cigarettes for accepted applicants is ', avgnumb:6:2);
end.
0
 

Author Comment

by:kazooie21
ID: 2661645
When I ran this, I got 'Error 200: division by 0'
0
 
LVL 49

Expert Comment

by:dbrunton
ID: 2662970
Replace the last five lines with these lines and try again.

if applicants  <> 0 then
begin
  avgnumb := totalnumb/applicants;
  writeln('Average number of cigarettes for accepted applicants is',
avgnumb:6:2);
  avgwt := totalwt/applicants;
  writeln('Average weight for accepted applicants is ', avgwt:6:2);
end
 else
   writeln('No candidates found');
end.

0
 
LVL 3

Expert Comment

by:vikiing
ID: 2663422
>>   if (posit = 'Accepted') then
         avgnumb := (numb + numb + numb + numb) / 4;
         avgwt := (wt + wt + wt + wt) / 4;
         . . .

You've done the same mistake than before: when you want to control more than ONE instruction within an IF, a begin-else pair is mandatory.


>>       avgwt := (wt + wt + wt + wt) / 4;

Dbrunton's correction was perfect, but perhaps you don't notice the reason why you're wrong. If you say wt + wt + wt + wt , you're using the SAME variable each time you add. Thus, you're adding four times the last value held by WT variable, which is NOT the idea of summing different values; that's why you have an instruction like this:

    totalwt := localwt + totalwt;

this is, an adder, which totalizes the corresponding value (localwt) each time it is executed.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

664 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