Repeating loops

2»

Comments

  • : The names listed in red [b]must[/b] be identical for the compiler to
    : understand them. For us GamesLost and Loss are similar, because we
    : know the meaning of those words. Computers must be taught that
    : meaning in every program. This is done using a declaration, like the
    : blue part. That part teaches the computer the meaning of a large
    : number of words. After that part the words:
    : [code]:
    : Team[2].GamesLost
    : [/code]:
    : have a certain meaning for the computer (it is a number between 0
    : and 255).
    :
    : Once you realize this, the solution is simple: Change [b]all[/b] the
    : words loss into GamesLost.

    Ok, thanks for the help. One problem thought now. When I try running it, the variable section of my procedure says identifier expected. I thought that procedures could have a variable section. I tried moving the variable section for the procedure above the procedure and then it highlighted the word "begin" within the procedure and said the same error. So how would I go about fixing this error?
  • : : The names listed in red [b]must[/b] be identical for the compiler to
    : : understand them. For us GamesLost and Loss are similar, because we
    : : know the meaning of those words. Computers must be taught that
    : : meaning in every program. This is done using a declaration, like the
    : : blue part. That part teaches the computer the meaning of a large
    : : number of words. After that part the words:
    : : [code]: :
    : : Team[2].GamesLost
    : : [/code]: :
    : : have a certain meaning for the computer (it is a number between 0
    : : and 255).
    : :
    : : Once you realize this, the solution is simple: Change [b]all[/b] the
    : : words loss into GamesLost.
    :
    : Ok, thanks for the help. One problem thought now. When I try running
    : it, the variable section of my procedure says identifier expected. I
    : thought that procedures could have a variable section. I tried
    : moving the variable section for the procedure above the procedure
    : and then it highlighted the word "begin" within the procedure and
    : said the same error. So how would I go about fixing this error?
    :
    Procedures can have a variable section, but it is not required. If you include the var keyword in a procedure, you [b]must[/b] provide at least one variable. The following is wrong:
    [code]
    procedure Wrong;
    var
    { Compiler expects at least 1 variable }
    begin
    end;
    [/code]
    The solution is quite simple:
    1 Remove the var keyword, if you don't need local variables
    2 add at least 1 variable
    [code]
    procedure Correct1;
    begin
    end;

    procedure Correct2;
    var
    example: integer;
    begin
    end;
    [/code]
    This also holds true for the [b]uses[/b] and [b]type[/b] keywords.
  • [code]
    : program Assignment4;
    : {Repeats itself}
    :
    : uses wincrt;
    :
    : TYPE TeamRecord = RECORD
    : Name : String[15];
    : GamesWon : Byte;
    : GamesLost : Byte;
    : GamesTied : Byte;
    : END;
    :
    : VAR Team : Array[1..5] OF TeamRecord;
    : var list : array [1..5] of TeamRecord;
    :
    : procedure sort(var list:TeamRecord; var newlist:TeamRecord;length:integer);
    : type
    : TeamRecord=record
    : loss:integer;
    :
    : var
    : a,b,index,nlength:integer;
    : position:boolean;
    :
    : [blue]begin[/blue]
    : nlength:=0;
    : for a:=1 to length do
    : [blue]begin[/blue]
    : index:=1;
    : position:=false;
    : while not position and (index <= nlength) do
    : if Team[a].loss < newlist[index].loss then
    : position:=true
    : else
    : index:=index+1;
    : for b:=nlength downto index do
    : newlist[b+1]:=newlist[{}b];
    : newlist[index]:=list[a];
    : nlength:=nlength+1
    : [blue]end[/blue]
    : [blue]end;[/blue]
    : [red]FOR WinCounter := 1 TO 5 DO [/red][blue]BEGIN[/blue]
    : Write('Enter team name: ');
    : Readln(Team[X].Name);
    : Write('Enter games won: ');
    : Readln(Team[X].GamesWon);
    : Write('Enter games tied: ');
    : Readln(Team[X].GamesTied);
    :
    : sort(list,newlist,len);
    :
    : writeln;
    : writeln('The teams in order of number of games lost are: ');
    : for x:=1 to 5 do
    : writeln(newlist[x].team:10,won[Wincounter]:3,newlist[x].loss:3,ties[Tiecounter]:3);
    : [blue]end.[/blue]
    [/code]

    Count your BEGIN...END statements. Although they do match in numbers, you have a [red]section (red area above)[/red] that is not inside anything!

    Note the following info:
    [red]-You must always have a main BEGIN & END. (with a period)
    -You may also have any other number of BEGIN & END; (semicolon)
    -You cannot have any code outside of a BEGIN...END
    -All variables MUST be declared outside of a BEGIN...END section[/red]

    If you use proper indentation you will easily be able to see that you have code outside of a BEGIN...END

    Note proper indentation:
    [code]
    program Assignment4;
    {Repeats itself}

    uses wincrt;

    TYPE TeamRecord = RECORD
    Name : String[15];
    GamesWon : Byte;
    GamesLost : Byte;
    GamesTied : Byte;
    END;

    VAR Team : Array[1..5] OF TeamRecord;
    var list : array [1..5] of TeamRecord;

    procedure sort(var list:TeamRecord; var newlist:TeamRecord;length:integer);
    type
    TeamRecord=record
    loss:integer;

    var
    a,b,index,nlength:integer;
    position:boolean;

    [blue]begin[/blue] { Start of Procedure Code }
    nlength:=0;
    for a:=1 to length do
    [blue]begin[/blue] { Start of Loop }
    index:=1;
    position:=false;
    while not position and (index <= nlength) do
    if Team[a].loss < newlist[index].loss then
    position:=true
    else
    index:=index+1;
    for b:=nlength downto index do
    newlist[b+1]:=newlist[{}b];
    newlist[index]:=list[a];
    nlength:=nlength+1
    [blue]end[/blue] { End of Loop }
    [blue]end;[/blue] { End of Procedure Code }

    [red]FOR WinCounter := 1 TO 5 DO [/red] { code outside of BEGIN...END }
    [blue]BEGIN[/blue] { Start of Main Section }
    Write('Enter team name: ');
    Readln(Team[X].Name);
    Write('Enter games won: ');
    Readln(Team[X].GamesWon);
    Write('Enter games tied: ');
    Readln(Team[X].GamesTied);

    sort(list,newlist,len);

    writeln;
    writeln('The teams in order of number of games lost are: ');
    for x:=1 to 5 do
    writeln(newlist[x].team:10,won[Wincounter]:3,newlist[x].loss:3,ties[Tiecounter]:3);
    [blue]end.[/blue] { End of Main Section }
    [/code]

    This should make it easier to read and follow. Don't forget to make comments throughout your code.

    Phat Nat
  • : Procedures can have a variable section, but it is not required. If
    : you include the var keyword in a procedure, you [b]must[/b] provide
    : at least one variable. The following is wrong:
    : [code]:
    : procedure Wrong;
    : var
    : { Compiler expects at least 1 variable }
    : begin
    : end;
    : [/code]:
    : The solution is quite simple:
    : 1 Remove the var keyword, if you don't need local variables
    : 2 add at least 1 variable
    : [code]:
    : procedure Correct1;
    : begin
    : end;
    :
    : procedure Correct2;
    : var
    : example: integer;
    : begin
    : end;
    : [/code]:
    : This also holds true for the [b]uses[/b] and [b]type[/b] keywords.

    Ok, I see what you are getting at, but I thought my procedure had variables within it.
    [code]
    var [color=Red]{Error:Identifier Expected}[/color]
    a,b,index,nlength:integer;
    position:boolean;
    [/code] It gives me the error, which is highlighted in red.

    And thanks for the advice Phat Nat, I will try my best to indent properly from now on.
  • : : Procedures can have a variable section, but it is not required. If
    : : you include the var keyword in a procedure, you [b]must[/b] provide
    : : at least one variable. The following is wrong:
    : : [code]: :
    : : procedure Wrong;
    : : var
    : : { Compiler expects at least 1 variable }
    : : begin
    : : end;
    : : [/code]: :
    : : The solution is quite simple:
    : : 1 Remove the var keyword, if you don't need local variables
    : : 2 add at least 1 variable
    : : [code]: :
    : : procedure Correct1;
    : : begin
    : : end;
    : :
    : : procedure Correct2;
    : : var
    : : example: integer;
    : : begin
    : : end;
    : : [/code]: :
    : : This also holds true for the [b]uses[/b] and [b]type[/b] keywords.
    :
    : Ok, I see what you are getting at, but I thought my procedure had
    : variables within it.
    : [code]:
    : var [color=Red]{Error:Identifier Expected}[/color]
    : a,b,index,nlength:integer;
    : position:boolean;
    : [/code]: It gives me the error, which is highlighted in red.
    :
    : And thanks for the advice Phat Nat, I will try my best to indent
    : properly from now on.
    :
    Are there any unclosed comments? For example:
    [code]
    var {Some comment
    a,b,index,nlength:integer;
    position:boolean; { other comment }
    begin
    end;
    [/code]
    If you're using an PAscal editor, which can hightlight code using various colors (such as TP6+), try using those colors for different code parts. I myself always have comments in a different color than the code itself. This shows you unclosed comments.
    When you indent your code, use 2 spaces instead of tabs. This shows the indentation clearly, and still leaves a lot of space for the actual code without horizontal scrolling.
  • Ok, I figured it all out. I decided to try and completely rewrite the program and then changed around some things and that seemed to work. For those who are interested, my code is:
    [code]
    program Assignment4;
    {Cole Gauthier 04/07/08}

    uses wincrt;

    TYPE TeamRecord = RECORD
    Name : String[15];
    GamesWon : integer;
    GamesLost : integer;
    GamesTied : integer
    END;
    Team = Array[1..5] OF TeamRecord;

    var
    list,newlist:team;
    len,x:integer;
    med:real;

    procedure sort(var list:Team; var newlist:Team;length:integer);

    var
    a,b,index,nlength:integer;
    position:boolean;

    begin
    nlength:=0;
    for a:=1 to length do
    begin
    index:=1;
    position:=false;
    while not position and (index <= nlength) do
    if list[a].GamesLost < newlist[index].GamesLost then
    position:=true
    else
    index:=index+1;
    for b:=nlength downto index do
    newlist[b+1]:=newlist[{}b];
    newlist[index]:=list[a];
    nlength:=nlength+1
    end
    end;

    BEGIN
    FOR x := 1 TO 5 DO
    begin
    Write('Enter team name: ');
    Readln(list[X].Name);
    Write('Enter games won: ');
    Readln(list[x].GamesWon);
    write('Enter games lost: ');
    readln(list[x].GamesLost);
    Write('Enter games tied: ');
    Readln(list[x].GamesTied);
    end;

    sort(list,newlist,x);

    writeln;
    writeln('The teams in order of number of games lost are: ');
    write('Name':10);
    write('Won':3);
    write('Lost':3);
    writeln('Tied':3);
    for x:=1 to 5 do
    writeln(newlist[x].name:10,newlist[x].GamesWon:3,newlist[x].GamesLost:3,newlist[x].GamesTied:3);
    end.
    [/code]
    Sorry that it's not properly indented. My next program will be for sure.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories