unknown runtime error in pascal - pascal

I am unable to locate the source of this error that i keep getting in my coding for school. Every time i enter one value in my array and the for loop runs it either encounters a runtime error or the rest of the program runs based on information from the first value and doesn't accept any other values. Can someone please explain to me how to fix this?
program Montserrat_Elections;
Var cndnme : array [1..4] of String;
votes : array [1..4] of Integer;
highest, cnt : Integer;
winner : string;
begin
highest:= 0;
winner:= 'Dan';
For cnt:= 1 to 4 do
begin
Writeln('Please enter the first name of the candidate and the number of votes');
Read (cndnme[cnt], votes[cnt]);
If votes[cnt] > highest then
highest := votes[cnt];
winner := cndnme[cnt];
end;
Writeln('The winner of this constituency is', winner, 'with', highest, 'votes')
end.

Change Read to Readln :
Readln (cndnme[cnt], votes[cnt]);
Then you need add begin...end; to this line:
If votes[cnt] > highest then
begin
highest := votes[cnt];
winner := cndnme[cnt];
end;
I update & test your codes :
program Montserrat_Elections;
Var cndnme : array [1..4] of String;
votes : array [1..4] of Integer;
highest, cnt : Integer;
winner : string;
begin
highest:= 0;
winner:= 'Dan';
For cnt:= 1 to 4 do
begin
Writeln('Please enter the first name of the candidate and the number of votes');
readln(cndnme[cnt], votes[cnt]);
If votes[cnt] > highest then
begin
highest := votes[cnt];
winner := cndnme[cnt];
end;
end;
Writeln('The winner of this constituency is ', winner, ' with ', highest, ' votes');
readln;
end.
Result:
Please enter the first name of the candidate and the number of votes
Me
23
Please enter the first name of the candidate and the number of votes
You
42
Please enter the first name of the candidate and the number of votes
Ainun
18
Please enter the first name of the candidate and the number of votes
Jhon
38
The winner of this constituency is You with 42 votes

Related

How to solve this syntax error, "identifier" expected but "ordinal const" found?

(14,22) Error: Illegal qualifier
(14,22) Fatal: Syntax error, "identifier" expected but "ordinal const" found
PROGRAM menentukan_bonus_karyawan;
uses crt;
var
nama, golongan: string;
lama_kerja : integer;
bonus : real;
begin
clrscr;
write('Masukkan nama anda : '); readln(nama);
write('Golongan karyawan (staf/ non-staf) : '); readln(golongan);
write('Lama kerja : '); readln(lama_kerja);
case (golongan) of
'staf' : if lama_kerja >= 10 then
bonus := 1.000.000 <--- error line
else
bonus := 700.000;
'non-staf' : if lama_kerja >= 10 then
bonus := 500.000
else
bonus := 350.000;
end;
writeln('Saudara ', nama, ' dengan golongan karyawan ', golongan, ' mendapatkan bonus sebesar Rp. ', bonus);
readln;
end.
The problem is that the value of 1.000.000 is invalid. If you meant one million, then you should use 1000000. If you wanted one thousand, then you could use 1000.000.

Pascal Pseudocode

I'm learning Pascal Pseudocode in school and my teacher sent me this exercise:
Write an algorithm, in pseudocode, that, when read the name and the ages for N students, counts how many of them have more than 18 years old. Should print the name of those students.
I've writen the code until printing the counting of those students, but how should I do to print their names. Can you guys give me a tip?
PS: I didn't learned anything of arrays or records.
BEGIN
WRITE ('Insert the number of students in the class.');
READ (n);
i := 1;
WHILE i<=n DO
BEGIN
WRITE ('Insert the student's name');
READ (name);
WRITE ('Insert the student's age');
READ (age);
IF age > 18 THEN
count := count + 1;
i := i + 1;
END;
WRITE ('There are ',count,' students with more than 18 years.');
I cannot proceed from here.
You had several errors, you read the variables with READ instead of with READLN and I assume that the variables you have already declared. On the other hand the if it is convenient to have closure (with END)
Finally, remember to "stop" the console so that the message can be displayed
program count_age;
USES CRT;
VAR i,age,count,n:INTEGER;
VAR name:STRING;
BEGIN
count :=0;
WRITE ('Insert the number of students in the class. ');
READLN (n);
i := 1;
WHILE i<=n DO
BEGIN
WRITE ('Insert the students name ');
READLN (name);
WRITE ('Insert the students age ');
READLN (age);
IF age > 18 THEN BEGIN
count := count + 1;
END;
i := i + 1;
WRITE ('There are ',count,' students with more than 18 years.');
readkey;
END.

Pascal multiple records being passed into a function

This is my code:
type
TStudent = record
Firstname:string[15];
Surname:string[15];
dateofbirth:tdatetime;
gender: char;
ASmark: integer;
end;
var student: array [1..10] of tstudent;
option:integer;
...
function readrecord:tstudent;
var //dob:string;
i:integer;
begin
for i := 1 to 10 do
begin
Writeln('Enter Firstname for student ',i, ' : ');
readln(student[i].firstname);
Writeln('Enter Surname ',i, ' : ');
readln(student[i].Surname);
writeln('Enter Date of Birth ',i, ' : ');
//readln(dob);
//student[i].dateofbirth:=strtodate(dob);
Writeln('Enter Gender (M or F) ',i, ' : ');
readln(student[i].gender);
Writeln('Enter AS Mark ',i, ' : ');
readln(student[i].ASmark);
end;
end;
DOB is commented out for now, don't worry about that :)
procedure averagemark(var student:tstudent);
var mark:real;i:integer;
begin
mark:=0;
for i := 1 to 10 do
begin
mark:=mark+(student[i].asmark);
end;
mark:=(mark/10);
writeln('Average mark: ',mark:5:2);
end;
I am trying to get an average of the asmark, but I get errors for mark:=mark+(student[i].asmark); which are Error: no default property available and Fatal: Syntax error,")" expected but "[" found.
It would be much appreciated if someone could point me in the right direction as I have tried quite a lot of things and also researched.
I also created a data type called tstudenta which was an array of the tstudent record, then set student:tstudentr; but had no luck here either.
procedure averagemark(var student: tstudent);
You pass a single record here instead of an array. Use an open array:
procedure averagemark(const students: array of tstudent);
Loop from low(students) to high(students) rather than hard coding the array bounds.
If your Pascal dialect does not support open arrays, then you should use an array. If it suffices to use an array with a fixed length of 10 then you could define the array type like so:
type
TStudentArray = array [1..10] of TStudent;
You'd then use that type for the parameter of your procedure:
procedure averagemark(const students: TStudentArray);
It's a little tricky to be specific without more knowledge of your particular Pascal dialect. However, the essence is as I have said. You need to pass an array of records rather than a single record.

The maximum in each row

I have a question for you. I need to write the maximum element in each line. For example, my table :
1 2 3 4
5 6 7 8
9 10 11 12
I want to get 4,8,12
I tried but no result:
Program Lab2;
type A=array[1..5,1..5] of integer;
var x:A;
i,j,s,max:integer;
Begin
writeln('Write date:');
for i:=1 to 5 do
for j:=1 to 5 do
read(x[i,j]);
for i:=1 to 5 do
for j:=1 to 5 do
begin
max:=x[i,1];
if (max<x[i,j]) then max:=x[i,j];
writeln(max);
end;
readln;
Please help me
end.
There are just three little mistakes:
1) if (max<x[i,j]) should be outside the second for loop, because you want initialize the max value only one time per row.
2) writeln(max); should be outside the second for loop, you want to print the value only one time per row.
3) read(x[i,j]); I reccomend to be readln (x[i,j]) because with read you only read one character, with readln you red characters till you find a new line character, and that will allow you to enter numbers with more than two digits.
This only make sense for strings, you can use read or readln with integers
Also I advice you to write the key word begin in the same line where you write a contol structure (for,while,if,etc), because in this way it's more similar to the C coding style convention, one of the most populars coding styles I guess. And also is better for you if you try to keep a similar coding style for any language.
so the code will be:
Program Lab2;
const SIZE=3;
type A=array [1..SIZE,1..SIZE] of integer;
var x:A;
i,j,max:integer;
Begin
writeln('Write date:');
for i:=1 to SIZE do begin
for j:=1 to SIZE do begin
readln(x[i,j]);
end;
end;
for i:=1 to SIZE do begin
max:=x[i,1];
for j:=1 to SIZE do begin
if (max<x[i,j]) then begin
max:=x[i,j];
end;
end;
writeln('the max value of the row ',i ,' is ',max);
end;
readln;
readln;
end.

Redefine variables which names depends of what user enters

I have some variables ordered by numebers, like ex1,ex2,ex3,etc (integers); i'd like to count how many times the user enter a letter of a number in a way such that if '1' is entered the program adds +1 to ex1, if 2 is enteres the program adds +1 to ex2- and so on (I can't use arrays because it's forbidden in the excersise). For example, if i ask the user to enter 15 numbers between 1 and 15, and i would like to count how many times each one of those is entered, the code i'm thinking of would be something like
for i:=1 to 15 do
read(number);
if number = i then
Begin
exi := exi + 1;
End
Obviously that didn't work. Is it possible to redefine a variable doing something similar?.
If you can't use arrays at all, you have just a couple of choices.
A case statement
for i := 1 to 15 do
begin
Read(number);
case number of
1: ex1 := ex1 + number;
2: ex2 := ex2 + number;
// rest of possible values and variables
else
// Handle number that doesn't have a variable
end;
end;
if..else statements
for i := 1 to 15 do
begin
Read(number);
if number = 1 then
ex1 := ex1 + number
else if number = 2 then
ex2 := ex2 + number
else if // rest of possible values and variables
else // Handle number that doesn't have a variable
end;
end;

Resources