I want to input two or multiple entries on the same line.
How do I get the program to read 2 or more entries?
begin
Write(' Input student namer and his/her score for the math test ');
readln(names[i],mark[i]);
Writeln;
end;
Related
I am converting number into word with own code using length function.
Here is the code:
CREATE OR REPLACE FUNCTION num_to_words (NUM IN NUMBER)
RETURN VARCHAR2
IS
v_length NUMBER;
v_words VARCHAR2(100);
word_1 VARCHAR2(100);
BEGIN
word_1 := 'HUNDRED';
v_length := LENGTH(NUM);
IF v_length = 3
THEN v_words := SUBSTR(TO_CHAR(TO_DATE(NUM,'J'),'JSP'),1,3) ||' '|| word_1;
END IF;
RETURN(v_words);
END;
Problem is when I enter "100", it successfully converts into "ONE HUNDRED".
How can I implement the code for converting "101" into "ONE HUNDRED ONE".
The JSP conversion will do that for you; you don't need to extract the first digit, or supply the 'hundred' text yourself:
IF v_length <= 3 THEN
v_words := TO_CHAR(TO_DATE(NUM,'J'),'JSP');
END IF;
If you pass in 101 the result is ONE HUNDRED ONE. And because I changed = 3 to <= 3 it will work for any 1, 2 or 3-digit value, so passing in 11 returns ELEVEN.
For longer values you might be looking for something like this, which breaks the number down into groups and converts each group onto words plus the corresponding scale (e.g. 'million'). If you want non-standard grouping then it's a bit more involved, but there's a recent example for lakh here.
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.
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
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.
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.