Howdy, Stranger!

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

Categories

sorting and displaying #'s from a text.file using 3 seperate arrays

jettamanjettaman Member Posts: 19
Hi there. I'm having just a bit of trouble with my currect program.
I'm trying to read #'s from a text.file into 3 seperate arrays, and then display them as such.
Evens:
Odds:
Negitives:
Here's what I've got so far.

program AppropriateArrays;

{$APPTYPE CONSOLE}

uses
SysUtils;

CONST
Min = -49;
Max = 49;

TYPE
Vector = Array [Min..Max] of integer;
Vector1 = Array [Min..Max] of integer;
Vector2 = Array [Min..Max] of integer;


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

PROCEDURE GetFile (VAR Infile:Text);

VAR
FileName : String;

Begin
Writeln;
Writeln ('Enter the path and filename for the file to be processed: ');
Readln (Filename);
writeln;
Assign (Infile, Filename);
Reset (Infile);
End;

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

FUNCTION ReadLine (VAR Infile: Text; Var Negitive: Vector; VAR Positive: Vector1;
VAR Odd: Vector2): integer;

VAR
count: integer;

Begin
count := 0;
if not EOF(Infile) then
Begin
while not EOLN(infile) do
Begin
count := count + 1;
read(infile, Negitive[count]);
read(infile, Positive[count]);
read(infile, Odd[count]);
End;
readln(infile);
End;
ReadLine := count;
End;

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

PROCEDURE DisplayResults (Var Negitive: vector; VAR Positive: vector1;
VAR Odd: vector2; count: integer);

VAR
negitives, positives, odds: integer;

Begin
Writeln;
Writeln;
writeln ('Negitive Numbers: ', negitives);
writeln;
write ('Positive Even Number: ',positives);
writeln;
writeln ('Odd Numbers: ',odds);
writeln;
End;

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

PROCEDURE ProcessLine (VAR Infile: Text; VAR Negitive: Vector;
VAR Positive: Vector1; VAR Odd: Vector2;
VAR negitives,positives,odds: integer);

Var
count, index: integer;

Begin
count := 0;
for index := Min to count do
Begin
If Negitive[count] < 0 then
Negitive := Negitives
else if Positive[count] mod 2 = 0 then
Positive := Positives
else ODD[count] := Odds;
End;
DisplayResults (negitive,positive,odd,count);
end;

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

{Main Program}
VAR
Infile: Text;
Count: Integer;
Negitive, Positive, Odd: Vector;

Begin
GetFile(Infile);
count := ReadLine(infile,Negitive,Positive,Odd);
ProcessLine(Infile,Negitive,Positive,Odd,Index);
writeln;
write ('Push ENTER to exit');
readln;
End.

Thanks for the help.
Ross




«1

Comments

  • ibyiby Member Posts: 323

    Can you be more specific on what exactly you are trying to do?
    I think what you wanted is to read all numbers, analyse them
    and if even put them into EVEN array, if off put them into ODD array
    and if NEGATIVE place them into NEGATIVE array.
    Also you want to keep track of how many elements are in each array.
    Here are also few more things you should look into:
    1) you open, but never close the file.
    2) you declare new types as Vector, Vector1, Vector2 but they
    are all same.

    [code]
    program read_numbers;

    var EvenArr,OddArr,NegArr : array[0..100]of integer;
    EvenIndex, OddIndex,NegIndex : integer;


    function GetFileName:string;
    var s:string;
    begin
    writeln;
    writeln('Enter file name');
    readln(s);
    GetFileName:=s;
    end;


    procedure ReadTextFile(FileName:string);
    var OneNumber : integer;
    TextFile : Text;
    begin
    EvenIndex:=0;
    OddIndex:=0;
    NegIndex:=0;

    assign(TextFile,FileName);
    Reset(TextFile);

    While Not(Eof(TextFile)) do begin
    Read(TextFile,OneNumber);

    If OneNumber<0 Then begin
    Inc(NegIndex);
    NegArr[NegIndex]:=OneNumber;
    End;

    If Odd(OneNumber) Then begin
    Inc(OddIndex);
    OddArr[OddIndex]:=OneNumber;
    End else begin
    Inc(EvenIndex);
    EvenArr[EvenIndex]:=OneNumber;
    End;
    End;

    Close(TextFile);
    End;

    Procedure DisplayResults;
    Begin
    Writeln;
    Writeln;
    writeLn('Negitive Numbers : ', NegIndex);
    writeLn('Even Number : ', EvenIndex);
    writeln('Odd Numbers : ', OddIndex);
    writeln;
    End;

    begin { Main }
    ReadTextFile(GetFileName);
    DisplayResults;
    writeln('Press ENTER to Exit');
    ReadLn;
    end.
    [/code]

    Iby






  • jettamanjettaman Member Posts: 19
    [b][red]This message was edited by the jettaman at 2002-7-13 11:25:55[/red][/b][hr]
    Hi there. Thanks for helping me out here. You were right when you said: "I think what you wanted is to read all numbers, analyse them
    : and if even put them into EVEN array, if off put them into ODD array
    : and if NEGATIVE place them into NEGATIVE array.
    : Also you want to keep track of how many elements are in each array".
    I'm not exactly sure how to read my text.file into those 3 seperate rays and then display them accordingly. These arrays are a bit confusing. :)

    Thanks again
    Ross


    :
    : Can you be more specific on what exactly you are trying to do?
    : I think what you wanted is to read all numbers, analyse them
    : and if even put them into EVEN array, if off put them into ODD array
    : and if NEGATIVE place them into NEGATIVE array.
    : Also you want to keep track of how many elements are in each array.
    : Here are also few more things you should look into:
    : 1) you open, but never close the file.
    : 2) you declare new types as Vector, Vector1, Vector2 but they
    : are all same.
    :
    : [code]
    : program read_numbers;
    :
    : var EvenArr,OddArr,NegArr : array[0..100]of integer;
    : EvenIndex, OddIndex,NegIndex : integer;
    :
    :
    : function GetFileName:string;
    : var s:string;
    : begin
    : writeln;
    : writeln('Enter file name');
    : readln(s);
    : GetFileName:=s;
    : end;
    :
    :
    : procedure ReadTextFile(FileName:string);
    : var OneNumber : integer;
    : TextFile : Text;
    : begin
    : EvenIndex:=0;
    : OddIndex:=0;
    : NegIndex:=0;
    :
    : assign(TextFile,FileName);
    : Reset(TextFile);
    :
    : While Not(Eof(TextFile)) do begin
    : Read(TextFile,OneNumber);
    :
    : If OneNumber<0 Then begin
    : Inc(NegIndex);
    : NegArr[NegIndex]:=OneNumber;
    : End;
    :
    : If Odd(OneNumber) Then begin
    : Inc(OddIndex);
    : OddArr[OddIndex]:=OneNumber;
    : End else begin
    : Inc(EvenIndex);
    : EvenArr[EvenIndex]:=OneNumber;
    : End;
    : End;
    :
    : Close(TextFile);
    : End;
    :
    : Procedure DisplayResults;
    : Begin
    : Writeln;
    : Writeln;
    : writeLn('Negitive Numbers : ', NegIndex);
    : writeLn('Even Number : ', EvenIndex);
    : writeln('Odd Numbers : ', OddIndex);
    : writeln;
    : End;
    :
    : begin { Main }
    : ReadTextFile(GetFileName);
    : DisplayResults;
    : writeln('Press ENTER to Exit');
    : ReadLn;
    : end.
    : [/code]
    :
    : Iby
    :
    :
    :
    :
    :
    :
    :



  • ibyiby Member Posts: 323
    : I'm not exactly sure how to read my text.file into those 3 seperate arrays and then display them accordingly. These arrays are a bit confusing. :)

    Read them into one array (ReadArr), close the file and think
    what would you like do do with the numbers. Maybe something like this:


    [code]

    program read_numbers;

    var ReadArr, EvenArr,OddArr,NegArr : array[0..100]of integer;
    ReadIndex, EvenIndex, OddIndex,NegIndex : integer;
    i:integer;


    function GetFileName:string;
    var s:string;
    begin
    writeln;
    writeln('Enter file name');
    readln(s);
    GetFileName:=s;
    end;


    procedure ReadTextFile(FileName:string);
    var OneNumber : integer;
    TextFile : Text;
    begin
    assign(TextFile,FileName);
    Reset(TextFile);
    ReadIndex:=0;

    While Not(Eof(TextFile)) do
    Begin
    Inc(ReadIndex);
    Read(TextFile,ReadArr[ReadIndex]);
    End;

    Close(TextFile);
    End;

    Procedure ProcessNegative;
    begin
    NegIndex:=0;
    For i:=1 to ReadIndex do
    Begin
    If ReadArr[i]<0 Then begin
    Inc(NegIndex);
    NegArr[NegIndex]:=ReadArr[i];
    End;
    End;
    end;

    Procedure ProcessEven;
    begin
    EvenIndex:=0;
    For i:=1 to ReadIndex do
    Begin
    If Not(Odd(ReadArr[i])) Then begin
    Inc(EvenIndex);
    EvenArr[EvenIndex]:=ReadArr[i];
    End;
    End;
    end;

    Procedure ProcessOdd;
    begin
    OddIndex:=0;
    For i:=1 to ReadIndex do
    Begin
    If Odd(ReadArr[i]) Then begin
    Inc(OddIndex);
    OddArr[OddIndex]:=ReadArr[i];
    End;
    End;
    end;

    Procedure DisplayResults;
    Begin
    Writeln;
    Writeln;

    writeLn('Found ',NegIndex,' Negitive Numbers:');
    For i:=1 to NegIndex do write(NegArr[i],' ');
    WriteLn;
    WriteLn;

    writeLn('Found ',EvenIndex,' Even Number:');
    For i:=1 to EvenIndex do write(EvenArr[i],' ');
    WriteLn;
    WriteLn;

    WriteLn('Found ',OddIndex,' Odd Numbers:');
    For i:=1 to OddIndex do write(OddArr[i],' ');
    WriteLn;
    WriteLn;

    writeln;
    End;

    begin { Main }
    ReadTextFile(GetFileName);
    ProcessNegative;
    ProcessEven;
    ProcessOdd;
    DisplayResults;
    writeln('Press ENTER to Exit');
    ReadLn;
    end.

    [/code]
  • jettamanjettaman Member Posts: 19
    [b][red]This message was edited by the jettaman at 2002-7-14 4:18:53[/red][/b][hr]
    [b][red]This message was edited by the jettaman at 2002-7-14 4:18:31[/red][/b][hr]
    Thanks for getting back to me so quickly on this. I really do appreciate it. I took what you had and make some changes here and there. I'm still having some problems though.
    1st: I am only able to display the last two lines of my text.file.
    2nd: The numbers are not sorting into the right places. Example: The odd and even could show up in the same place.
    3rd. It keeps displaying 000000000 before it displays the number it's read from the text.file. Any idea on how to fix that?
    I think I've got a really good start to it. Any more help on this is greatly appreciated.
    I'm pretty new to thhis Pascal stuff and having people like you on this board really help me out a lot.
    Here's the new code I've come up with so far.

    PROGRAM testarray;

    {$APPTYPE CONSOLE}

    uses
    SysUtils;

    Const
    Min = -25;
    Max = 25;

    Type
    ReadArr = array [Min..Max] of integer;
    VAR
    NegArr, EvenArr, OddArr : ReadArr;

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

    PROCEDURE GetFile(var infile: text);

    VAR
    fileName : string;

    Begin
    writeln;
    writeln ('Enter the path and filename for the file to be processed: ');
    readln (filename);
    assign (infile, filename);
    reset (infile);
    writeln;
    End;

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

    PROCEDURE ReadTextFile(VAR infile:text; VAR NegArr, EvenArr, OddArr: ReadArr);

    VAR
    ReadIndex: integer;

    Begin
    ReadIndex:=0;
    While not Eof(infile) do
    Begin
    Read(infile,NegArr[ReadIndex]);
    Read(infile,EvenArr[ReadIndex]);
    Read(infile,OddArr[ReadIndex]);
    End;
    Close(infile);
    End;

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

    PROCEDURE ProcessNegative (VAR infile: text; VAR ReadIndex, NegIndex: integer);

    VAR
    i: integer;
    begin
    NegIndex:=0;
    For i:= min to NegIndex do
    Begin
    If NegArr[i]<0 Then
    Begin
    NegArr[NegIndex]:= NegArr[i];
    End;
    End;
    End;

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

    PROCEDURE ProcessEven (VAR infile: text; VAR ReadIndex, EvenIndex: integer);

    VAR
    i: integer;

    Begin
    EvenIndex:=0;
    For i:= 2 to EvenIndex do
    Begin
    read (infile,evenarr[i]);
    If EvenArr[i] MOD 2 = 0 Then
    begin
    EvenArr[EvenIndex]:=EvenArr[i];
    End;
    End;
    End;

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

    PROCEDURE ProcessOdd (VAR infile: text; VAR ReadIndex, OddIndex: integer);

    VAR
    i: integer;

    Begin
    OddIndex:=0;
    For i:= min to OddIndex do
    Begin
    If (OddArr[i] > 0) and (OddArr[i] mod 2 <> 0) then
    OddArr[i] := OddArr[i];
    End;
    End;

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

    PROCEDURE DisplayResults (VAR Infile:text; VAR count,negindex: integer;
    VAR NegArr, EvenArr, OddArr: ReadArr);

    VAR
    i: integer;

    Begin
    Writeln;
    Writeln;
    for i := Min to negindex do
    write (NegArr[i]);
    WriteLn;
    WriteLn;

    for i := Min to count do
    write (EvenArr[i]);
    WriteLn;
    WriteLn;

    for i := min to count do
    write (OddArr[i]);
    Writeln;
    Writeln;
    End;

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

    VAR

    ReadIndex, EvenIndex, OddIndex, NegIndex, count: integer;
    infile: text;
    ans: char;

    Begin { Main }

    Repeat
    GetFile (infile);
    ReadTextFile(infile,NegArr, EvenArr, OddArr);
    ProcessNegative (infile, readindex, NegIndex);
    ProcessEven (infile, readindex, EvenIndex);
    ProcessOdd (infile, readindex, OddIndex);
    DisplayResults (infile,count,negindex,negarr,evenarr,oddarr);

    repeat
    writeln ('Would you like to run program again? Y/N');
    readln (ans);
    until (ans = 'Y') or (ans = 'y') or (ans = 'N') or (ans = 'n');
    until (ans = 'N') or (ans = 'n');
    End.



    : : I'm not exactly sure how to read my text.file into those 3 seperate arrays and then display them accordingly. These arrays are a bit confusing. :)
    :
    : Read them into one array (ReadArr), close the file and think
    : what would you like do do with the numbers. Maybe something like this:
    :
    :
    : [code]
    :
    : program read_numbers;
    :
    : var ReadArr, EvenArr,OddArr,NegArr : array[0..100]of integer;
    : ReadIndex, EvenIndex, OddIndex,NegIndex : integer;
    : i:integer;
    :
    :
    : function GetFileName:string;
    : var s:string;
    : begin
    : writeln;
    : writeln('Enter file name');
    : readln(s);
    : GetFileName:=s;
    : end;
    :
    :
    : procedure ReadTextFile(FileName:string);
    : var OneNumber : integer;
    : TextFile : Text;
    : begin
    : assign(TextFile,FileName);
    : Reset(TextFile);
    : ReadIndex:=0;
    :
    : While Not(Eof(TextFile)) do
    : Begin
    : Inc(ReadIndex);
    : Read(TextFile,ReadArr[ReadIndex]);
    : End;
    :
    : Close(TextFile);
    : End;
    :
    : Procedure ProcessNegative;
    : begin
    : NegIndex:=0;
    : For i:=1 to ReadIndex do
    : Begin
    : If ReadArr[i]<0 Then begin
    : Inc(NegIndex);
    : NegArr[NegIndex]:=ReadArr[i];
    : End;
    : End;
    : end;
    :
    : Procedure ProcessEven;
    : begin
    : EvenIndex:=0;
    : For i:=1 to ReadIndex do
    : Begin
    : If Not(Odd(ReadArr[i])) Then begin
    : Inc(EvenIndex);
    : EvenArr[EvenIndex]:=ReadArr[i];
    : End;
    : End;
    : end;
    :
    : Procedure ProcessOdd;
    : begin
    : OddIndex:=0;
    : For i:=1 to ReadIndex do
    : Begin
    : If Odd(ReadArr[i]) Then begin
    : Inc(OddIndex);
    : OddArr[OddIndex]:=ReadArr[i];
    : End;
    : End;
    : end;
    :
    : Procedure DisplayResults;
    : Begin
    : Writeln;
    : Writeln;
    :
    : writeLn('Found ',NegIndex,' Negitive Numbers:');
    : For i:=1 to NegIndex do write(NegArr[i],' ');
    : WriteLn;
    : WriteLn;
    :
    : writeLn('Found ',EvenIndex,' Even Number:');
    : For i:=1 to EvenIndex do write(EvenArr[i],' ');
    : WriteLn;
    : WriteLn;
    :
    : WriteLn('Found ',OddIndex,' Odd Numbers:');
    : For i:=1 to OddIndex do write(OddArr[i],' ');
    : WriteLn;
    : WriteLn;
    :
    : writeln;
    : End;
    :
    : begin { Main }
    : ReadTextFile(GetFileName);
    : ProcessNegative;
    : ProcessEven;
    : ProcessOdd;
    : DisplayResults;
    : writeln('Press ENTER to Exit');
    : ReadLn;
    : end.
    :
    : [/code]
    :





  • ibyiby Member Posts: 323
    Why don't you post your text file?
    My test file was:
    [code]
    3 16 25 -33 11 -4
    0 15 0 0 2 -3 26
    17 25 -13
    [/code]

    Note that some numbers will be in two groups.
    For example, -4 is NEGATIVE and EVEN.

    If you want to sort the arrays that's simple.
    Bublesort will do. Here is sample code for Negative:
    [code]
    for m:=1 to NegIndex-1 do
    for n:=m to NegIndex do
    if NegArr[m]>NegArr[n] then begin { swap elements }
    TempInteger:=NegArr[m];
    NegArr[m]:=NegArr[n];
    NegArr[n]:=TempInteger;
    end;
    [/code]


    Iby
  • jettamanjettaman Member Posts: 19
    [b][red]This message was edited by the jettaman at 2002-7-14 6:11:10[/red][/b][hr]
    Here's my text.file..Teacher wants us to have a seperate line for each number.
    1
    2
    3
    -14
    6
    9
    11
    12
    4
    13
    -22
    14
    15
    -16
    8
    17
    19
    -17
    -23
    -19
    20
    21
    -8
    22
    -6
    24
    10
    -1
    -2
    5
    -3
    -4
    16
    -5
    25
    -7
    23
    -9
    -10
    -11
    18
    -12
    -13
    7
    -15
    -18
    -20
    -21
    -24
    -25
    Thanks again.

    : Why don't you post your text file?
    : My test file was:
    : [code]
    : 3 16 25 -33 11 -4
    : 0 15 0 0 2 -3 26
    : 17 25 -13
    : [/code]
    :
    : Note that some numbers will be in two groups.
    : For example, -4 is NEGATIVE and EVEN.
    :
    : If you want to sort the arrays that's simple.
    : Bublesort will do. Here is sample code for Negative:
    : [code]
    : for m:=1 to NegIndex-1 do
    : for n:=m to NegIndex do
    : if NegArr[m]>NegArr[n] then begin { swap elements }
    : TempInteger:=NegArr[m];
    : NegArr[m]:=NegArr[n];
    : NegArr[n]:=TempInteger;
    : end;
    : [/code]
    :
    :
    : Iby
    :



  • ibyiby Member Posts: 323
    : Here's my text.file..Teacher wants us to have a seperate line for each number.
    : 1
    : 2
    : 3
    : -14
    : 6...


    I run the program with your file and it worked just fine.
    I have no idea how you got into problems you mentioned earlier
    (printing 0000000, displaying only last two lines etc.)

    Iby
  • jettamanjettaman Member Posts: 19
    So you used the new program to run the text.file and everything worked out correctly?

    : : Here's my text.file..Teacher wants us to have a seperate line for each number.
    : : 1
    : : 2
    : : 3
    : : -14
    : : 6...
    :
    :
    : I run the program with your file and it worked just fine.
    : I have no idea how you got into problems you mentioned earlier
    : (printing 0000000, displaying only last two lines etc.)
    :
    : Iby
    :

  • ibyiby Member Posts: 323
    : So you used the new program to run the text.file and everything worked out correctly?


    Yeah... Didn't you?


    Iby

  • jettamanjettaman Member Posts: 19
    I used the code that you posted and I also used the version that I posted last time. For somereason your code doesn't run correctly when I run it. I keep getting a ElnOutError message when I run it.


    : : So you used the new program to run the text.file and everything worked out correctly?
    :
    :
    : Yeah... Didn't you?
    :
    :
    : Iby
    :
    :

«1
Sign In or Register to comment.