Pascal programming help - algorithm

I posted this earlier and it got closed because I did not show my try at coding it, so here is the question:
SECTIONS
$160 = section 1
$220 = section 2
$280 = section 3
$350 = section 4
$425 = section 5
Develop pseudocode that accepts as input the name of an unspecified number of masqueraders who each have paid the full cost of their costume and the amount each has paid.
A masquerader may have paid for a costume in any of the five sections in the band. The algorithm should determine the section in which the masquerader plays, based on the amount he/she has paid for the costume. The algorithm should also determine the number of masqueraders who have paid for costumes in each section.
The names of persons and the section for which they have paid should be printed. A listing of the sections and the total number of persons registered to play in each section should also be printed, along with the total amount paid in each section.
Here is my try at it: *Note this is programmed in Pascal, and I need help fixing it up and finishing it. Please help and thanks again.
program Masqueraders;
uses
WinCrt; { Allows Writeln, Readln, cursor movement, etc. }
const
MAX = 5; {this determine the amount of masquarader entered}
Type
listname = Array[1..MAX] of string;
listsect = Array[1..MAX] of string;
var
names : listname;
sections : listsect;
i, amount, TotalMas, TotalAmt, c1, c2, c3, c4, c5, amt1, amt2, amt3, amt4, amt5 : integer;
begin
amount := 1;
while amount <> 0 do
begin
i := i + 1;
readln(names[i]);
readln(amount);
if(amount = 160) then
begin
c1 := c1 + 1; {Count the number of persons for section 1}
amt1 := amt1 + amount; {accumulate the amount for section 1}
sections[i] := 'Section 1';
end;
if(amount = 220) then
begin
c2 := c2 + 1; {Count the number of persons for section 1}
amt2 := amt2 + amount; {accumulate the amount for section 1}
sections[i] := 'Section 2';
end; {end the IF for section 2}
if(amount = 280) then
begin
c3 := c3 + 1; {Count the number of persons for section 1}
amt3 := amt3 + amount; {accumulate the amount for section 1}
sections[i] := 'Section 3';
end; {end the IF for section 3}
if(amount = 350) then
begin
c4 := c4 + 1;
amt4 := amt4 + amount;
sections[i] := 'Section4';
end; {end If for section 4}
if (amount = 425) then
begin
c5 := c5 + 1;
amt5 := amt5 + amount;
sections[i] := 'Section5';
end;{end the while loop}
TotalMas := c1 + c2 + c3;
TotalAmt := amt1 + amt2 + amt3;
writeln('Name Section'); {Heading for the output}
for i := 1 to MAX do
begin
write(names[i]);
writeln(' ',sections[i]);
end;
writeln('Section 1: ');
write('Masquader: ', c1);
write('Amount: ', amt1);
writeln('Total Number of Masquarader: ', TotalMas);
writeln('Total Amount Paid by masquarader: ', TotalAmt);
end;
end.
In short, it should accept an undefined number of people and assign them to their respective sections based on the amount of money they entered, then calculate the number of people in each section. This is my current output:
Name John Money=160 Section 1
Name Keith Money=220 Section John
This is what I want:
Name John Money=160 Section1
Name Keith Money=220 Section2

I'm not really familiar with Pascal so I can't tell you how to fix this but one issue I notice is that your code seems to violate the "DRY" rule: Don't Repeat Yourself. The code inside each if amt = xxx block looks almost exactly the same, so is there a way you can write that code once, and then call your code with different parameters each time? Something so that you're not rewriting the same code five times.

Here's are a few hints to improve your code:
Do you think there might be a way to print "Section {i}" in your final loop without looking it up using sections[i], thereby negating the need for the array completely?
How could you build this so that adding a section 6 wouldn't require modifications to your code?
listname and listsect are awfully similar, what modifications to your code could you do to eliminate the need to have two identical definitions?
What happens if the user enters 0 for the amount the third time they are prompted?
Note: one of these hints should point you directly to the source of your problem.

Here are the problems I see:
The requirements say an "unspecified number of masqueraders" but you have set the max to 5. You can't store the list of names in a fixed size array if there are possibly going to be more than 5 masqueraders. As is, the app may either crash or corrupt memory (depending on the version of Pascal you're using) when user enters the 6th masquerader. If you are allowed to print the masquerader's Name and Section immediately after it is input, then you should print that right after the user inputs the Name and Amount (not after the input while loop). However, if it is required that you print the list of Names and Sections after all input, then you need to store the Name+Section in a variable length data structure like a linked list or even simpler a string.
The variables c1 to c5 and amt1 to amt5 would be better declared as two arrays (c and amt) of 1..MAX instead of 10 variables. When user inputs the amount, use it determine the section number which then can be used as the index into the c and amt arrays.
Not sure what implementation of Pascal you're using but it would be safer to initialize all variables before use otherwise they could contain unpredictable values.
The sections array is unnecessary. All it ends up containing is the title of the section which doesn't need to be calculated or stored.
If you used a repeat/until loop instead of a while loop, it would avoid the slightly kludgy "amount := 1" initialization at the top to enter the while loop. The repeat/until would be until Amount = 0.
TotalMas is only adding c1 to c3 (what about c4 and c5)?
TotalAmt is only adding amt1 to amt3 (what about amt4 and amt5)?
The for-loop to print the names and section is completely wrong, see points 1 and 4.
Hope this helps and let me know if you need clarification on any points.

My gripes with this code:
1) The variable names stink. Virtually all of them are way too short, they do not communicate what they do.
2) The wrong loop control structure is used here.
3) I see virtually identical code repeated 5 times. This is simply crying out for arrays.
4) The comments explain what you are doing, not why you are doing it. Half of them are virtually completely useless, if you're going to comment an end you should simply comment it with the identity of whatever it's an end to. end; //if I see only one comment that even tries to explain why and you've got it wrong as you're mixing up people with sections.
5) Your sections array accomplishes nothing useful. The assignments to the values are always fixed and thus there is no reason to store them at all--if you need the labels you can create them at print time.
6) You are assuming there are only going to be 5 people. That's not given in the problem--there's 5 sections.

Related

Why does syntax error appear after running code?

presently learning how to code in pascal and vba. Assisting my daughter who is preparing for examinations next year. I am stuck on a problem concerning her present assignment. After running the code the following errors were received:
main.pas(1,2) Fatal: Syntax error, "BEGIN" expected but "identifier N" found
Fatal: Compilation aborted
Tried fixing the code, but as i said I have just started learning to code.
n: integer; (*n is ID number for each candidate*)
DV: integer; (*DV is the number of district votes available*)
VR: integer; (*VR is the number of votes received by the candidate in
the district*)
x: integer;
y: integer;
Divide: integer;
found: Boolean;
n: array[1..10] of integer; (*n is an array of 10 integers*)
Names: array[1…10] of string = (‘Richards’, ‘Gumbs’, ‘Carty’,
‘Fleming’, ‘Jones’, ‘Amorowat’, ‘De la cruz’, ‘Walker’,
‘Brooks’, ‘Baker’);
DV: array[1…10] of integer = (‘200’, ‘900’, ‘700’, ‘100’, ‘80’, ‘15’,
‘6, ‘20’, ‘50’, ‘1’);
VR: array[1…10] of integer = (‘50’, ‘700’, ‘600’, ‘20’, ‘30’, ‘2’,
‘6, ‘3’, ‘30’, ‘2’);
For x := 1 to 10 do
Begin
Repeat
Found:=false;
writeln('Enter Candidate ID Number: ’);
readln(n);
For y:= 1 to 10 do
if n = n[y] then
Found = true;
writeln(‘Name of Candidate is’ Names: array[1] ‘.’);
readln;
writeln(‘Number of votes available in the District is’
DV: array[1] ‘.’);
readln;
writeln(‘Number of votes received by the Candidate in the
District is’ VR: array[1] ‘.’);
readln;
Endif;
For y:= 1 to 10 do
if n = n[y] then
Divide:= (DV: array[1] DIV VR: array[1]);
Result:= Divide;
writeln(‘The percentage of votes received by’ Names:
array[1] ‘is’ Result ‘.’);
readln;
if Result:>= 0.20 then
writeln(‘The candidate,’ Names: array[1] ‘is to receive a
refund.’);
readln;
Elseif
writeln(‘The candidate,’ Names: array[1] ‘will not receive
a refund.’);
readln;
Endif;
Endif;
End;
The expected result is to choose a candidate by his ID number which would lead to his name, the number of vote available in a district and the number of votes the candidate obtained being displayed. it would then result in a calculation between the two vote counts (division) and if the percentage is greater than 20% he would receive a refund, if less than 20% he would not receive a refund. either result should be displayed.
I'm afraid that you q, as it currently stands, isn't really suited to SO
because SO is really about specific (single) programming problems, not
incrementally debugging source code (see Sertac's comment), nor providing
online tutorials, which I think you probably need at this point. And I feel
rather uncomfortable about posting this as an answer, because it isn't one
in the normal SO sense. However it seems to me that you could use a few pointers:
Unless you absolutely have to use the online compiler, download
and use a free online one like Free Pascal, which is well supported, uses
standard Pascal syntax, and I'm sure there are basic first-time tutorials
available. See here to download Lazarus, which is an excellent IDE for Free Pascal (which is included
in the install) and here for an introductory tutorial.
Secondly, there are structural and syntactic elements of your source-code
which are definitely not standard Pascal, in particular endif and elseif.
Another example is that in standard Pascal, you have to surround a string (like your
Richards, etc) with single-quotes ', not precede the string by a back-quote. This is very possibly the cause of your "illegal character" error
For a decent Free Pascal introductory
tutorial see here and this youtube tutorial, both found by googling
"free pascal" introductory tutorial.
Fourthly, your online compiler ought to be complaining about the endif abd elseifs, the incorrectly string formatting and the fact that several of your variables are duplicated (DV and VR used as the names of an integer variable and an array, for example as in Pascal, identifiers within the same 'scope' need to have unique names (the fact that I should explain what 'scope' means is a sign that what the q needs is a tutorial).

Reading a text file and constructing a matrix from it

I need to construct a matrix; a number of columns and rows are also in the first row of the matrix, I'll make an example so its more clearer.
4 3
1 2 3
5 6 7
9 10 8
1 11 13
Where m=4 (number of rows) and n=3 (number of columns)
This is an example of a text file. Is something like this even possible?
Program Feb;
const
max=100;
type
Matrix=array[1..max,1..max] of integer;
var datoteka:text;
m,n:integer;
counter:integer;
begin
assign(datoteka,'datoteka.txt');
reset(datoteka);
while not eoln(datoteka) do
begin
read(datoteka, m);
read(datoteka, n);
end;
repeat
read eoln(n)
until eof(datoteka)
write (m,n);
end.
My code isn't a big help, cause I don't know how to write it.
First, have a look at the code I wrote to do the task, and then look at my explanation below.
program Matrixtest;
uses
sysutils;
var
NoOfCols,
NoOfRows : Integer;
Source : TextFile;
Matrix : array of array of integer;
FileName : String;
Row,
Col : Integer; // for-loop iterators to access a single cell of the matrix
Value : Integer;
begin
// First, construct the name of the file defining the matrix
// This assumes that the file is in the same folder as this app
FileName := ExtractFilePath(ParamStr(0)) + 'MatrixDef.Txt';
writeln(FileName); // echo it back to the screen so we can see it
// Next, open the file
Assign(Source, FileName);
Reset(Source);
read(Source, NoOfRows, NoOfCols);
writeln('Cols: ', NoOfCols, 'Rows: ', NoOfRows);
SetLength(Matrix, NoOfCols, NoOfRows);
readln(source); // move to next line in file
// Next, read the array data
for Row := 1 to NoOfRows do begin
for Col := 1 to NoOfCols do begin
read(Source, Value);
Matrix[Col - 1, Row - 1] := Value;
end;
end;
// Display the array contents
for Row := 1 to NoOfRows do begin
for Col := 1 to NoOfCols do begin
writeln('Row: ', Row, ' contents', Matrix[Col - 1, Row - 1]);
end;
end;
Close(Source); // We're done with the file, so close it to release OS resources
readln; // this waits until you press a key, so you can read what's been displayed
end.
In your program, you can use a two-dimensional array to represent your matrix. Free Pascal supports multi-dimensional arrays; see https://wiki.lazarus.freepascal.org/Multidimensional_arrays for more information.
This is a complex task, so it helps to know how to do more basic things like reading an array of a size known at compile-time from a text file.
The wrinkle in this task is that you are supposed to read the dimensions (numbers of rows and columns) of the matrix at run-time from the file which contains the matrix's contents.
One inefficient way to do this would be to declare the matrix array with huge dimensions, larger than anything you would expect in practice, using the type of array declaration in the Wiki page linked above.
A better way is to use dynamic arrays, whose dimensions you can set at run-time. To use this, you need to know:
How to declare a dynamic array in Free Pascal
How to set the dimensions of the array at run-time, once you've picked them up from your matrix-definition file (hint: SetLength is the way to do this)
The fact that a Free Pascal dynamic array is zero-based
The easiest way of managing zero-based arrays is to write your code (in terms of Row and Column variables) as if the matrix were declared as array[1..NoOfRows, 1..NoOfColumns] and subtract one from the array indexes only when you actually access the array, as in:
Row := 3;
Column := 4;
Value := Matrix[Row - 1, Column - 1];

why am i getting 22 / 3 itprog~1.pas Fatal: Syntax error, ; expected but ELSE found

PROGRAM approvedapplicants(input,output);
uses crt;
var
applcntname,housingcomm,clarendon_court,providence_gardens,
sangre_grande_villas:string;
slry,spcslry:integer;
c_qual_sal,s_qual_sal,p_qual_sal,qualifying_salary:integer;
BEGIN
writeln('enter applicant name, salary, spouce salary');
readln(applcntname,slry,spcslry);
writeln('enter housing community');
readln(housingcomm);
BEGIN
qualifying_salary:=0;
IF(housingcomm=clarendon_court)
then
qualifying_salary:=$12500;
writeln('you have selected clarendon court!');
readln(c_qual_sal) ;
end if ;
else if(housingcomm=sangre_grande_villas)then
qualifying_salary:=$9500;
writeln('you have selected sangre grande villas!');
readln(s_qual_sal);
end if ;
else(housingcomm=providence_gardens)then;
qualifying_salary:=$7500;
writeln('you have selected providence gardens!');
readln(p_qual_sal);
end if;
END.
Ordinarily, on SO, we don't post answers to homework/coursework, but your code is so far wide of the mark that I think it's ok to make an exception in this case.
Try compiling and running this program, which I think does pretty much what I think you are intending, then I'll explain a few things about it:
program approvedapplicants(input,output);
uses crt;
var
ApplicantName,
HousingCommunity,
ClarendonCourt,
ProvidenceGardens,
SangreGrandVillas :string;
Salary,
SpouseSalary,
QualifyingSalary : Integer;
CQualSal,
PQualSal,
SQualSal : Integer;
slry,spcslry:integer;
begin
ClarendonCourt := 'Clarendon Court';
ProvidenceGardens := 'Providence Gardens';
SangreGrandVillas := 'Sangre Grand Villas';
QualifyingSalary := 0;
writeln('enter applicant name');
readln(ApplicantName);
writeln('enter salary');
readln(Salary);
writeln('enter spouse salary');
readln(SpouseSalary);
writeln('enter housing community');
readln(HousingCommunity);
if (HousingCommunity = ClarendonCourt) then begin
QualifyingSalary := $12500;
writeln('you have selected clarendon court!');
readln(CQualSal);
end
else
if(HousingCommunity = SangreGrandVillas)then begin
QualifyingSalary := $9500;
writeln('you have selected sangre grande villas!');
readln(SQualSal);
end
else
if HousingCommunity = ProvidenceGardens then begin
QualifyingSalary :=$7500;
writeln('you have selected providence gardens!');
readln(CQualSal);
end;
end.
Firstly, notice how much easier it is to read and follow its logic. This is mainly
because of
The use of a layout (including indented blocks) which reflects the logical
structure of the code.
The use of consistent, lower case for keywords like program, begin, end, etc.
Keywords are usually the least interesting contents of source code, and it is distracting
to have them SHOUTing at you.
The avoidance of arbitrarily dropping characters from variable names (like the "i"
and second "a" of "applicant". In the days of interpreted code running on slow machines there was
argubably some justification for this, but not any more imo. Likewise, the avoidance
of underscores in variable names - admittedly this is more of a personal preference, but
why have you used them everywhere except the applicant's name?
Secondly, you still have quite a bit of work to do.
Having 3 different variables for the salary (?) numbers you prompt the user
for, one for each of the 3 communities, is probably a bad idea unless you will
subsequently want to work with all 3 figures at the same time. Also, you haven't provided text prompts to tell the user what information to enter for the readln(c_qual_sal) etc statements. It wasn't obvious to me what you intend, so I have not tried to guess.
The way you echo the user's choice of community is just creating you a maintenance
headache (what if you want to add more communities later?). It would be better
to have a variable which you set to whichever of the community names matches
what the user has entered.
You have 3 statements to execute for each community, which are duplicated for
each community. The only one you actually need is the QualifyingSalary one -
the others can execute regardless of the inputted community.

Very strange behaviour in the increment of a FOR

I'm having a problem when I use these 2 FOR to initialize a two dimensional vector/array:
I have these types defined:
type
Range9 = 0..8;
Digit = '0'..'9';
Board = array [Range9,Range9] of Digit;
and then the part of the code where there are problems with the FOR's is the following:
var
i : Range9;
j : Range9;
table : Board;
BEGIN
for i:=0 to 8 do begin
for j:=0 to 8 do begin
table[i,j] := '0'
end
end;
END.
Now the problem is that, when I debug this portion of code, for some reason, my i variable is modified when it's not supposed to.
For example, I have a watch on i and j and if I put a breakpoint in the line table[i,j] := 0
I see with the watches these values:
i j
0 0
256 1
512 2
768 3
1024 4
1280 5
1536 6
1792 7
2048 8
2049 8
1 0
257 1
513 2
769 3
and so on...
So, when the program enters in the second for (the one that increases the j) my i increases in intervals of 256... I really don't know why is this happening.
And another thing I discovered is that, the problem solves if I change the TYPE of the i variable.
If in the VAR section I put i : integer instead of i : Range9, i doesn't get modified when isn't supposed to.
I would really appreciate if someone explains me why is happening this.
I've found the answer to my own question... well, I didn't exactly found the answer, I've asked this same question in the forum board of the programming course I'm attending and one of the professors gave me this link:
(it's in spanish btw)
http://www.fing.edu.uy/inco/cursos/prog1/pm/field.php/FAQ/Laboratorio#toc17
A quick translation:
This happens with variables defined as subranges. The reason isn't sure; but without doubt is an implementation error of the debugger. There is a 'trick' that can work to solve this (although not always), to be able to see the correct values on the debugger:
Suppose that you have the following variable in your program:
var anything: 1 .. 10;
Add in your program a integer variable which won't be used in any part of the program:
var anything: 1..10;
aux: integer; { only for the debugger }
Then when you define the debugger watch, instead of adding the anything variable, you should add the following expression:
aux:= anything
The aux variable can be used to view different variables, so you only need to declare one aux variable.
In some cases, the previous may not work. Another solution is to change the type of all the variables defined with subranges to integer, char, string, etc (depending the case) only for debug and the change it back again.
end of the translation.
Hope this will be useful for someone else facing the same error.
BTW, this happens with the debugger of free pascal IDE 2.2.2 , maybe in another IDE/compiler/debugger of pascal it doesn't happen.
I haven't done Pascal in a while, so I might be a bit rusty. The only thing I can think of that is creating your problem is that you created a character range that was interpreted as a byte array, which was then converted to a Digits and then multiplied, which gave you those weird values. But, I could be wrong. I am unfamiliar with FreePascal.
Type
Range9 = 0..8
Board = Array[Range9,Range9] of Integer;
var
A : Board;
I,J : Integer;
begin
For I:=0 to 8 do
For J:=0 to 8 do
A[I,J]:=I*J;
end.
Reference: ftp://ftp.freepascal.org/pub/fpc/docs-pdf/ref.pdf

Free Pascal - Problem solving query (not syntax) - how to approach the next phase of this loop

I have more of a 'problem solving' question than a syntax related problem.
Briefly, I'm creating a program that will read a text file full of words (that may feasibly be a list of passwords), one word per line - I'll be using ReadLn for that bit in a loop. For every word it finds, I want it to add "an amount" of obfuscation in line with how users these days will use '3' instead of 'E' in their passwords, or '1' instead of 'I'. I work in the IT security field and password breaking is often part of it and that's what the program is for.
I have managed to create the program so far that it generates a LEET table full of many different values for each letter of the alphabet and stacks them in a StringGrid that I can access as part of the process (and it is also outputted visually to a table).
type
TLetters = 'A'..'Z';
TLeet = array[TLetters] of TStringList;
var
SourceFileName, str : string;
StartIndexFile : TextFile;
i : TLetters;
leet : TLeet;
s : string;
n, o, ColumnSize : integer;
begin
for i in TLetters do
leet[ i ] := TStringList.Create;
// The next sequence of loops populates the string grid by counting the number of characters for each letter of the alphabet and then inserting them down, column by column and row by row...
//Letter A:
s := '4 # /-\ /\ ^ aye ∂ ci λ Z';
ColumnSize := wordcount(s,[' ']);
o := 0;
for n := 0 to ColumnSize do
leet['A'].Add(ExtractWord(n,s,[' ']));
for o := 0 to ColumnSize do
StringGrid1.Cells[1,o] := Leet['A'][o];
// And so on for B - Z
// ... then an OpenDialog that opens the source text file to read. Got that sorted
// A load of file opening stuff and then the obsfucation
repeat
Readln(StartIndexFile, Str);
LblProgress.Caption := ('Parsing Index File...please wait');
OBSFUCATE THE WORDS HERE TO SOME EXTENT
// but now I have hit a barrier....
until(EOF(StartIndexFile));
My problem is this : given the word 'Edward', for example, how do I decide to what level I should obfuscate it? Just the first letter 'E' to be replaced with a '3', and nothing more perhaps? Or the first two letters 'E' and 'd' to be replaced with ALL the values in the LEET table for both of the letters E and d (meaning dozens of new words would be generated from 'Edward', and so on), or all the values for 'E' but nothing else...the list goes on. Potentially, for every word, I could create thousands of additional one's! A 100Gb source file would soon become terabytes!
In other words, I need to set "a level" for which the program will function, that the user can decide. But I'm not sure how to structure that level?
So I'm not sure how to make it work? I didn't really think it through enough before I started. My initial thoughts were "It would be cool to have a program that would take an index of words from a computer, and then generate variations of every word to account for people who obfuscate characters." but having come to code it, I've realised it's a bigger job than I thought and I am now stuck at the section for actually 'LEETing my input file'!
You could use a level (0-10) as input.
0: replace nothing
10: replace all letters with LEET letters.
Depending on the length of the word, you calculate how many letters to replace and just replace random letters in the word, so that you not always replace the first letter for level 1 etc.

Resources