2D Array - in PASCAL with loop - arrays

I need to write a program which demonstrates 2d array, the program need to ask for student name and then the mark.. and this is for 15 students. At the end program outputs 15 names with their marks next to it.
I don't know how to manipulate 2D arrays accurately, I managed to do simple version without using for loop.
`
table: array[1..2, 1..15] of string; {2 rows for 15 columns}
begin
clrscr;
writeln('Enter 15 student names, and a set of marks after each ');
writeln('With marks you can enter more marks after comma for e.g. 34, 26, 31 etc.');
writeln('Enter NAME and SURNAME of STUDENT NR 1 or q to quit ');
read(table[1][1]); {read name in row 1 col 1}
readln;
writeln(' Enter MARKS of STUDENT NR 1 ');
read(table[2][1]); { read mark in row 2 col 1}
readln;
clrscr;
writeln('Enter NAME and SURNAME of STUDENT NR 2 or q to quit ');
read(table[1][2]);
readln;
writeln('Enter MARKS of STUDENT NR 2 ');
read(table[2][2]);
readln; read marks into row 2 column 2}
`
etc. up to student nr 15... but I know I could use two for loops to do that for me instead of copying code for each student... same for output... I've spend many hours to figure out how to do it with different tries... but still Im confused with using two for loops and indexing this array correctly.
could someone help me with this appropriate looping ?
Thanks

Wow, Pascal, haven't seen it for a very long time! It's a good language for learning to program.
Try something like this..
program testarray2d;
uses crt;
var
table: array[1..2, 1..15] of string; {2 rows for 15 columns}
counter: Integer;
Begin
writeln('Enter 15 student names, and a set of marks after each ');
writeln('With marks you can enter more marks after comma for e.g. 34, 26, 31 etc.');
For counter := 1 to 15 do
Begin
writeln('Enter NAME and SURNAME of STUDENT NR ');
write(counter);
write(' or q to quit ');
readln(table[1][counter]);
writeln('Enter MARKS of STUDENT NR ');
write(counter);
readln(table[2][counter]);
clrscr;
End;
End.
Please let me know if it works, because I haven't tested it.

Related

Initialization of field in array of records (Pascal) [duplicate]

This question already has an answer here:
Lazarus readln doesn't read the variable
(1 answer)
Closed 4 years ago.
I have a small code where I want to initialize an array of records by fields and then just output this records on screen.
Data types:
type
grade = 1..5;
Person = record
Name: string[16];
isMale: boolean;
grades: array [1..6] of grade;
end;
var
Table: array [1..10] of Person;
R: Person;
N,J,I: Integer;
Part of code with initialization and output:
readln(n);
if N>10 then N:=10; if N<1 then N:=1;
for I:=1 to N do begin
R:=Table[I];
//write('Gender?'); readln(j); R.isMale:=j>=0; <= This works just fine
write('Name? '); readln(R.Name);
write('Gender? '); readln(j); R.isMale:=j>=0;
write('Grades? '); for j:=1 to 6 do read(R.grades[J]); writeln;
end;
for I:=1 to N do begin
R:=Table[I];
write(I,' ', R.Name,' ',R.isMale);
end;
When I enter info about first person it works fine, but then every other person's name input is skipped (output is "Name? Gender? ). If I switch entering boolean and string, code works correct, but that's not a logic order.
Why is this happening?
At the end of the loop, you should assign the record to the array. Note that, unlike with classes, assigning a record copies the data in the record, it does not reference the record. So instead of what you have, rather do:
for I := 1 to N do
begin
//write('Gender?'); readln(j); R.isMale:=j>=0; <= This works just fine
write('Name? ');
readln(R.Name);
write('Gender? ');
readln(j);
R.isMale := j >= 0;
write('Grades? ');
for j := 1 to 5 do
read(R.grades[J]);
readln(R.grades[6]); // readln reads the end-of-line too.
writeln;
Table[I] := R; // copy the data from R into the table
end;
That way, the data from the record R is copied into the table. There is no need to copy R from the table at the beginning of the loop, as the table is empty anyway.
Unlike with classes, with records like this, you could do the following too:
write('Name? ');
readln(Table[I].Name);
write('Gender? ');
readln(j);
Table[I].isMale := j >= 0;
// etc...
And in the final loop:
Writeln(I, ' ', Table[I].Name, ' ', Table[I].IsMale);
without using R at all.

PASCAL Arrays (choose Common values from within an array and insert it into a variable)

Note: This is not a Homework I don't Want an Answer for the Question given here it is given so that you all can get a clearer understanding on what i ask since it's Hard for me to explain it in English, please forgive me and try to understand what I ask.
This was a question in my Sisters ICT Exam Paper, She asked my help to find the answer but I know only a very little Turbo Pascal. I appreciate if you could help. at least how to calculate how many common Values are within an Array and put them to a variable.
Here is the question.
In array T(8) - Year of birth of Employees
In array P(8) - Gender
In array S(8) - Civil Status
Element of array Y with number N - Year of Birth of Employee N
Element of array P with number N - Gender of Employee N
Element of array S with number N - Civil Status of Employee N
P(N) = 0, Female Employee
P(N) = 1, Male Employee
S(N) = 0, Single Employee
S(N) = 1, Married Employee
Find
K = Number of Female Employees in age 55/ Total Number of Women * 100
What I ask is how to get the the total of a common value in an array(ex: Array=[1,0,0,1,1] there are only 2 0's) to a variable.
Thanks in Advance
I downloaded a Book on Pascal and read it until now and Figured it out at least a little... I post so it might help someone else.
It goes like this, It Can be Wrong but it worked somehow for an extent
Program Emp_Details(input, output);
uses crt;
var
Y: Array[1..8] of integer;
P: Array[1..8] of integer;
S: Array[1..8] of integer;
I: integer;
K: real;
YearCheck: integer;
FemaleCount: integer;
begin
clrscr;
For I:= 1 to 8 Do
begin
Write('Please enter Year of Birth: ');
Readln(Y[I]);
Write('Please Enter Gender: ');
Readln(P[I]);
if(P[I] = 0) then
FemaleCount := FemaleCount + 1;
Write('Please Enter Civil Status: ');
Readln(S[I]);
if(Y[I] = 1961) then
if(P[I] = 0) then
YearCheck := YearCheck + 1;
end;
K := (YearCheck/FemaleCount) * 100;
Writeln(K);
Readkey;
end.

Making an (iterative?) Do loop in SAS

I know this is simple but I can't seem to figure it out.
I have a dataset that has 50 students, and one of the columns is called test score which has a test score for each of the students. I need to go through and find the difference between all of the students- so student 1 score- student 2 score, student 2 score-student 3 score..... to 50 then student 2 score-student 3 score,... student 2 score-student 50 score.
I essentially need to end up with a matrix of differences for the test scores.
I have to use an array- so it would be something like
Data:
Student Score
Alejandro 91
Atkinsin 87
Beal 72
Butler 94
Coleman 91
data array;
set testscores;
array score(50) Score1-Score 50; ?I dont think this is correct
do i=1 to 50;
difference= score(i) -score(i+1)?? I really have no idea everything I try isn't working
end;
run;
I need to end up with something that has the difference between every students scores
The first data step generates random scores between 1 (a) and 100 (b) following the uniform distribution for 50 students.
Then proc distance calculates the differences between the score of each student with all other students. For this to work, "Student" should be a character variable.
data scores;
a = 1;
b = 100;
do Student_temp = 1 to 50;
Student = compress(put(student_temp, 8.));
u = ranuni(12345);
Score = floor(a + (b-a)*u);
output;
end;
drop Student_temp a b u;
run;
proc distance data=scores out=Diff method=Euclid;
var interval(score);
id Student;
run;

PASCAL Can I take array elements and assign them to integer variables?

I am currently trying to write a program which provides a quarterly overview of the amount of money that a user defined number of salesmen has made for a whole year.
Here is the code I have at the moment:
program Verkopers;
var month, day, sellernr:array[0..99] of integer;
sale:array[0..99] of real;
count, num: integer;
begin
num := 0;
writeln('Enter seller number. To stop enter "0"');
readln(sellernr[num] );
while (sellernr[num] <> 0) do
begin
writeln('Enter date in DD MM format');
readln(day[num] , month[num] );
writeln('Enter sale amount');
readln(sale[num] );
num := num + 1;
writeln('');
writeln('Enter seller number. To stop enter "0"');
readln(sellernr[num] );
end;
writeln('Seller Nr.':10, 'Date':14, 'Amount':16);
for count := 0 to num-1 do
begin
writeln(sellernr[count], day[count]:20,'/',month[count], sale[count]:14:2);
end;
writeln('');
writeln('ENTER to stop');
readln();
end.
So as you see, the program asks for the seller number, then date in DD MM format and then the amount of the sale.
It the prints out to the screen.
What I have to do next is provide the per quarter overview. I have to take each seller number which the user has defined in the array sellernr and add up the total sales per quarter.
That's where I have a problem. How do I take the values that are stored in the array and recognise when they are equal (ie when sellernr[x] and sellernr[y] are the same)
Say the seller numbers the user inputs are 10, 50, 100. I must then take only the array elements which correspond to, first, 10 and print out, for example:
Seller Quarter1 Quarter2 Quarter3 Quarter4
10 $360.32 $567.21 $988.27 $1023.66
How can I take sellernr[x] which correspond to a particular user-defined value?
You can do something like this:
program Verkopers;
var
SalesByQuarters:array[1..4]of real;//here you will store the sums
ReporSeller:integer; //this will be the nr. of the seller for report
begin
...//append your code with this
writeln('Write seller number for report: ');
readln(ReporSeller);
for count:=1 to 4 do
SalesByQuarters[count]:=0;//initialize the output variables for each quarter
for count:= Low(sellernr) to High(sellernr) do
begin
if sellernr[count]=ReporSeller then //count will store the proper index
SalesByQuarters[((month[count]-1) div 3) +1]:=
SalesByQuarters[((month[count]-1) div 3) +1]+sale[count];
end; //((month[count]-1) div 3) +1 gives us the number of quarter.
// You can get it in a different way (if/case/etc)
write('Report for seller ');
writeln(ReporSeller);
for count:=1 to 4 do
begin
write('Quarter ');
writeln(count);
writeln(SalesByQuarters[count]:14:2); //output the stored sums
end;
readln;
end.
You can also beautify the output by something like this:
writeln('Seller Quarter1 Quarter2 Quarter3 Quarter4');
writeln(ReporSeller:6,
' $', SalesByQuarters[1]:7:2,
' $', SalesByQuarters[2]:7:2,
' $', SalesByQuarters[3]:7:2,
' $', SalesByQuarters[4]:7:2);

How do I write arrays in pascal?

Is this program correctly written with the array?
Program Malaria_Outbreak (input,output);
Var
BC:real;
LO:real;
count:integer;
Num:integer;
BloodTest:integer;
Registration:integer;
Clinic:string;
DoctorFee:integer;
Total:integer;
NMB_Payable:real;
Company:string;
Name:string;
Patient:Array[1..10] of string
Begin
clrscr;
BC:=(0.8);
LO:=(0.7);
Count:=(0);
Num:=(0);
BloodTest:=(Num * 700);
Registration:=(500);
Writeln('Please enter the name of the patient');
Readln(Name);
While (Name <> 'END')Do
Begin
For count:= 1 to 10 Do
Begin
Writeln('Please enter the clinic the patient attends');
Readln(Clinic);
If (Clinic = 'Type 1') Then
Begin
DoctorFee:=(800);
End;
If (Clinic = 'Type 2') Then
Begin
DoctorFee:=(1200);
End;
Writeln('The doctor fee for the patient is $',DoctorFee);
Writeln('Please enter the number of blood tests the patient has had');
Readln(Num);
BloodTest:=(Num * BloodTest);
Writeln('The blood test for the patient is $',BloodTest);
TMB:=(Registration + DoctorFee + BloodTest);
Writeln('The total medical bill for the patient is $',TMB);
Writeln('Please enter the insurance company the clinic is affiliated with');
Readln(Company);
If (Company = 'Blue Cross') Then
Begin
NMB_Payable:=(BC * TMB);
End;
If (Company = 'LOJ') Then
Begin
NMB_Payable:=(LO * TMB);
End;
Writeln('The net medical bill for the patient is $',NMB_Payable);
End;
Readln;
Readln;
End
Looks good, but you might want to include the ; after the datatype (string)
Patient : Array[1..10] of String;
There are some problems in the code.
Your code was not formatted. Especially the lack of indenting makes it hard to understand what's going on. (thanks to GolezTrol for fixing that)
You're missing a semi-colon (;) after Array[1..10] of string
Some end; statement is missing. Either While (Name <> 'END')Do begin or For count:= 1 to 10 Do begin should have a matching end; statement.
Variable Tmb is not declared.
Bloodtest will always be 0. It's initialized to 0, and the only time you write to Bloodtest is on this line: BloodTest := (Num * BloodTest);. That's probably not what you want to do.
DoctorFee is uninitialized unless the user types Type 1 or Type 2. NMB_Payable has a similar problem.
There's a variable Count that's initialized, but never used afterwards. Doesn't do any damage, but for readability I'd clean it up.
To answer your question: No, you're not using the array that's declared, and I don't think this program does what you want it to do.
If you explain what you're trying to accomplish, we can help you out with that.
I don't see where it's writing to the array at all, nor where it would make any use of the array in the first place. It's simply processing each item it gets, nothing is carried to be stored in an array in the first place.
It's also going to ask and bill each patient 10 times. I've heard of double-billing but this is crazy!
You should always run your code and see what actually happens. It's quite obvious you didn't.

Resources