brianrhg
asked on
Paramaters
Hey,
This program is suppose to take 5 names, alphabetize them, and print them out. It uses records parameters and procedures. I don't relly understand how paramaters work, can someone please explain? Whenever I compile I get an type mismatch error. Heres the program:
program presidents (input, output);
uses
wincrt;
type
name=record
fname,mname,lname:string;
end;
arraytype=array [1..5] of name;
var
card:arraytype;
i,j:integer;
temp:name;
procedure ask (var ccard:arraytype;ii:integer );
begin
for ii:=1 to 5 do
begin
writeln('Type in the first name',ii);
readln(ccard[ii].fname);
writeln('Type in the middle name');
readln(ccard[ii].mname);
writeln('type in the last name');
readln(ccard[ii].lname);
clrscr;
end;
end;
procedure sort (var ttemp:name;ccard:arraytype ;ii,jj:int eger);
begin
for ii:= 1 to 5-1 do
begin
for jj:=1 to 5-1 do
begin
if (ccard[jj].lname) > (ccard[jj+1].lname) then
begin
ttemp:=ccard[jj];
ccard[jj]:=ccard[jj+1];
ccard[jj+1]:=ttemp;
end;
end;
end;
end;
procedure print (var ccard:arraytype;ii:integer );
begin
for ii:=1 to 5 do
begin
writeln(ii,'. ',ccard[i].fname,' ',ccard[i].mname,' ',ccard[i].lname); {do I define .lname as a paramater?}
end;
writeln('Brian Abbott');
end;
begin
ask (card,i);
sort (card,temp); {Error message is here}
print(card,i);
end.
This program is suppose to take 5 names, alphabetize them, and print them out. It uses records parameters and procedures. I don't relly understand how paramaters work, can someone please explain? Whenever I compile I get an type mismatch error. Heres the program:
program presidents (input, output);
uses
wincrt;
type
name=record
fname,mname,lname:string;
end;
arraytype=array [1..5] of name;
var
card:arraytype;
i,j:integer;
temp:name;
procedure ask (var ccard:arraytype;ii:integer
begin
for ii:=1 to 5 do
begin
writeln('Type in the first name',ii);
readln(ccard[ii].fname);
writeln('Type in the middle name');
readln(ccard[ii].mname);
writeln('type in the last name');
readln(ccard[ii].lname);
clrscr;
end;
end;
procedure sort (var ttemp:name;ccard:arraytype
begin
for ii:= 1 to 5-1 do
begin
for jj:=1 to 5-1 do
begin
if (ccard[jj].lname) > (ccard[jj+1].lname) then
begin
ttemp:=ccard[jj];
ccard[jj]:=ccard[jj+1];
ccard[jj+1]:=ttemp;
end;
end;
end;
end;
procedure print (var ccard:arraytype;ii:integer
begin
for ii:=1 to 5 do
begin
writeln(ii,'. ',ccard[i].fname,' ',ccard[i].mname,' ',ccard[i].lname); {do I define .lname as a paramater?}
end;
writeln('Brian Abbott');
end;
begin
ask (card,i);
sort (card,temp); {Error message is here}
print(card,i);
end.
and if ever this tells you something, Pascal records are just C "structs" (variant types included)
ASKER
I still get the type mismatch error. I'm not sure if I did exactly what you said. I also doubled up the three i's at the print line. heres the new program.
program presidents (input, output);
uses
wincrt;
type
name=record
fname,mname,lname:string;
end;
arraytype=array [1..5] of name;
var
card:arraytype;
i,j:integer;
temp:name;
procedure ask (var ccard:arraytype;ii:integer );
begin
for ii:=1 to 5 do
begin
writeln('Type in the first name',ii);
readln(ccard[ii].fname);
writeln('Type in the middle name');
readln(ccard[ii].mname);
writeln('type in the last name');
readln(ccard[ii].lname);
clrscr;
end;
end;
procedure sort (var ttemp:name;ccard:arraytype );
VAR ii,jj:integer;
begin
for ii:= 1 to 5-1 do
begin
for jj:=1 to 5-1 do
begin
if (ccard[jj].lname) > (ccard[jj+1].lname) then
begin
ttemp:=ccard[jj];
ccard[jj]:=ccard[jj+1];
ccard[jj+1]:=ttemp;
end;
end;
end;
end;
procedure print (var ccard:arraytype;ii:integer );
begin
for ii:=1 to 5 do
begin
writeln(ii,'. ',ccard[ii].fname,' ',ccard[ii].mname,' ',ccard[ii].lname);
end;
writeln('Brian Abbott');
end;
begin
ask (card,i);
sort (card,temp);{type mismatch error}
print(card,i);
end.
program presidents (input, output);
uses
wincrt;
type
name=record
fname,mname,lname:string;
end;
arraytype=array [1..5] of name;
var
card:arraytype;
i,j:integer;
temp:name;
procedure ask (var ccard:arraytype;ii:integer
begin
for ii:=1 to 5 do
begin
writeln('Type in the first name',ii);
readln(ccard[ii].fname);
writeln('Type in the middle name');
readln(ccard[ii].mname);
writeln('type in the last name');
readln(ccard[ii].lname);
clrscr;
end;
end;
procedure sort (var ttemp:name;ccard:arraytype
VAR ii,jj:integer;
begin
for ii:= 1 to 5-1 do
begin
for jj:=1 to 5-1 do
begin
if (ccard[jj].lname) > (ccard[jj+1].lname) then
begin
ttemp:=ccard[jj];
ccard[jj]:=ccard[jj+1];
ccard[jj+1]:=ttemp;
end;
end;
end;
end;
procedure print (var ccard:arraytype;ii:integer
begin
for ii:=1 to 5 do
begin
writeln(ii,'. ',ccard[ii].fname,' ',ccard[ii].mname,' ',ccard[ii].lname);
end;
writeln('Brian Abbott');
end;
begin
ask (card,i);
sort (card,temp);{type mismatch error}
print(card,i);
end.
of course
the call is sort(temp,card), not card,temp
look at the definition
the call is sort(temp,card), not card,temp
look at the definition
ASKER
Hey, I still getthe same error at the call.
program presidents (input, output);
uses
wincrt;
type
name=record
fname,mname,lname:string;
end;
arraytype=array [1..5] of name;
var
card:arraytype;
i,j:integer;
temp:name;
procedure ask (var ccard:arraytype;ii:integer );
begin
for ii:=1 to 5 do
begin
writeln('Type in the first name',ii);
readln(ccard[ii].fname);
writeln('Type in the middle name');
readln(ccard[ii].mname);
writeln('type in the last name');
readln(ccard[ii].lname);
clrscr;
end;
end;
procedure sort (var ttemp:name;ccard:arraytype );
Var ii,jj:integer;
begin
for ii:= 1 to 5-1 do
begin
for jj:=1 to 5-1 do
begin
if (ccard[jj].lname) > (ccard[jj+1].lname) then
begin
ttemp:=ccard[jj];
ccard[jj]:=ccard[jj+1];
ccard[jj+1]:=ttemp;
end;
end;
end;
end;
procedure print (var ccard:arraytype;ii:integer );
begin
for ii:=1 to 5 do
begin
writeln(ii,'. ',ccard[ii].fname,' ',ccard[ii].mname,' ',ccard[ii].lname);
end;
writeln('Brian Abbott');
end;
begin
ask (card,i);
sort (card,temp);
print(card,i);
end.
program presidents (input, output);
uses
wincrt;
type
name=record
fname,mname,lname:string;
end;
arraytype=array [1..5] of name;
var
card:arraytype;
i,j:integer;
temp:name;
procedure ask (var ccard:arraytype;ii:integer
begin
for ii:=1 to 5 do
begin
writeln('Type in the first name',ii);
readln(ccard[ii].fname);
writeln('Type in the middle name');
readln(ccard[ii].mname);
writeln('type in the last name');
readln(ccard[ii].lname);
clrscr;
end;
end;
procedure sort (var ttemp:name;ccard:arraytype
Var ii,jj:integer;
begin
for ii:= 1 to 5-1 do
begin
for jj:=1 to 5-1 do
begin
if (ccard[jj].lname) > (ccard[jj+1].lname) then
begin
ttemp:=ccard[jj];
ccard[jj]:=ccard[jj+1];
ccard[jj+1]:=ttemp;
end;
end;
end;
end;
procedure print (var ccard:arraytype;ii:integer
begin
for ii:=1 to 5 do
begin
writeln(ii,'. ',ccard[ii].fname,' ',ccard[ii].mname,' ',ccard[ii].lname);
end;
writeln('Brian Abbott');
end;
begin
ask (card,i);
sort (card,temp);
print(card,i);
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ok, the main program : last three lines.
Call ask(card,i), then sort(), then print().
i is an integer, temp is a record of type "name", and card is an array[1..5] of that "name" type (hence records)
ok until now ?
ask() is pretty straithforward, as is print()
Apparently you'll end up with a compilation error on the line calling Sort(card,temp) BECAUSE sort() is defined this way (signature, prototype, whatever) :
procedure sort (var ttemp:name;ccard:arraytype
so it REQUIRES two extra parameters of type integer.
Given the body of the sort() procedure, you mistyped the declaration of the procedure sort().
It should read :
procedure sort (var ttemp:name;ccard:arraytype
Var ii,jj:integer;
A part from this (your program will work seamlessly after this correction), I suggest you use the text editor to replace all "5" by "cMax" and define cMax after the "uses WinCrt;" line as :
Const cMax=5; // now easily modifiable for program's expansion.
PS : there are no procedure parameters in this program.
regards,