Solved

Reading Information From Arrays

Posted on 2000-03-23
5
385 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Interbase BDE 4 454
delphi 8 620
1 paragraph C code to translate into Delphi code 3 230
how do i remove white background in node inside virtual TVirtualStringTree? 12 92
When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now