Recent

Author Topic: Is there any problem?  (Read 4446 times)

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Is there any problem?
« on: October 02, 2016, 10:38:53 am »
Here is my code and i got some problems.
Code: [Select]
var
   no:array[1..30] of string;
begin
for x:=1 to 30 do
  no[x]:'';
  {Then i enter the value of arrayX.One is null and other is not}
  y:=0;
  while y<30 do
  begin
  if no<>''
  then
     begin
     y:=y+1;
     no[y]:=no[y];
     end;
   end;
end.

i use trace into and found my program is stuck in the selection of ' if no<>'' ' ,  could someone help me please?
7vinbaby

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: Is there any problem?
« Reply #1 on: October 02, 2016, 10:51:35 am »
 
Code: Pascal  [Select][+][-]
  1. if no[y]<>'' //not  if no<>''

But that will crash too since there is no no[0];
You have to revise your code accordingly.... like:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. var
  3.    no:array[1..30] of string;
  4.    X,y:integer;
  5. begin
  6. for x:=1 to 30 do
  7.   no[x]:='';
  8.   {Then i enter the value of arrayX.One is null and other is not}
  9.   y:=1;
  10.   while y<31 do
  11.   begin
  12.   if no[y]<>''
  13.   then
  14.      begin
  15.      no[y]:=no[y];
  16.      y:=y+1;
  17.      end;
  18.    end;
  19. end.
« Last Edit: October 02, 2016, 10:59:04 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Re: Is there any problem?
« Reply #2 on: October 02, 2016, 10:59:09 am »
 
Code: Pascal  [Select][+][-]
  1. if no[y]<>'' //not  if no<>''

But that will crash too since there is no no[0];
You have to revise your code accordingly.... like:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. var
  3.    no:array[1..30] of string;
  4.    X,y:integer;
  5. begin
  6. for x:=1 to 30 do
  7.   no[x]:='';
  8.   {Then i enter the value of arrayX.One is null and other is not}
  9.   y:=1;
  10.   while y<30 do
  11.   begin
  12.   if no[y]<>''
  13.   then
  14.      begin
  15.      no[y]:=no[y];
  16.      y:=y+1;
  17.      end;
  18.    end;
  19. end.

Yes,thank you .I miss that point.
7vinbaby

wp

  • Hero Member
  • *****
  • Posts: 11908
Re: Is there any problem?
« Reply #3 on: October 02, 2016, 10:59:52 am »
Here is my code and i got some problems.
Which problems? We cannot read your mind.

Code: [Select]
for x:=1 to 30 do
  no[x]:'';
  {Then i enter the value of arrayX.One is null and other is not}
  y:=0;
...
The indentation tells me that the for loop should contain not only the no[ x] := '' line. If the for loop should contain more lines then use "begin" after the for's "do". If not correct your indentation. Proper indentation is one of the most important time savers for a programmer!

Code: [Select]
  no[x]:'';
This is not a valid statement. You probably mean: no[ x] := ''

Code: [Select]
  if no<>''
  then
Another invalid statement. "no" is an array, and you can only access it by element. You probably mean "no[ x]", or "no[ y]"

Code: [Select]
     no[y]:=no[y];
This is pure nonsense: You assign a variable to itself and do not change anything.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Is there any problem?
« Reply #4 on: October 02, 2016, 11:00:08 am »
Perfectly valid code
Code: [Select]
y:=y+1;
no[y]:=no[y];
Simply doesn't make any sense

Perhaps You must be thinking of something similar like:
Code: [Select]
y:=y+1;
no[y]:=no[y+1];

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: Is there any problem?
« Reply #5 on: October 02, 2016, 11:05:47 am »
Yes,thank you .I miss that point.
But how can you miss that?
The compiler explains to you what mistake you made....
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
Re: Is there any problem?
« Reply #6 on: October 02, 2016, 11:21:54 am »
Quote
Code: [Select]
y:=y+1;
no[y]:=no[y];

actually i want to update the record in the array so if there is null space,i do not put it into the array.
However i can't have more idea about this
« Last Edit: October 02, 2016, 11:24:59 am by 7vinbaby »
7vinbaby

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Is there any problem?
« Reply #7 on: October 02, 2016, 12:04:09 pm »
actually i want to update the record in the array so if there is null space,i do not put it into the array.
However i can't have more idea about this
Ah ok. You would like to clean up your array from 'empty' answers ? Did i understood that correctly ?

In that case, a couple of questions arises
- Why put empty answers into the array to begin with ?
- Why use a static array ?

But, in order to satisfy your curiosity which follows your lead:
Code: [Select]
procedure CleanArray;
var
  i, j : Integer;
begin
  i := low(no);
  for j := low(no) to High(no) do
  begin
    if (no[j] <> '') then
    begin
      no[i] := no[j];
      If i <> j then no[j] := '';
      inc(i);
    end;
  end;
  WriteLn('Number of non empty items in array = ', Pred(i));
end;

Note though, that the above code is _not_ the way to go as it is too error-prone. I'm showing it in order to let you see why.

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Is there any problem?
« Reply #8 on: October 02, 2016, 12:18:12 pm »
How about this one:

Code: Pascal  [Select][+][-]
  1. program UpdateArrayDataIfNotEmpty;
  2.  
  3. var
  4.   MyArray: array[1..30] of string;
  5.   i, j   : integer;
  6.   Found  : boolean;
  7.  
  8. begin
  9.  
  10.   // Initial the array with empty string
  11.   for i := Low(MyArray) to High(MyArray) do
  12.     MyArray[i] := '';
  13.  
  14.   // Store data into MyData but skip non empty array
  15.   i := 1;
  16.   while i<30 do begin // Store 30 items into the array
  17.     j := Low(MyArray);
  18.     Found := False;
  19.     repeat // to find an empty space on the position j
  20.       if MyData[j] := '' then
  21.         Found := True
  22.       else
  23.         inc(j);    
  24.     until Found or (j>=High(MyData));  
  25.     if Found then MyData[j] := 'Data#'+IntToStr(j);
  26.     inc(i);
  27.   end;
  28.  
  29.   // Show it
  30.   WriteLn('Stored data in the array:');
  31.   for i := Low(MyArray) to High(MyArray) do
  32.     WriteLn(MyArray[i]);
  33.  
  34. end.
« Last Edit: October 02, 2016, 12:30:11 pm by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: Is there any problem?
« Reply #9 on: October 02, 2016, 03:18:52 pm »
@Handoko your code is wrong:
Code: Pascal  [Select][+][-]
  1.   while i<30 do begin // Store 30 items into the array
  2. // Nope it stores 29 elements. I corrected that already in my example
  3. // he starts at one...
  4.  
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Is there any problem?
« Reply #10 on: October 02, 2016, 06:01:33 pm »
Oh, my mistake.  :-[

I usually use <= or >= but why I forgot to use it. It should be:

Code: Pascal  [Select][+][-]
  1.   while i<=30 do begin

Thanks Thaddy for that correction.

 

TinyPortal © 2005-2018