Howdy, Stranger!

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

Categories

still having some problems with arrays

jettamanjettaman Member Posts: 19
Hi there. I've been getting some great help on this matter but I'm still a bit slow when it comes to understanding arrays.
I'm trying to read 3's from a text file into an array/arrays. I need to have my program read the file, then display all the numbers that are NEGITIVE, POSITIVE(not negitive), AND ODD(not negitive).

The help I got was really good but It only displays 1 number for each catagory and it doesn't place negitives in the NEGITIVE: catagory and it only displays 1 number in each catagory. I'm looking for some help on how to display all the numbers from my text.file in their proper catagories.
This is what I've got so far.
program read_numbers;

{$APPTYPE CONSOLE}

uses
SysUtils;

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;
readln(textfile);
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.

Also, could someone please explain what/how Inc(textfile); is used.
Thanks
Ross
«1

Comments

  • ManningManning Member Posts: 1,621
    Here is a working program. I used the same variables names, but I wrote it from scratch so it is a little bit different in places. I didnt take a good look at your old code, but the only problem I saw was that you were adding negative values to the odd and even array even though you said you dont want them...other than that the logic is mostly the same.

    [code]
    Program ThreeArrays;

    {$APPTYPE CONSOLE}

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

    function GetFileName: String;
    var
    S: String;
    begin
    WriteLn;
    Write('Enter File Name: ');
    ReadLn(S);
    Result := S;
    end;


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

    Assign(TextFile, FileName);
    {$I-}Reset(TextFile);{$I+}

    if (IOResult = 0) then
    begin
    While Not(Eof(TextFile)) do
    begin
    ReadLn(TextFile, OneNumber);
    if (OneNumber < 0) Then
    begin
    NegArr[NegIndex] := OneNumber;
    Inc(NegIndex);
    end else
    begin
    if Odd(OneNumber) Then
    begin
    OddArr[OddIndex] := OneNumber;
    Inc(OddIndex);
    end else
    begin
    EvenArr[EvenIndex] := OneNumber;
    Inc(EvenIndex);
    end;
    end;
    end;
    Close(TextFile);
    end else
    begin
    WriteLn;
    WriteLn('Error Opening File');
    WriteLn;
    WriteLn('Press ENTER to Exit');
    ReadLn;
    Halt(1);
    end;
    End;

    Procedure DisplayResults;
    begin
    WriteLn;
    WriteLn;
    WriteLn('Negitive Numbers: ', NegIndex);
    WriteLn('Even Number : ', EvenIndex);
    Writeln('Odd Numbers : ', OddIndex);
    Writeln;
    end;

    begin
    ReadTextFile(GetFileName);
    DisplayResults;
    WriteLn('Press ENTER to Exit');
    ReadLn;
    end.
    [/code]

    : Also, could someone please explain what/how Inc(textfile); is used.

    Im not sure why you asked about that since you dont use Inc(TextFile) anywhere in your code (nor should you since if you look up Inc in the help file you'll know its meant to increment an ordinal value)
  • jettamanjettaman Member Posts: 19
    Thanks for getting back to me on this so quickly.
    I was inquiring about the Inc(textfile); because I wasn't sure what Inc did and wanted to understand it.
    Also, when I run the program it automatically goes to the "Error opening File" and then the programs stops.
    Any ideas?
    Thanks again.



    : Here is a working program. I used the same variables names, but I wrote it from scratch so it is a little bit different in places. I didnt take a good look at your old code, but the only problem I saw was that you were adding negative values to the odd and even array even though you said you dont want them...other than that the logic is mostly the same.
    :
    : [code]
    : Program ThreeArrays;
    :
    : {$APPTYPE CONSOLE}
    :
    : var
    : EvenArr, OddArr, NegArr: Array[0..100] of Integer;
    : EvenIndex, OddIndex, NegIndex: Integer;
    :
    : function GetFileName: String;
    : var
    : S: String;
    : begin
    : WriteLn;
    : Write('Enter File Name: ');
    : ReadLn(S);
    : Result := S;
    : end;
    :
    :
    : procedure ReadTextFile(FileName: String);
    : var
    : OneNumber: Integer;
    : TextFile: Text;
    : begin
    : EvenIndex := 0;
    : OddIndex := 0;
    : NegIndex := 0;
    :
    : Assign(TextFile, FileName);
    : {$I-}Reset(TextFile);{$I+}
    :
    : if (IOResult = 0) then
    : begin
    : While Not(Eof(TextFile)) do
    : begin
    : ReadLn(TextFile, OneNumber);
    : if (OneNumber < 0) Then
    : begin
    : NegArr[NegIndex] := OneNumber;
    : Inc(NegIndex);
    : end else
    : begin
    : if Odd(OneNumber) Then
    : begin
    : OddArr[OddIndex] := OneNumber;
    : Inc(OddIndex);
    : end else
    : begin
    : EvenArr[EvenIndex] := OneNumber;
    : Inc(EvenIndex);
    : end;
    : end;
    : end;
    : Close(TextFile);
    : end else
    : begin
    : WriteLn;
    : WriteLn('Error Opening File');
    : WriteLn;
    : WriteLn('Press ENTER to Exit');
    : ReadLn;
    : Halt(1);
    : end;
    : End;
    :
    : Procedure DisplayResults;
    : begin
    : WriteLn;
    : WriteLn;
    : WriteLn('Negitive Numbers: ', NegIndex);
    : WriteLn('Even Number : ', EvenIndex);
    : Writeln('Odd Numbers : ', OddIndex);
    : Writeln;
    : end;
    :
    : begin
    : ReadTextFile(GetFileName);
    : DisplayResults;
    : WriteLn('Press ENTER to Exit');
    : ReadLn;
    : end.
    : [/code]
    :
    : : Also, could someone please explain what/how Inc(textfile); is used.
    :
    : Im not sure why you asked about that since you dont use Inc(TextFile) anywhere in your code (nor should you since if you look up Inc in the help file you'll know its meant to increment an ordinal value)
    :

  • ManningManning Member Posts: 1,621
    : Thanks for getting back to me on this so quickly.
    : I was inquiring about the Inc(textfile); because I wasn't sure what Inc did and wanted to understand it.
    : Also, when I run the program it automatically goes to the "Error opening File" and then the programs stops.
    : Any ideas?

    Sorry, no ideas. I tested it before sending and didnt have that problem. You might want to add some WriteLn() and ReadLn; calls in there so you can print out information and pause before continuing. One place I would put one is at the top of ReadTextFile so you can write the FileName variable to screen and see if maybe something isnt being passed correctly.
  • jettamanjettaman Member Posts: 19
    Thanks. That's actually what I ended up doing.
    I does count the number of negitives, even, and odds and displays the count. What I'm trying to figure out is how to display the actual numbers instead of the count.
    Thansk again for your help.
  • ManningManning Member Posts: 1,621
    : Thanks. That's actually what I ended up doing.
    : I does count the number of negitives, even, and odds and displays the count. What I'm trying to figure out is how to display the actual numbers instead of the count.
    : Thansk again for your help.
    :

    Use a for loop. For example:

    [code]
    for I := 0 to NegIndex - 1 do
    WriteLn(NegArr[I]);
    [/code]
  • ibyiby Member Posts: 323
    [b][red]This message was edited by the iby at 2002-7-15 19:28:28[/red][/b][hr]
    : Thanks. That's actually what I ended up doing.
    : I does count the number of negitives, even, and odds and displays the count. What I'm trying to figure out is how to display the actual numbers instead of the count.
    : Thansk again for your help.
    :


    Hi JettaMan,

    I'm not sure what are you doing wrong but you sure
    manage to do it anyway. There have been few working
    versions of the program and only you seam to have
    problem. Last version I was posting was not only
    displaying [b]number[/b] of Negative, Even and Odd
    numbers but it was also [b]listing[/b] them.
    Before I can help you more I would need some answers
    if you don't mind. So here are the questions:

    1. what the heck is SysUtils and what do you
    need it for? You should use only what you need
    and not blindly include what ever you have handy...

    2. what compiler you use and what version?
    Also, are you sure it's a version of
    [b]Pascal[/b] compiler and not maybe modula2 or
    something else?

    3. what are [b]exact[/b] steps you do when trying to
    run posted code? I mean how [b]exactly[/b] do you
    transfer posted code into *.PAS file? Do you retype it
    or use copy and paste?

    4. what is your email address? i want to send you email
    with all files (sources, compiled files and
    sample TXT files with some numbers).

    Regards,

    Iby



  • jettamanjettaman Member Posts: 19
    Hi Iby. My email is ross12121@attbi.com
    email me and I'll send you my most recient code and explaine what's happening.
    Thanks


    : [b][red]This message was edited by the iby at 2002-7-15 19:28:28[/red][/b][hr]
    : : Thanks. That's actually what I ended up doing.
    : : I does count the number of negitives, even, and odds and displays the count. What I'm trying to figure out is how to display the actual numbers instead of the count.
    : : Thansk again for your help.
    : :
    :
    :
    : Hi JettaMan,
    :
    : I'm not sure what are you doing wrong but you sure
    : manage to do it anyway. There have been few working
    : versions of the program and only you seam to have
    : problem. Last version I was posting was not only
    : displaying [b]number[/b] of Negative, Even and Odd
    : numbers but it was also [b]listing[/b] them.
    : Before I can help you more I would need some answers
    : if you don't mind. So here are the questions:
    :
    : 1. what the heck is SysUtils and what do you
    : need it for? You should use only what you need
    : and not blindly include what ever you have handy...
    :
    : 2. what compiler you use and what version?
    : Also, are you sure it's a version of
    : [b]Pascal[/b] compiler and not maybe modula2 or
    : something else?
    :
    : 3. what are [b]exact[/b] steps you do when trying to
    : run posted code? I mean how [b]exactly[/b] do you
    : transfer posted code into *.PAS file? Do you retype it
    : or use copy and paste?
    :
    : 4. what is your email address? i want to send you email
    : with all files (sources, compiled files and
    : sample TXT files with some numbers).
    :
    : Regards,
    :
    : Iby
    :
    :
    :
    :

  • jettamanjettaman Member Posts: 19
    That fixed it. Thanks everybody for the help.
    It's people like yourselves that make Pascal a little bit less irritating. :)
    Thanks again


    : : Thanks. That's actually what I ended up doing.
    : : I does count the number of negitives, even, and odds and displays the count. What I'm trying to figure out is how to display the actual numbers instead of the count.
    : : Thansk again for your help.
    : :
    :
    : Use a for loop. For example:
    :
    : [code]
    : for I := 0 to NegIndex - 1 do
    : WriteLn(NegArr[I]);
    : [/code]
    :

  • ManningManning Member Posts: 1,621
    : 1. what the heck is SysUtils and what do you
    : need it for? You should use only what you need
    : and not blindly include what ever you have handy...

    SysUtils is a standard unit that comes with Delphi (and now other compilers that try to be Delphi compatable). When you start a new console program with Delphi it automatically adds SysUtils to the uses line.

    : 2. what compiler you use and what version?
    : Also, are you sure it's a version of
    : [b]Pascal[/b] compiler and not maybe modula2 or
    : something else?

    The {$APPTYPE CONSOLE} compiler directive he used in one of his programs tells the Delphi compiler that the following code is a console and not GUI application, which is why Im assuming he is using Delphi.
  • ibyiby Member Posts: 323
    : : 1. what the heck is SysUtils and what do you
    : : need it for? You should use only what you need
    : : and not blindly include what ever you have handy...
    :
    : SysUtils is a standard unit that comes with Delphi (and now other compilers that try to be Delphi compatable). When you start a new console program with Delphi it automatically adds SysUtils to the uses line.
    :
    : : 2. what compiler you use and what version?
    : : Also, are you sure it's a version of
    : : [b]Pascal[/b] compiler and not maybe modula2 or
    : : something else?
    :
    : The {$APPTYPE CONSOLE} compiler directive he used in one of his programs tells the Delphi compiler that the following code is a console and not GUI application, which is why Im assuming he is using Delphi.
    :


    I see... That explains it.
    Thanks Manning.




«1
Sign In or Register to comment.