The task is given: For each character of a given text, indicate its relative frequency of appearance in the text. Build the appropriate chart. A single character message should be printed no more than once.

Here is my draft. The program reads text from a file that can consist of any number of characters (English, Russian letters, all kinds of punctuation marks and all this in a few lines), you only need to display characters-letters. everything went fine until the text consisted of two or three lines, but if you read, for example, a full-fledged poem, then errors begin (displays a message about identical symbols several times, does not display some letters at all, incorrectly counts) and as I already suggested the formula for which the relative frequency is considered to be incorrect (. up to the diagram, it does not reach as I understand that there is no point in taking it while there are such jambs ..

Please see the program and help with the identification of errors

Program chast; uses crt; var t: text; s: string; a: array [0 .. 255] of integer; k, b, i, max: integer; BEGIN clrscr; k := 0; assign(t, 'int.pas'); reset(t); while not(eof(t)) do begin readln(t, s); for i := 1 to length(s) do begin inc(k); case ord(s[i]) of 97 .. 122, 224 .. 255: inc(a[ord(s[i])]); 65 .. 90, 128 .. 159: begin s[i] := chr(ord(s[i]) + 32); inc(a[ord(s[i])]); end; end; end; writeln('Относительная частота повторений символов в тексте'); writeln; max := 0; for i := 32 to 255 do if a[i] <> 0 then begin writeln(chr(i), ' - частота ', (a[i] / 100 * k):3:2, '% встречается в тексте ', a[i], ' раз(а)'); if a[i] > max then begin max := a[i]; b := i end; end; end; writeln; writeln; writeln('всего символов - ', k, ' больше всего встречается - ''', chr(b), ''' ', max, ' раз(а)'); close(t); readln; END. 
  • @student___, To format a code, select it with the mouse and click on the {} button of the editor. - Zelta

1 answer 1

To be honest, it's easier for me to write everything anew than to figure out your code. Especially not commented.

Try to take a piece of paper and analyze your algorithm. Most likely a mistake in it. If you did not find the error, check the code. I advise you to break it into functions. I would make the function is_letter , which receives a character and returns False if it is not a letter and True if it is a letter. I also advise you to use standard functions such as UpperCase, etc., they greatly simplify life, because s[i]:=chr(ord(s[i])+32); - it looks awful.