David Nugent David Nugent - 1 month ago 6
Pascal Question

Totals from a multidimensional array in PASCAL

I have a program where I take user input info about products made in a factory and split the information into 2 tables. The first table is for good products and the second is for bad. Each table represents how many of each product (10 products) are made by each machine (7 machines). There are supposed to be totals on the Y-axis (for total products made per machine) and X-axis (for total of a specific product across all machines)

I have a total on the Y-axis, but it always comes out wrong. It shows 10 by default instead of 0 and when you add one to it, it becomes 9 instead of 1.

Here is the code:

program Production8;

uses crt;

var
machine: array of char;
machineindex: integer;
product: array of integer;
quality: array of integer;
min, number: integer;
i, j, k, extra, total: integer;
myTable: array[0..6,0..9,0..1] of integer;


procedure Header;

begin
writeln('Production Overview');
writeln();
end;
procedure Quit;

begin
writeln();
writeln('To Quit hit <ENTER>');
readln;
end;

procedure ShowTable(pQuality: integer; pTitle: string);

begin
writeln(pTitle);
writeln(' 0 1 2 3 4 5 6 7 8 9 T');
for i := 0 to 6 do
begin
total := 0;
write(CHR(65+i),' ');
for j := 0 to 9 do
begin
write(myTable[i,j,pQuality], ' ');
if myTable[machineindex,(product[number]),(quality[number])]
= myTable[i,j,pQuality] then
begin
total:= total + 1;
end;
if j = 9 then
begin
write (total);
end;
end;
writeln('');
end;
writeln ('T');
end;

begin
for i:= 0 to 6 do
for j:= 0 to 9 do
for k:= 0 to 1 do
myTable[i, j, k]:= 0;
i:= 0;
j:= 0;
number := 0;
min := 1;
extra := 1;
SETLENGTH( machine, min );
SETLENGTH( product, min );
SETLENGTH( quality, min );
Header;
writeln('Input Machine ID ( A, B, C, D, E, F or G ). Input "*" to stop');
readln(machine[number] );
while (machine[number] <> '*') do
begin
while ( ORD( machine[number] ) < 65 ) or ( ORD( machine[number] ) > 71 ) do
begin
writeln('Input Invalid. Please try again.');
readln(machine[number] );
end;
clrscr;
Header;
machineindex := (ORD(machine[number])-65);
writeln('Input Product Number ( 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9 ) ');
readln(product[number] );
while ( product[number] < 0 ) or ( product[number] > 9 ) do
begin
writeln('Input Invalid. Please try again.');
readln(product[number] );
end;
clrscr;
Header;
writeln('Quality Control Check. Input 0 for GOOD or 1 for BAD.');
readln(quality[number] );
while ( quality[number] <> 0 ) and ( quality[number] <> 1 ) do
begin
writeln('Input Invalid. Please try again.');
readln(quality[number] );
end;
clrscr;
Header;
myTable[machineindex,(product[number]),(quality[number])] :=
myTable[machineindex,(product[number]),(quality[number])] + 1;
number := number + 1;
if number = LENGTH(machine) then
begin
SETLENGTH(machine, LENGTH(machine)+extra);
SETLENGTH(product, LENGTH(product)+extra);
SETLENGTH(quality, LENGTH(quality)+extra)
end;
writeln('Input Machine ID ( A, B, C, D, E, F or G ). Input "*" to stop');
readln(machine[number] );
end;
clrscr;
Header;
ShowTable(0, 'Good Products');

writeln();
ShowTable(1, 'Bad Products');
Quit;
end.


At first I thought the problem was with my initialisation, as I didn't have "myTable" initialised. Now I do and the problem persists. Where am I going wrong?

Also, I'm not sure how to achieve a total on the X-axis. How can I go about that?

Answer

the summation of the var "total" is not correct.

I tried to make the code work without making to many design changes to your code:

procedure ShowTable(pQuality: integer; pTitle: string);
var totalx: integer;
    totaly: array[0..9] of integer;
begin
  // initialize array totaly
  for j:= 0 to 9 do 
    totaly[j] := 0;

  // output header
  writeln(pTitle);
  writeln('  0   1   2   3   4   5   6   7   8   9   T');

  // iterate machines
  for i := 0 to 6 do
  begin
    totalx := 0;
    write(CHR(65+i),' ');

    for j := 0 to 9 do
    begin
      write(myTable[i,j,pQuality], '   ');

      if myTable[i,j,pQuality] = 1 then
      begin
        totalx:= totalx + 1;
        totaly[j] := totaly[j] + 1;
      end;

      if j = 9 then
      begin
        write (totalx);
      end;
    end;

    writeln('');
  end;

  write('T ');
  total := 0;

  for j := 0 to 9 do
  begin
    total := total + totaly[j];
    write(totaly[j], '   ');
  end;
  write(total);

end;

Generally try to reduce your use of global variables and use more comments. You should also consider object oriented concepts...

Comments