Solved

Reading Information From Arrays

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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

In this article we will learn how to backup a VMware farm using Nakivo Backup & Replication. In this tutorial we will install the software on a Windows 2012 R2 Server.
The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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