It skips the first output whenever i print the list, how do i correct this? - freepascal

Whenever i try to print the list of voters (output) with first entering the information for 5 eligible persons named a, b, c , d and e and the other 5 are not eligible named f, g, h, i, j. it outputs b, c, d, e, f as the valid voters when it should be a, b, c, d, e
PROGRAM List_of_Eligible_Voters;
{States whether a person is a valid voter or not}
Const
YOE=2019;
VAR
Felony_Status: string;
Eligibility:ARRAY[1..10] of string;
Name:ARRAY[1..10] OF string;
YOB,Age:integer;
Count:integer;
i:integer;
BEGIN
i:=0;
Count:=0;
FOR i:= 1 TO 10 DO
Begin
Name[i]:='Name';
Eligibility[i]:='Eligibility';
End;
Writeln('Please enter the name of the person.');
Readln (Name[Count]);
Writeln ('Please enter the year of birth of the person.');
Readln (YOB);
Writeln ('Have the person ever been convicted of a felony?()Answer with yes/no.');
Readln (Felony_Status);
While (YOB <> 0) AND (Count<=10) Do
begin
Age:= YOE-YOB;
IF (Age>= 18) AND (Felony_Status = 'no')Then
Eligibility[Count]:= 'yes'
ELSE
Eligibility[Count]:= 'no';
Writeln('Please enter the name of the person.');
Readln (Name[Count]);
Writeln ('Please enter the year of birth of the person.');
Readln (YOB);
Writeln ('Have the person ever been convicted of a felony?()Answer with yes/no.');
Readln (Felony_Status);
COUNT:=COUNT+1;
END;
Writeln ('List of Eligible Candidates');
FOR i:= 1 TO Count DO
begin
IF Eligibility[i]= 'yes' THEN
Writeln (Name[i], '()is eligible to vote.');
END;
readln;
END.

Looks like you're using Count as your Index (i) when writing, then Index as your Index when reading. Count is 0 based, and index is 1 based. Have you tried syncing the two up?

Related

How to draw the activation record of the following code

I am having trouble drawing the activation record for the procedure Third. The language is assumed to be stack based and parameter passing is by value-result.
'''
program First
var A, B, C: real;
A = B = C = 1.0;
procedure Second;
var B, C, D : integer;
B = C = D = 2;
Call Third( C, D);
end Second
procedure Third(X, Y: integer)
var C, F, G : integer;
C = F = G = 3;
C:= A + Y;
end Third
call Second();
end First
'''
I understand that an activation record consists of a pointer, a return value and address, and parameters and I understand that passing by value-result creates a copy of a variable before a procedure and reassigns it to the return value after a procedure. However, I am confused how to align the procedure Third to the activation record and I am unsure what ":=" refers to in this context.

Need help understanding how this Haskell code works

I am trying to learn Haskell programming language by trying to figure out some pieces of code.
I have these 2 small functions but I have no idea how to test them on ghci.
What parameters should I use when calling these functions?
total :: (Integer -> Integer) -> Integer -> Integer
total function count = foldr(\x count -> function x + count) 0 [0..count]
The function above is supposed to for the given value n, return f 0 + f 1 + ... + f n.
However when calling the function I don't understand what to put in the f part. n is just an integer, but what is f supposed to be?
iter :: Int -> (a -> a) -> (a -> a)
iter n f
| n > 0 = f . iter (n-1) f
| otherwise = id
iter' :: Int -> (a -> a) -> (a -> a)
iter' n = foldr (.) id . replicate n
This function is supposed to compose the given function f :: a -> a with itself n :: Integer times, e.g., iter 2 f = f . f.
Once again when calling the function I don't understand what to put instead of f as a parameter.
To your first question, you use any value for f such that
f 0 + f 1 + ... + f n
indeed makes sense. You could use any numeric function capable of accepting an Integer argument and returning an Integer value, like (1 +), abs, signum, error "error", (\x -> x^3-x^2+5*x-2), etc.
"Makes sense" here means that the resulting expression has type ("typechecks", in a vernacular), not that it would run without causing an error.
To your second question, any function that returns the same type of value as its argument, like (1+), (2/) etc.

Variables automatically changes its value

I'm doing this question with Pascal (Google Kick Start 2020 Round A - Workout) and I ran into a problem that doesn't make any sense at all. Here is a part of my program:
var N,K,i,max,max1 : longint;
M : array [1..100000] of longint;
A : array [1..99999] of longint;
begin
readln(N,K);
for i := 1 to N do
read(M[i]);
for i := 1 to N-1 do A[i] := M[i+1]-M[i];
max := 0;
for i := 1 to N-1 do
if A[i] >= max then
begin
max := A[i];
max1 := i;
end;
writeln('max = ',max); writeln('max1 = ',max1);
readln; readln;
end.
So first I type in all the input data which are:
5 6 and
9
10
20
26
30.
When I run the program, the value of max is 10 and the value of max1 is 2.
But when I change the way max gets its value and totally did nothing with max1, the program becomes like this:
uses crt;
var N,K,i,max,max1 : longint;
M : array [1..100000] of longint;
A : array [1..99999] of longint;
begin
readln(N,K);
for i := 1 to N do
read(M[i]);
for i := 1 to N-1 do A[i] := M[i+1]-M[i];
max := 0;
for i := 1 to N-1 do
if A[i] >= max then
begin
max := i;
max1 := i;
end;
writeln('max = ',max); writeln('max1 = ',max1);
readln; readln;
end.
I run the program, and suddenly both the values of max and max1 are 4. How can this happen? Should I delete Pascal?? By the way if you can't install Pascal for some reasons then go to this link:https://www.onlinegdb.com/, select Pascal language and paste my program. Thanks for helping me!

Function with vector as argument in Octave

How can I make a function with a vector as input and a matrix as an output?
I have to write a function that will convert cubic meters to liters and English gallons. The input should be a vector containing volume values ​​in m ^ 3 to be converted. The result should be a matrix in which the first column contains the result in m ^ 3, the second liter, the third English gallon.
I tried this:
function [liter, gallon] = function1 (x=[a, b, c, d]);
liter= a-10+d-c;
gallon= b+15+c;
endfunction
You're almost there.
The x=[a,b,c,d] part is superfluous, your argument should be just x.
function [liter, gallon] = function1 (x);
a = x(1); b = x(2); c = x(3); d = x(4);
liter = a - 10 + d - c;
gallon = b + 15 + c;
endfunction
If you want your code to be safe and guard against improper inputs, you can perform such checks manually inside the function, e.g.
assert( nargin < 1 || nargin > 4, "Wrong number of inputs supplied");
The syntax x=[a,b,c,d] does not apply to octave; this is reserved for setting up default arguments, in which case a, b, c, and d should be given specific values that you'd want as the defaults. if you had said something like x = [1,2,3,4], then this would be fine, and it would mean that if you called the function without an argument, it would set x up to this default value.

Invalid directory path error while connecting to database

I am using oracle 11g and using sqldeveloper to run/edit queries.
My table is:
ID FIRST_NAME LAST_NAME MAJOR CURRENT_CREDITS
10008 A Dd Computer Science 2
10009 B DD History 3
10010 D dd Computer Science 3
10011 C ad Economics 3
10012 D Da History 3
10013 E df History 3
Above table is in CSV format (lect.csv) stored in ‘D:\1Deepak\Data’..
I have created below procedure to import the csv file in oracle, but it’s not working getting below error :
Connecting to the database r.
ORA-29280: invalid directory path
ORA-06512: at "SYSTEM.LOADLECTURER", line 39
ORA-06512: at line 2
Process exited.
Disconnecting from the database r.
Code :
create user D identified by d
CREATE DIRECTORY log_dir AS 'D:\1Deepak\Data';
Grant all privileges to D;
Grant Read, Write on Directory log_dir to D;
CREATE TABLE lecturer (
id NUMBER(5) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(20),
major VARCHAR2(30),
current_credits NUMBER(3)
);
CREATE OR REPLACE PROCEDURE Loadlecturer
AS
p_TotalInserted number;
v_FileHandle UTL_FILE.FILE_TYPE;
v_NewLine VARCHAR2(100); -- Input line
myFirstName lecturer.first_name%TYPE;
v_LastName lecturer.last_name%TYPE;
v_Major lecturer.major%TYPE;
v_FirstComma NUMBER;
v_SecondComma NUMBER;
BEGIN
v_FileHandle := UTL_FILE.FOPEN ('Log_dir','Lect.csv', 'r');
p_TotalInserted := 0;
LOOP
BEGIN
UTL_FILE.GET_LINE(v_FileHandle, v_NewLine);
EXCEPTION
WHEN NO_DATA_FOUND THEN
EXIT;
END;
v_FirstComma := INSTR(v_NewLine, ',', 1, 1);
v_SecondComma := INSTR(v_NewLine, ',', 1, 2);
myFirstName := SUBSTR(v_NewLine, 1, v_FirstComma - 1);
v_LastName := SUBSTR(v_NewLine, v_FirstComma + 1,
v_SecondComma - v_FirstComma - 1);
v_Major := SUBSTR(v_NewLine, v_SecondComma + 1);
INSERT INTO lecturer (ID, first_name, last_name, major) VALUES (1, myFirstName, v_LastName, v_Major);
p_TotalInserted := p_TotalInserted + 1;
dbms_output.put_line(p_totalinserted);
END LOOP;
UTL_FILE.FCLOSE(v_FileHandle);
Exception
WHEN OTHERS THEN
UTL_FILE.FCLOSE(v_FileHandle);
RAISE;
END Loadlecturer;
Could anyone help me in figuring out why above mentioned error is coming?