Recent

Author Topic: Pascal program (plz help me out)  (Read 2977 times)

dandum

  • Newbie
  • Posts: 1
Pascal program (plz help me out)
« on: November 23, 2018, 01:10:16 am »
Consider the linear array T [1..n] whose elements are numbers equal to 0 or 1. Need a program that will determine the k-number of sequences of the array, formed by units only.
Example:
Entry: n = 10
array: 0 (1 1) 0 (1 1 1 1) 0 (1) {i've marked the digits in brackets so that you can have a better view on what's the actual idea}
                 Exit: k = 3

P.S.: Sorry for my bad English :( Hope i get this solved soon, i need this ASAP for an exam T_T

also i have tried to figure out the program, here's my last update:

const n = 15;
var i,k:integer; x: array [1..n] of integer;

begin
Randomize;
k:=0;

   for i:=1 to n do
   begin
   x:=Random(2);
   write(x,'  ');
      if x = x[i+1] then
         k:=k+1;      
   end;
   writeln('   k=',k);
end.

{The program calculates just how many digits of 0 are there(if u change '=' to '<>' it calculates how many units are there)}

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Pascal program (plz help me out)
« Reply #1 on: November 23, 2018, 02:09:26 am »
Your code is almost done. Only - you cannot test
Code: Pascal  [Select][+][-]
  1. x[i+1]
inside the loop because that element of array is not defined yet and you will test 16-th element in the last step which may cause SIGSEGV.

I would rather store previous value (new variable prev):
Code: Pascal  [Select][+][-]
  1. const n = 15;
  2. var i,k,prev:integer; x: array [1..n] of integer;
  3. begin
  4.   Randomize;
  5.   k:=0;
  6.   prev:=0;
  7.   for i:=1 to n do
  8.     begin
  9.       x[i]:=Random(2);
  10.       write(x[i],' ');
  11.       if (x[i] = 1) and (prev <> x[i]) then
  12.          k:=k+1;
  13.       prev:=x[i];
  14.     end;
  15.   writeln('k=', k);
  16. end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Pascal program (plz help me out)
« Reply #2 on: November 23, 2018, 03:07:20 pm »
To me it's unclear what you want to achieve.
Do you want the count of the longest run of one's?
Do you want the count of runs of one's?
You code does neither.
Given 0 (1 1) 0 (1 1 1 1) 0 (11) it will exit with k = 4 and for
0 (1 1) 0 (1 1 1 1) 0 (111), k will be 5.
Is that what you want to achieve?

Bart

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Pascal program (plz help me out)
« Reply #3 on: November 23, 2018, 04:31:29 pm »
The coder is looking for the number of GROUPS of 1's

In their example it would be 3, which means the code needs to examine the previous element in
the array and if not a (1) then increment(K).
 
 Basically this..
 if X[ N ]  ='1' Then
   If (N=1) or (X[ N-1 ] <>'1') Then K := K+1;
     

 That is how I get it from the example results of the lecture.
The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Pascal program (plz help me out)
« Reply #4 on: November 23, 2018, 09:51:45 pm »
This would be my solution:
Code: Pascal  [Select][+][-]
  1. program GetK;
  2.  
  3. {$Mode objfpc}{$H+} {$IfDef windows}{$AppType console}{$EndIf}
  4.  
  5. const
  6.   n = 15;
  7. type
  8.   TBinary = (zero, one);
  9. var
  10.   x: array[1..n] of Integer;
  11.  
  12.   function GetKvalue(aBinary: TBinary):Integer;
  13.   var
  14.     k: Integer;
  15.   begin
  16.     Result := 0;
  17.     for k := Low(x) to Pred(High(x)) do begin
  18.       if x[k] <> x[Succ(k)] then begin
  19.         case Ord(aBinary) of
  20.           0: if x[k] = 0 then Inc(Result);
  21.           1: if x[k] = 1 then Inc(Result);
  22.         end;
  23.       end;
  24.     end;
  25.     case Ord(aBinary) of
  26.       0: if x[n] = 0 then Inc(Result);
  27.       1: if x[n] = 1 then Inc(Result);
  28.     end;
  29.   end;
  30.  
  31.   procedure ShowArray;
  32.   var
  33.     j: Integer;
  34.   begin
  35.     for j in x do
  36.       Write(j, ' ');
  37.     WriteLn;
  38.   end;
  39.  
  40.   procedure FillArray;
  41.   var
  42.     i: Integer;
  43.   begin
  44.     for i := Low(x) to High(x) do
  45.       x[i] := Random(2);
  46.   end;
  47.  
  48. begin
  49.   Randomize;
  50.   FillArray;
  51.   ShowArray;
  52.   WriteLn;
  53.   WriteLn(' k value of zero groups = ', GetKvalue(zero));
  54.   WriteLn(' k value of one groups = ',  GetKvalue(one));
  55.   WriteLn;
  56.   WriteLn('Press Enter to finish');
  57.   ReadLn;
  58. end.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Pascal program (plz help me out)
« Reply #5 on: November 23, 2018, 10:42:08 pm »
The coder is looking for the number of GROUPS of 1's

If so than the original example should result in k = 2, since AFAIK a single 1 does not constitue a group?
Also incrementing K every time prev and current = 1 is wrong then.
(111) would give 2, when it should be 1.

It would require a state machine, only incrementing k when state changes and state equals "we're in a group of ones now".

Bart
« Last Edit: November 23, 2018, 10:43:48 pm by Bart »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Pascal program (plz help me out)
« Reply #6 on: November 23, 2018, 10:52:37 pm »
Ok, maybe I and wording it incorrect,,,


 The "start" of  (1), be it a single or grouped.

 Its like the starting edge count of a variable width square wave signal.
 
 how many starting edges do we have in a measured time domain.



 

The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Pascal program (plz help me out)
« Reply #7 on: November 24, 2018, 11:20:59 am »
OK, I understand now.
Makes life easier, jus increment K each time you encounter a 1 and the previous was not a 1...

Bart

 

TinyPortal © 2005-2018