Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help working with 2 parellel arrays, one needs to be a 2d array

jettamanjettaman Member Posts: 19
Hi all. I'm trying to read from a text file which contains 5 names and 4 sets of numbers. Total of 5 lines.
I need to read the names into a single array and the numbers into a 2D Array.

I need to display that out and then have the user push ENTER, then display the names, numbers (multiplyed by 75.5) and then add and display the totals for the rows and columns.

I have been able to display the names and numbers but I don't think I've got the numbers in a 2D Array. I think the entire text file is in a single array.

I haven't started on the second portion of this program yet, simply because I want to get the first part correct and then try and work it from there.

Here is the code I've got and I really do appreciate the help everybody.

program salesreport;

{$APPTYPE CONSOLE}

uses
SysUtils;

TYPE
Names = ARRAY [1..5] OF STRING;
Sales = ARRAY [1..5,1..5] OF real;
totals = ARRAY [1..5,1..6] OF real;

{******************************************************************************}

PROCEDURE GetFile (VAR Infile: text);

VAR
Filename: string;

Begin
Writeln;
Writeln ('Enter the file to be processed: ');
Writeln ('Be sure to enter the entire path!');
Readln (Filename);
Assign (Infile, Filename);
writeln;
End;

{******************************************************************************}

PROCEDURE ReadData (VAR Infile: text; VAR count: integer; VAR name: names);

Begin
Reset (infile);
count := 0;
While not EOF(infile) do
Begin
count := count + 1;
readln (infile, name[count]);
writeln (name[count]);
End;
close (infile);
End;

{******************************************************************************}

PROCEDURE totalsales (VAR infile: text; VAR total: totals);

VAR
row, column: integer;

Begin
Reset (infile);
for row := 1 to 5 do
Begin
for column := 1 to 6 do
write (infile, total[row,column]);
writeln (infile);
End;
End;

{******************************************************************************}

VAR
Infile: text;
Ans: char;
Name: Names;
count: integer;

Begin
Repeat
Getfile (infile);
Readdata (infile, count, name);

Repeat
writeln;
writeln ('Do you want to test another file? Y/N');
readln (Ans);
UNTIL (Ans = 'Y') OR (Ans = 'y') OR (Ans = 'N') OR (Ans = 'n');
UNTIL (Ans = 'N') OR (Ans = 'n');
End.

Thanks again.
Ross

Comments

  • zibadianzibadian Member Posts: 6,349
    : Hi all. I'm trying to read from a text file which contains 5 names and 4 sets of numbers. Total of 5 lines.
    : I need to read the names into a single array and the numbers into a 2D Array.
    :
    : I need to display that out and then have the user push ENTER, then display the names, numbers (multiplyed by 75.5) and then add and display the totals for the rows and columns.
    :
    : I have been able to display the names and numbers but I don't think I've got the numbers in a 2D Array. I think the entire text file is in a single array.
    :
    : I haven't started on the second portion of this program yet, simply because I want to get the first part correct and then try and work it from there.
    :
    : Here is the code I've got and I really do appreciate the help everybody.
    :
    : program salesreport;
    :
    : {$APPTYPE CONSOLE}
    :
    : uses
    : SysUtils;
    :
    : TYPE
    : Names = ARRAY [1..5] OF STRING;
    : Sales = ARRAY [1..5,1..5] OF real;
    : totals = ARRAY [1..5,1..6] OF real;
    :
    : {******************************************************************************}
    :
    : PROCEDURE GetFile (VAR Infile: text);
    :
    : VAR
    : Filename: string;
    :
    : Begin
    : Writeln;
    : Writeln ('Enter the file to be processed: ');
    : Writeln ('Be sure to enter the entire path!');
    : Readln (Filename);
    : Assign (Infile, Filename);
    : writeln;
    : End;
    :
    : {******************************************************************************}
    :
    : PROCEDURE ReadData (VAR Infile: text; VAR count: integer; VAR name: names);
    :
    : Begin
    : Reset (infile);
    : count := 0;
    : While not EOF(infile) do
    : Begin
    : count := count + 1;
    : readln (infile, name[count]);
    : writeln (name[count]);
    : End;
    : close (infile);
    : End;
    :
    : {******************************************************************************}
    :
    : PROCEDURE totalsales (VAR infile: text; VAR total: totals);
    :
    : VAR
    : row, column: integer;
    :
    : Begin
    : Reset (infile);
    : for row := 1 to 5 do
    : Begin
    : for column := 1 to 6 do
    : write (infile, total[row,column]);
    : writeln (infile);
    : End;
    : End;
    :
    : {******************************************************************************}
    :
    : VAR
    : Infile: text;
    : Ans: char;
    : Name: Names;
    : count: integer;
    :
    : Begin
    : Repeat
    : Getfile (infile);
    : Readdata (infile, count, name);
    :
    : Repeat
    : writeln;
    : writeln ('Do you want to test another file? Y/N');
    : readln (Ans);
    : UNTIL (Ans = 'Y') OR (Ans = 'y') OR (Ans = 'N') OR (Ans = 'n');
    : UNTIL (Ans = 'N') OR (Ans = 'n');
    : End.
    :
    : Thanks again.
    : Ross
    :
    You never read the numbers into an array anywhere in your code.
  • jettamanjettaman Member Posts: 19
    I did read the numbers into the 'name' array. That's my problem. I need to know how to read them into a '2d array'. That's one of the things I need help with.


    : : Hi all. I'm trying to read from a text file which contains 5 names and 4 sets of numbers. Total of 5 lines.
    : : I need to read the names into a single array and the numbers into a 2D Array.
    : :
    : : I need to display that out and then have the user push ENTER, then display the names, numbers (multiplyed by 75.5) and then add and display the totals for the rows and columns.
    : :
    : : I have been able to display the names and numbers but I don't think I've got the numbers in a 2D Array. I think the entire text file is in a single array.
    : :
    : : I haven't started on the second portion of this program yet, simply because I want to get the first part correct and then try and work it from there.
    : :
    : : Here is the code I've got and I really do appreciate the help everybody.
    : :
    : : program salesreport;
    : :
    : : {$APPTYPE CONSOLE}
    : :
    : : uses
    : : SysUtils;
    : :
    : : TYPE
    : : Names = ARRAY [1..5] OF STRING;
    : : Sales = ARRAY [1..5,1..5] OF real;
    : : totals = ARRAY [1..5,1..6] OF real;
    : :
    : : {******************************************************************************}
    : :
    : : PROCEDURE GetFile (VAR Infile: text);
    : :
    : : VAR
    : : Filename: string;
    : :
    : : Begin
    : : Writeln;
    : : Writeln ('Enter the file to be processed: ');
    : : Writeln ('Be sure to enter the entire path!');
    : : Readln (Filename);
    : : Assign (Infile, Filename);
    : : writeln;
    : : End;
    : :
    : : {******************************************************************************}
    : :
    : : PROCEDURE ReadData (VAR Infile: text; VAR count: integer; VAR name: names);
    : :
    : : Begin
    : : Reset (infile);
    : : count := 0;
    : : While not EOF(infile) do
    : : Begin
    : : count := count + 1;
    : : readln (infile, name[count]);
    : : writeln (name[count]);
    : : End;
    : : close (infile);
    : : End;
    : :
    : : {******************************************************************************}
    : :
    : : PROCEDURE totalsales (VAR infile: text; VAR total: totals);
    : :
    : : VAR
    : : row, column: integer;
    : :
    : : Begin
    : : Reset (infile);
    : : for row := 1 to 5 do
    : : Begin
    : : for column := 1 to 6 do
    : : write (infile, total[row,column]);
    : : writeln (infile);
    : : End;
    : : End;
    : :
    : : {******************************************************************************}
    : :
    : : VAR
    : : Infile: text;
    : : Ans: char;
    : : Name: Names;
    : : count: integer;
    : :
    : : Begin
    : : Repeat
    : : Getfile (infile);
    : : Readdata (infile, count, name);
    : :
    : : Repeat
    : : writeln;
    : : writeln ('Do you want to test another file? Y/N');
    : : readln (Ans);
    : : UNTIL (Ans = 'Y') OR (Ans = 'y') OR (Ans = 'N') OR (Ans = 'n');
    : : UNTIL (Ans = 'N') OR (Ans = 'n');
    : : End.
    : :
    : : Thanks again.
    : : Ross
    : :
    : You never read the numbers into an array anywhere in your code.
    :

  • zibadianzibadian Member Posts: 6,349
    The way to load the numbers into a 2-D array depend greatly on the format of the text file. If the file uses a certain delimiter to differentiate between the fields, then you can split the name using the Pos(), Copy(), and StrToInt() functions. If the fields have a set length, then only the last two function are necessary. Without knowing the file format, t is impossible for me to show a code, which extracts or read the numbers.

    : I did read the numbers into the 'name' array. That's my problem. I need to know how to read them into a '2d array'. That's one of the things I need help with.
    :
    :
    : : : Hi all. I'm trying to read from a text file which contains 5 names and 4 sets of numbers. Total of 5 lines.
    : : : I need to read the names into a single array and the numbers into a 2D Array.
    : : :
    : : : I need to display that out and then have the user push ENTER, then display the names, numbers (multiplyed by 75.5) and then add and display the totals for the rows and columns.
    : : :
    : : : I have been able to display the names and numbers but I don't think I've got the numbers in a 2D Array. I think the entire text file is in a single array.
    : : :
    : : : I haven't started on the second portion of this program yet, simply because I want to get the first part correct and then try and work it from there.
    : : :
    : : : Here is the code I've got and I really do appreciate the help everybody.
    : : :
    : : : program salesreport;
    : : :
    : : : {$APPTYPE CONSOLE}
    : : :
    : : : uses
    : : : SysUtils;
    : : :
    : : : TYPE
    : : : Names = ARRAY [1..5] OF STRING;
    : : : Sales = ARRAY [1..5,1..5] OF real;
    : : : totals = ARRAY [1..5,1..6] OF real;
    : : :
    : : : {******************************************************************************}
    : : :
    : : : PROCEDURE GetFile (VAR Infile: text);
    : : :
    : : : VAR
    : : : Filename: string;
    : : :
    : : : Begin
    : : : Writeln;
    : : : Writeln ('Enter the file to be processed: ');
    : : : Writeln ('Be sure to enter the entire path!');
    : : : Readln (Filename);
    : : : Assign (Infile, Filename);
    : : : writeln;
    : : : End;
    : : :
    : : : {******************************************************************************}
    : : :
    : : : PROCEDURE ReadData (VAR Infile: text; VAR count: integer; VAR name: names);
    : : :
    : : : Begin
    : : : Reset (infile);
    : : : count := 0;
    : : : While not EOF(infile) do
    : : : Begin
    : : : count := count + 1;
    : : : readln (infile, name[count]);
    : : : writeln (name[count]);
    : : : End;
    : : : close (infile);
    : : : End;
    : : :
    : : : {******************************************************************************}
    : : :
    : : : PROCEDURE totalsales (VAR infile: text; VAR total: totals);
    : : :
    : : : VAR
    : : : row, column: integer;
    : : :
    : : : Begin
    : : : Reset (infile);
    : : : for row := 1 to 5 do
    : : : Begin
    : : : for column := 1 to 6 do
    : : : write (infile, total[row,column]);
    : : : writeln (infile);
    : : : End;
    : : : End;
    : : :
    : : : {******************************************************************************}
    : : :
    : : : VAR
    : : : Infile: text;
    : : : Ans: char;
    : : : Name: Names;
    : : : count: integer;
    : : :
    : : : Begin
    : : : Repeat
    : : : Getfile (infile);
    : : : Readdata (infile, count, name);
    : : :
    : : : Repeat
    : : : writeln;
    : : : writeln ('Do you want to test another file? Y/N');
    : : : readln (Ans);
    : : : UNTIL (Ans = 'Y') OR (Ans = 'y') OR (Ans = 'N') OR (Ans = 'n');
    : : : UNTIL (Ans = 'N') OR (Ans = 'n');
    : : : End.
    : : :
    : : : Thanks again.
    : : : Ross
    : : :
    : : You never read the numbers into an array anywhere in your code.
    : :
    :
    :

  • ibyiby Member Posts: 323
    : The way to load the numbers into a 2-D array depend greatly
    : on the format of the text file. If the file uses a certain
    : delimiter to differentiate between the fields, then you can
    : split the name using the Pos(), Copy(), and StrToInt() functions.
    : If the fields have a set length, then only the last two function
    : are necessary. Without knowing the file format, it is impossible
    : for me to show a code, which extracts or read the numbers.
    :

    Exactly. File format is needed [b]and[/b] a sample input file.
    Maybe something like this?

    [code]
    name1 01 02 03 04
    name2 05 06 07 08
    name3 09 10 11 12
    name4 13 14 15 16
    name5 17 18 19 20
    (etc.)...
    [/code]

    Now important thing is what is delimiter (separator)?
    Is it a space character? Is the length of the NAMEs fixed
    or not? Another question, where do you want the numbers
    read into? The sample code you posted is missing some
    variable declarations too. Here is something that
    might work for you:

    [code]
    var OneLine : string; { read one line from text and then extract values }
    InName : array[1..5] of string; { names array }
    InNumber : array[1..5,1..4] of real; { values array }

    Count : integer; { array indexes }
    Column : byte;

    SpacePos : byte; { string processing indexes }
    Leftover : byte;

    s : string; { used for type conversion }
    v : real;
    c : integer;

    { remove leading space characters since this is used as delimiter }
    procedure StartClean;
    begin
    while pos(' ',OneLine)=1 do delete(OneLine,1,1);
    end;

    begin
    Count:=1; { work on first element for now...}

    { simulate line in input text file, number of delimiter character
    (spaces) is not important }
    OneLine:=' TheName 1.000 2 3.0 4.25';
    writeln;
    writeln(count,') raw: ',OneLine);

    StartClean;
    SpacePos:=pos(' ',OneLine); { this is just to make it more readable }
    Leftover:=length(OneLine)-SpacePos; { ... and this one too}

    s:=copy(OneLine,1,SpacePos-1); { get the NAME and remove it from input }
    OneLine:=copy(OneLine,SpacePos+1,Leftover);

    InName[count]:=s; { save it into array }

    For Column:=1 to 4 do
    begin
    StartClean;
    If column<4 then SpacePos:=pos(' ',OneLine)
    else SpacePos:=Leftover+1;
    Leftover:=length(OneLine)-SpacePos;

    s:=copy(OneLine,1,SpacePos-1);{ get one NUMBER and remove it from input }
    OneLine:=copy(OneLine,SpacePos+1,Leftover);

    val(s,v,c); { convert it to number... }
    if c<>0 then begin { stop program if not ok...}
    writeln;
    writeln('Invalid input...');
    halt(1);
    end;

    InNumber[count,column]:=v*75.5; { save it into array }
    end;
    write(count,') processed: ',InName[count],' ');
    for column:=1 to 4 do write(InNumber[count,column]:2:2,' ');
    writeln;
    write('Press Enter to exit...'); readln;
    end.
    [/code]

    Iby
  • Justin BibJustin Bib USAMember Posts: 0

    _____ || http://forcoder.org || free video tutorials and ebooks about || R Swift Scratch Java C# PL/SQL MATLAB PHP Assembly Perl C JavaScript Python Objective-C Visual Basic C++ Delphi Visual Basic .NET Ruby Go Crystal Scheme Alice Bash Ada VBScript Hack Dart Julia Clojure Transact-SQL Rust ML Apex D Lua ABAP Logo Scala F# Erlang Prolog Lisp FoxPro COBOL Awk LabVIEW Kotlin Fortran SAS ||

Sign In or Register to comment.