Languages :: Pascal :: Paramaters |
|||
| By: VB guy |
Date: 26/02/2003 00:00:00 |
Points: 50 | Status: Answered Quality : Excellent |
|
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: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.fname,' ',ccard.mname,' ',ccard.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. |
|||
| By: VGR | Date: 26/02/2003 02:34:00 | Type : Comment |
|
| well, given the way it's written, no surprise you don't understand it quickly :D 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;ii,jj:integer); 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, |
|||
| By: VGR | Date: 26/02/2003 02:37:00 | Type : Comment |
|
| and if ever this tells you something, Pascal records are just C "structs" (variant types included) |
|||
| By: VB guy | Date: 27/02/2003 06:10:00 | Type : Comment |
|
| 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. |
|||
| By: VGR | Date: 27/02/2003 06:18:00 | Type : Comment |
|
| of course the call is sort(temp,card), not card,temp look at the definition |
|||
| By: VB guy | Date: 28/02/2003 01:15:00 | Type : Comment |
|
| 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. |
|||
| By: VGR | Date: 28/02/2003 02:20:00 | Type : Answer |
|
| begin ask (card,i); sort (TEMP, CARD); print(card,i); end read me !!! TEMP, CARD, not CARD, TEMP |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








