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
Solved

Reading Information From Arrays

Posted on 2000-03-23
5
431 Views
Last Modified: 2010-04-16
I'm working on a little program teaching myself about arrays but have come to a stop at the present.  I have writtern and compiled the progame but for some reason that I just can not see not all of the information held within an array is being displayed.  The program is based on a billing system with 5 customer over 3 months, if you can help me I would be most grateful :0)

Program Electricty_Bill;
Uses Wincrt;

Var Units,A,B:Integer;
Var UnitCharge,SubTotal,VAT:Real;
Var Month1:Array[1..5] OF Integer;
Var Month2:Array[1..5] OF Integer;
Var Month3:Array[1..5] OF Integer;
Var CustName:Array[1..5] OF String;
Var CustNo:Array[1..5] Of Real;
Var Total1:Array[1..5] OF Real;
Var Total2:Array[1..5] OF Real;
Var Total3:Array[1..5] OF Real;

Function Charge (Units:Integer):Real;  
Begin
CASE Units OF                          
0..300     : UnitCharge:=0.09;
301..500   : UnitCharge:=0.08;          
501..800   : UnitCharge:=0.07;
801..1000  : UnitCharge:=0.06;
ELSE         UnitCharge:=0.05;
end;
Charge:=((Units*UnitCharge)+12.57)*1.175;                    
End;

Procedure Collect_Data;
begin
Write('Please Enter your Customer Number...>>');      
readln(Custno[A]);                                      
Write('Please Enter Your Name...>>');                  
readln(Custname[A]);                                    
Write('Enter First Months Amount Of Units Use...>>');  
readln(Month1[B]);                                      
Write('Enter Second Months Amount Of Units Used...>>');
readln(Month2[B]);
Write('Enter Third Months Amount Of Units Used...>>');                            
readln(Month3[B]);
Clrscr;
end;

Procedure Calculate_Charge;
begin
     For B:= 1 To 5 Do                          
     Total1[B]:=Charge(Month1[B]);    
     Total2[B]:=Charge(Month2[B]);    
     Total3[B]:=Charge(Month3[B]);    
  end;
                               
Procedure Display_Output;
Begin
Writeln('Customer Number>>',CustNo[1]:4:0,'Customer Name>>',CustName[1]);
Writeln('Total Units Used>>',Month1[1],'Total Charge>>',Total1[1]:4:2);
Writeln('Total Units Used>>',Month2[1],'Total Charge>>',Total2[1]:4:2);
Writeln('Total Units Used>>',Month3[1],'Total Charge>>',Total3[1]:4:2);
Writeln('');
Writeln('Customer Number>>',CustNo[2]:4:0,'Customer Name>>',CustName[2]);
Writeln('Total Units Used>>',Month1[2],'Total Charge>>',Total1[2]:4:2);
Writeln('Total Units Used>>',Month2[2],'Total Charge>>',Total2[2]:4:2);
Writeln('Total Units Used>>',Month3[2],'Total Charge>>',Total3[2]:4:2);
Writeln('');
Writeln('Customer Number>>',CustNo[3]:4:0,'Customer Name>>',CustName[3]);
Writeln('Total Units Used>>',Month1[3],'Total Charge>>',Total1[3]:4:2);
Writeln('Total Units Used>>',Month2[3],'Total Charge>>',Total2[3]:4:2);
Writeln('Total Units Used>>',Month3[3],'Total Charge>>',Total3[3]:4:2);
Writeln('');
Writeln('Customer Number>>',CustNo[4]:4:0,'Customer Name>>',CustName[4]);
Writeln('Total Units Used>>',Month1[4],'Total Charge>>',Total1[4]:4:2);
Writeln('Total Units Used>>',Month2[4],'Total Charge>>',Total2[4]:4:2);
Writeln('Total Units Used>>',Month3[4],'Total Charge>>',Total3[4]:4:2);
Writeln('');                  
Writeln('Customer Number>>',CustNo[5]:4:0,'Customer Name>>',CustName[5]);
Writeln('Total Units Used>>',Month1[5],'Total Charge>>',Total1[5]:4:2);
Writeln('Total Units Used>>',Month2[5],'Total Charge>>',Total2[5]:4:2);
Writeln('Total Units Used>>',Month3[5],'Total Charge>>',Total3[5]:4:2);
end;

Begin
For B:=1 To 5 Do
For A:=1 To 5 Do      
Begin
Collect_Data;
Calculate_Charge;
end;
Display_Output;
end;
End.


0
Comment
Question by:3_Armed_Human
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Hypo
ID: 2650883
A little advice:

Instead of making three arrays called Month1, Month2 and Month3, You can make one 2Dimensional array called months...

Like this,
  var Months : array[1..3,1..5] of integer;

and then you use it like this: Months[2,1] := 10;
or: Months[CustomerNumber,MonthNumber] := 10;

The use of records and 2D arrays could make your example much smaller.
0
 
LVL 12

Expert Comment

by:Hypo
ID: 2650893
oh... sorry,
 Months[CustomerNumber,MonthNumber] := 10; should be the opposite...

Months[MonthNumber,CustomerNumber] := 10;
is what i meant!
0
 
LVL 48

Expert Comment

by:dbrunton
ID: 2652487
Here's part of the problem I think - still checking code.  You have missed out a begin and end


Procedure Calculate_Charge;
begin
  For B:= 1 To 5 Do
    begin
      Total1[B]:=Charge(Month1[B]);
      Total2[B]:=Charge(Month2[B]);
      Total3[B]:=Charge(Month3[B]);
    end;
end;
0
 

Expert Comment

by:ScottyBhoy
ID: 2652687
Set a variable called index as an integer in the chosen procedure.  Then when you want to use the infomation from the array you can type

WITH Month1[index] Do
Etc
Etc
Ect

I dont know why this changes things, but it does.
0
 
LVL 48

Accepted Solution

by:
dbrunton earned 50 total points
ID: 2653973
This works.  Code has been cleaned up greatly.  Major error.  Redundant for loop in original code.

for loop1
  for loop2

Error.  Redundant variable B.  B is not needed.
Use variable A as index in procedures.

Program Electricty_Bill;
{ Uses Wincrt; }

const
  Max = 5;

Var
  A : Integer;
  UnitCharge, SubTotal, VAT : Real;
  Month1 : Array[1..Max] OF Integer;
  Month2 : Array[1..Max] OF Integer;
  Month3 : Array[1..Max] OF Integer;
  CustName : Array[1..Max] OF String;
  CustNo : Array[1..Max] Of Real;
  Total1 : Array[1..Max] OF Real;
  Total2 : Array[1..Max] OF Real;
  Total3 : Array[1..Max] OF Real;

Function Charge(Units : Integer) : Real;
Begin
  CASE Units OF
    0..300     : UnitCharge:=0.09;
    301..500   : UnitCharge:=0.08;
    501..800   : UnitCharge:=0.07;
    801..1000  : UnitCharge:=0.06;
  ELSE        
   UnitCharge := 0.05;
    end;
 Charge := ((Units*UnitCharge)+12.57)*1.175;
End;

Procedure Collect_Data;
begin
  Write('Please Enter your Customer Number...>>');
  readln(Custno[A]);
  Write('Please Enter Your Name...>>');
  readln(Custname[A]);
  Write('Enter First Months Amount Of Units Use...>>');
  readln(Month1[A]);
  Write('Enter Second Months Amount Of Units Used...>>');
  readln(Month2[A]);
  Write('Enter Third Months Amount Of Units Used...>>');
  readln(Month3[A]);
  ;
end;

Procedure Calculate_Charge;
begin
  Total1[A] := Charge(Month1[A]);
  Total2[A] := Charge(Month2[A]);
  Total3[A] := Charge(Month3[A]);
end;

Procedure Display_Output;
Begin
  Writeln('Customer Number>>',CustNo[A]:4:0,'Customer Name>>',CustName[A]);
  Writeln('Total Units Used>>',Month1[A],'Total Charge>>',Total1[A]:4:2);
  Writeln('Total Units Used>>',Month2[A],'Total Charge>>',Total2[A]:4:2);
  Writeln('Total Units Used>>',Month3[A],'Total Charge>>',Total3[A]:4:2);
  Writeln('');
end;

Begin
  For A := 1 To Max Do
    Begin
      Collect_Data;
      Calculate_Charge;
    end;
  For A := 1 to Max Do
    Display_Output;
End.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

766 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