0

I have to script a pascal code that rations into calculation the frequency of a character's appearance in the code and displays it through the output mode

Input P2 changes:

Second Attempt at the coding phase I tried revisioning the code.I added the output variable writeln('input array of characters'); & writeln('Number of Occurrences',k);, which should help me output how many times did the S character appear overall in the code, plus utilised the for & if commands to have the final values showcased based on the conditions, if the frequency is 1 then count in S, still getting errors, take a look at the Input P2 & Output P2

Input P1
function Count(t, s: String): Integer;
var
  Offset, P: Integer;
begin
  Result := 0;
  Offset := 1;
  P := PosEx(t, s, Offset);
  
  while P > 0 do
  begin
    Inc(Result);
    P := PosEx(t, s, P + 1);
  end;
end;

Output P2
Target OS: Linux for x86-64
Compiling main.pas
main.pas(5,3) Error: Identifier not found "Result"
main.pas(7,8) Error: Identifier not found "PosEx"
main.pas(8,3) Error: Identifier not found "unsigned"
main.pas(8,12) Fatal: Syntax error, ";" expected but "identifier N" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode

-------------------------------------------------------------------

Input P2
program p1

var S:string

i:integer
begin
writeln('input array of characters');
k:=O;
for i:=1 to length (S) do
if (S[i])='m') and (S[i+1]='a') then k:=k+1;
writeln('Number of  Occurrences',k);
Readln;
end.

Output P2
Compiling main.pas
main.pas(2,1) Fatal: Syntax error, ";" expected but "VAR" found
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode
  • 1
    What have you tried, and how has what you've tried failed? Ideally, you should provide a [MCVE] of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. [SO] is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [Ask] a Good Question (and [How do I ask and answer homework questions](https://meta.stackoverflow.com/q/334822), if relevant). – Jeff Zeitlin Dec 10 '21 at 19:21
  • I'm just trying to learn the coding language, so this is something I can't get right! – ReallyQuerdey Dec 10 '21 at 19:25
  • Does this answer your question? [How to count number of occurrences of a certain char in string?](https://stackoverflow.com/questions/15294501/how-to-count-number-of-occurrences-of-a-certain-char-in-string) – eglease Dec 10 '21 at 19:26
  • Not so much, the code is sorta compiled in a way that it has no repetitive bites, just one-liners that operate on their own account – ReallyQuerdey Dec 10 '21 at 19:34
  • Do you know how to calculate the "frequency of a character's appearance in an array"? How would you write the formula on paper? Do you know how to traverse an array, to check what characters it holds? Do you know how to declare a variable and increment it? How would you ever learn, if we would spoil your homework by writing your homework for you? You need to be specific in your question and show whatever code you already have done. We are happy to guide you forward. – Tom Brunberg Dec 10 '21 at 20:06
  • 1
    Great!, that looks like a good start. But when you ask questions here or where ever, don't say "It keeps crashing", "It doesn't work" etc. That's a waist of characters. Instead say exactly how it crashes, or what it does that it shouldn't. And provide a test case that shows the crash. So with what input for `t` and `s` does it crash? Btw, should you count upper case and lower case characters separately or together? And what about the "frequency ...". Have you decided how you will present it? – Tom Brunberg Dec 10 '21 at 23:30
  • Both upper and lowercase characters are to be counted together as the frequency would just get displayed along the lines of `Frequency of Character S: 25` meaning it appeared 25 times in the words or phrase previously coded in – ReallyQuerdey Dec 11 '21 at 08:00
  • ReallyQuerdey: please add the exact compiler and version you use and if possible commandline arguments. – Marco van de Voort Dec 11 '21 at 13:35

2 Answers2

1

The errors you see in the first block:

Identifier not found "Result"

Standard Pascal doesn't recognize the pseudovariable Result. In some Pascal implementations (like e.g. Delphi) it can be used to assign a value to the function result. The Pascal you are using needs to have the result of a function assigned to the name of the function. For example:

function Whatever(): integer;
begin
  Whatever := 234;
end;

Identifier not found "PosEx"

Not all Pascal implementations include the PosEx() function. You need to use Pos() instead. But, the standard implementation of Pos() doesn't include the "search start position" that PosEx has. Therefore you need to ditch Pos() and do as you do in "Input P2", that is traverse the text character per character and count the occurances as you go.

Identifier not found "unsigned"

Seems you have removed that unknown identifier.

The error you see in the second block:

In Output P2 the error message should be clear. You are missing a semicolon where one is needed. Actually you are missing three of them.

You are also missing the line that reads user input: ReadLn(S);.

Finally, to calculate both upper and lower case characters you can use an extra string variable, say SU: string to which you assign SU := UpperCase(S) after reading user input, and then use that string to count the occurances.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • Even though, I put the semicolons after `S:String;` & `I:Integer;` in Input P2, I'm facing the same error with the Output P2. Despite factoring in `ReadLn(S)` below `writeln("Number of occureances",k);`. Would you mind telling me where in the code should I consider adding the third semicolon? – ReallyQuerdey Dec 11 '21 at 11:45
  • You need to have a semicolon after `program p1`. Regarding `ReadLn(S);`, try hard to think again, when is the appropriate time to read user input. – Tom Brunberg Dec 11 '21 at 11:53
  • `Compiling main.pas main.pas(8,1) Error: Identifier not found "k" main.pas(8,4) Error: Identifier not found "o" main.pas(9,21) Warning: Variable "S" does not seem to be initialized main.pas(10,14) Fatal: Syntax error, "THEN" expected but ")" found Fatal: Compilation aborted Error: /usr/bin/ppcx64 returned an error exitcode` What is this about, I typed in the semicolons, and `Readln(S);` is supposed to be after `writeln` or before. Either way, I'm guessing there's something wrong with the variables! – ReallyQuerdey Dec 11 '21 at 12:04
  • You need to declare variables in a `Var` section before usage. If you ask the user for input, do you expect to get that input before you ask or after. – Tom Brunberg Dec 11 '21 at 12:46
  • From the compilation output it seems that Free Pascal is used. It is probably a matter of enabling Delphi mode in lazarus, or passing -Sd to the FPC commandline. The default mode is more Turbo Pascal alike. The "unsigned" bit of the error is a bit weird though and suggests an different compiler that maps closer to C/C++. – Marco van de Voort Dec 11 '21 at 13:32
  • Come to think of `Var`, didn't I declare it at the beginning of the code when I typed `Var`, or is there something else impeding me from running this program? – ReallyQuerdey Dec 11 '21 at 14:51
  • @ReallyQuerdey My previous comment regarding `var` sections was in response to your earlier comment where you referred to error messages you received: *"Error: Identifier not found "k" main.pas(8,4) Error: Identifier not found "o" main.pas(9,21)"*. Those variables, `k` and `o` must be declared in a variable section before usage. You can have global `var` sections at the start of program immediately after `program p1;` as you have or you can have local `var` sections as part of functions or procedures. – Tom Brunberg Dec 11 '21 at 16:12
  • How do I add the variables at the start of the program `i, o, k:integer`? Or there's some other way work-around – ReallyQuerdey Dec 12 '21 at 10:20
  • @ReallyQuerdey Look at your existing code and/or documentation for inspiration. – Tom Brunberg Dec 12 '21 at 17:14
0

I think this is more like what you want to do:

function Count(t, s: String): Integer;
var
  Offset,Res, P: Integer;
begin
  Res := 0
  Offset := 1;
  repeat
       P := Pos(t, s, Offset);
       if p>0 then 
          Inc(Res);
       Offset := P+1
  untl P = 0;
  Count := Res; 
end;

Now, if you don't have Pos, you can implement it:

Function Pos(const t,s:string; const Start:integer):Integer;
Var
    LS, LT,     {Length}
    IxS, IxT,   {Index)
    R: Integer; {Result}

begin
    R := 0;
{use only one of the two following lines of code}
{if your compiler has length}
    LS := length(S); LT := Length(T);
{If it does not}
    LS := Ord(s[0]); LT := Ord(T[0]);

    if (LS <= LT)  {if target is larger than search string, it's not there}
      and (Start<=LT) and {same if starting position beyond size of S}
       (Start+LT <-LS)  then  {same if search would go beyond size of S} 
    begin  {Otherwise, start the search}
        ixT := 1;
        ixS := Start;
        repeat 
             Inc(R); {or R:= R+1; if INC not available }
             If (S[ixS] <> T[ixT]) then
                  R := 0  {they don't match, we're done}
             else
             begin {Move to next char}
                 Inc(ixS);
                 Inc(ixT);
             end;
        until (R=0) or (ixT>LT); {if search failed or end of target, done}
        Pos := R;
    end;