Recent

Author Topic: importing real numbers from text file into a vector  (Read 2506 times)

snar3beats

  • Newbie
  • Posts: 2
importing real numbers from text file into a vector
« on: June 18, 2018, 03:44:02 pm »
I am trying to read real numbers from a text file and create a vector

program bac18III;
var
v:array[1..100]of real;
i,x:integer;
y:real;
f:text;
function count(v:array of real;n:integer):integer;
var i,x:integer;
begin
x:=0;
for i:=1 to n do begin
if(v>=(v[1]+v[n])/2) then
x:=x+1; end;
count:=x;
end;
begin
assign(f,'numere.txt');reset(f);
x:=0;
v[1]:=0;
i:=2;
while not(eof(f)) do begin
read(f,y);
v:=y;
i:=i+1;
x:=x+1;
end;
for i:=1 to x+1 do
write(v);
write(count(v,x));
readln;
end.

The numbers saved in my vector looks like this : a.bcdef... not ab.cdef
« Last Edit: June 18, 2018, 03:46:04 pm by snar3beats »

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: importing real numbers from text file into a vector
« Reply #1 on: June 18, 2018, 04:00:50 pm »
Hello snar3beats,
Welcome to the forum.

For your information, you should use code tag when posting code in this forum. Because without it, the code will show incorrectly. Here I fixed it for you:

Code: Pascal  [Select][+][-]
  1. program bac18III;
  2.  
  3. var
  4.   v: array[1..100] of Real;
  5.   i, x: Integer;
  6.   y: Real;
  7.   f: Text;
  8.  
  9. function Count(v: array of Real; n: Integer): Integer;
  10. var
  11.   i, x: Integer;
  12. begin
  13.   x := 0;
  14.   for i := 1 to n do begin
  15.     if (v[i] >= (v[1] + v[n]) / 2) then
  16.       x := x + 1;
  17.   end;
  18.   Count := x;
  19. end;
  20.  
  21. begin
  22.   Assign(f, 'numere.txt');
  23.   Reset(f);
  24.   x    := 0;
  25.   v[1] := 0;
  26.   i    := 2;
  27.   while not (EOF(f)) do begin
  28.     Read(f, y);
  29.     v[i] := y;
  30.     i    := i + 1;
  31.     x    := x + 1;
  32.   end;
  33.   for i := 1 to x + 1 do
  34.     Write(v[i]);
  35.   Write(Count(v, x));
  36.   ReadLn;
  37. end.

I'm not good in vector things, so I left question to someone who is good in that.
« Last Edit: June 18, 2018, 04:03:17 pm by Handoko »

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: importing real numbers from text file into a vector
« Reply #2 on: June 18, 2018, 04:12:02 pm »
The numbers saved in my vector looks like this : a.bcdef... not ab.cdef
If you want 4 decimal places in the output, use a format specifier, e.g. :20:4 with write.
Code: Pascal  [Select][+][-]
  1. //...
  2. for i:=1 to x+1 do
  3.   writeln(v[i]:20:4);
  4. writeln(count(v,x));
  5. readln;
  6. end.
  7.  
And I suggest using writeln to  see very vector element an a single line.

RayoGlauco

  • Full Member
  • ***
  • Posts: 179
  • Beers: 1567
Re: importing real numbers from text file into a vector
« Reply #3 on: June 18, 2018, 04:23:12 pm »
snar3beats, I think your code works.
I only will make some cosmetic changes:

Code: Pascal  [Select][+][-]
  1. program bac18III;
  2.  
  3. var
  4.   v: array[1..100] of real;
  5.   i, x: integer;
  6.   y: real;
  7.   f: Text;
  8.  
  9.   function Count(v: array of real; n: integer): integer;
  10.   var
  11.     i: integer;
  12.   begin
  13.     Result := 0;
  14.     for i := 1 to n do
  15.       if (v[i] >= (v[1] + v[n]) / 2) then
  16.         Inc(Result);
  17.   end;
  18.  
  19. begin
  20.   Assign(f, 'numere.txt');
  21.   Reset(f);
  22.   x := 0;
  23.   v[1] := 0;
  24.   i := 2;
  25.   while not (EOF(f)) do
  26.   begin
  27.     Read(f, y);
  28.     v[i] := y;
  29.     Inc(i);
  30.     Inc(x);
  31.   end;
  32.   for i := 1 to x + 1 do
  33.     writeln(v[i]: 20: 4);
  34.   writeln(Count(v, x));
  35.   ReadLn;
  36. end.      

But if you have more than 100 numbers in your file, you will get out of your array.
« Last Edit: June 18, 2018, 04:29:34 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: importing real numbers from text file into a vector
« Reply #4 on: June 18, 2018, 04:54:10 pm »
To avoid errors arising from reading or writing beyond the bounds of your vector array you could write the program along these lines:
Code: Pascal  [Select][+][-]
  1. program ReadRealsFromText;
  2.  
  3. {$Mode objfpc}{$H+}
  4.  
  5. type
  6.   TRange = 1..100;
  7.   TSingleArray = array[TRange] of Single;
  8.  
  9. var
  10.   v: TSingleArray;
  11.   s: Single;
  12.   f: TextFile;
  13.   i: Integer;
  14.  
  15. function Count(anArray: TSingleArray; n: TRange): Integer;
  16. var
  17.   i: TRange;
  18. begin
  19.   Result := 0;
  20.   for i := Low(TRange) to n do
  21.     if anArray[i] >= (anArray[1] + anArray[n]) / 2 then
  22.       Inc(Result);
  23. end;
  24.  
  25. begin
  26.   Randomize;
  27.   Assign(f,'numere.txt');
  28.   try
  29.     Rewrite(f);
  30.     for i in TRange do
  31.       begin
  32.         s := Random*100;
  33.         WriteLn(f, s);
  34.       end;
  35.   finally
  36.     CloseFile(f);
  37.   end;
  38.  
  39.   v := default(TSingleArray);
  40.   Assign(f,'numere.txt');
  41.   try
  42.     Reset(f);
  43.     i := Low(TRange);
  44.     while (not eof(f)) and (i <= High(TRange)) do
  45.       begin
  46.         ReadLn(f, v[i]);
  47.         Inc(i);
  48.       end;
  49.   finally
  50.     CloseFile(f);
  51.   end;
  52.  
  53.   i := Low(TRange);
  54.   for s in v do
  55.     begin
  56.       WriteLn(s:8:2,', Count=',Count(v, i));
  57.       Inc(i);
  58.     end;
  59.   WriteLn('Press [Enter] to finish');
  60.   ReadLn;
  61. end.

snar3beats

  • Newbie
  • Posts: 2
Re: importing real numbers from text file into a vector
« Reply #5 on: June 19, 2018, 07:29:10 am »
Thank you very much, I'm new on forum, sorry for my mistakes :)

 

TinyPortal © 2005-2018