Arrays and Procedures

Hey All,

I'm working on my final project and need some H E L P...

Here is the assignment:

"You must properly pass parameters to all procedures and functions. The compiler will allow you to bypass this, but your solution will be wrong.

-Scores.txt (I've included the text file below) is a file that contains scores of students in a class for an exam. You are to read records from the file, assign a grade to each student and store the details in arrays. You are then to read data from the arrays and display a report.
-Each line of the file (Scores.txt-see below) contains the student's SSN (11 characters) and the exam score.
-USE A PROCEDURE to read data from the file.
-USE A FUNCTION to assign a grade for each student based on:
90-100=A, 80-89=B, 70-79=C, 60-69=D, 0-59=F. Store each student's SSN, exam score, and the grade into 3 separate arrays.
-USE A PROCEDURE to read from the arrays and displaya 3 column report with the following headings:
ID Score Grade
-You must use proper parameter passing."
--Here is the text file contents:

123-45-6789 88
567-32-5432 65
345-56-8901 76
782-45-4321 72
890-89-9000 90
321-45-7896 21
345-65-8976 95
345-56-8765 81

Here's my program that only displays 1 column of zeros. Can you guys help me figure out my problems?

[code]
Program Calculator {Input,Output};
Uses CRT;
{Declare Global Variables}
Type
IDArrayType = Array[1..8] of String[11];
ScoresArrayType = Array[1..8] of Integer;
GradeArrayType = Array[1..8] of Char;

Var
SSN1: IDArrayType;
Score1: ScoresArrayType;
FinalGrade: GradeArrayType;
G1,G2,G3,G4,G5,G6,G7,G8,J: Integer;
Grades1: Integer;
SSN1a: IDArrayType;
Score1a: ScoresArrayType;
FinalGradea: GradeArrayType;

Procedure InputSub(SSN: IDArrayType; Score: ScoresArrayType);
Var InFile: Text; InFileName: String; I: Integer;
Separator: Char;

{This procedure will access a text file for all input}

Begin
Assign(InFile,'A:Scores.txt');
Reset(Infile);
While Not EOF(InFile) Do
Begin

For I:=1 to 8 Do
Begin
Read(InFile,SSN[I]);
Read(Infile,Separator);
Read(InFile,Score[I]);
Readln(InFile);

End;
End
End;

Function Grades(Score2: ScoresArrayType; Grade: GradeArrayType): Integer;
Var I: Integer;
Begin
For I:=1 to 8 Do
Begin
If Score2[I] >90 Then
Grade[I]:='A'
Else If (Score2[I] >80) And (Score2[I]<89) Then
Grade[I]:='B'
End
End;

Procedure PrintSub(SS: IDArrayType; SC: ScoresArrayType; FG: GradeArrayType);
Var I: Integer;
Begin
Writeln('ID ','Score ','Grade');
For I:=1 to 8 Do
Begin
Writeln(SS[I],' ',SC[I]);
End
End;



{Main Program}
Begin
CLRSCR;
InputSub(SSN1,Score1);
For J:=1 to 8 Do
Begin
Grades1:=Grades(Score1,FinalGrade);
End;
PrintSub(SSN1a,Score1a,FinalGradea);
Readln;
End.
[/code]

Thanks,

Bondo

Comments

  • : Here's my program that only displays 1 column of zeros. Can you guys help me figure out my problems?

    There are only a few minor changes you need to make. I'll give you a couple hints and see if you can make the changes on your own.

    1) Normally when you pass a variable to a function you only pass the value. So when you make changes to it, they are local to that procedure only and the variable you passed in will not keep those changes. So anytime you pass a variable that you want to retain the changes, you need to change the declaration. For example:

    procedure Foo(AParam: Integer);
    Normal way, AParam is local

    procedure Foo(var AParam: Integer);
    Alternate way, changes made to AParam will be kept when Foo returns to the calling function.

    2) You have doubles of your arrays (SSN1/SSN1a, Score1/Score1a, FinalGrade1/FinalGradea). The duplicates are not necessary. You should kill those and work only with SSN1/Score1/FinalGrade1

    3) You define a lot of variables you dont use/need.

    4) You define a lot of global variables, but then use parameter passing. Usually the reason for global variables is so you dont need parameter passing, or the reason for parameter passing is to avoid global variables. (They are mutually exclusive).

    5) A couple of your procedures look to be unfinished, but I assume that is just because you want to get the other bugs out before finishing them up.

  • Try to omit paramters for procedure InputSub and test it again:

    [code]
    Procedure InputSub;
    Var InFile: Text; InFileName: String; I: Integer;
    Separator: Char;

    {This procedure will access a text file for all input}

    Begin
    Assign(InFile,'a:scores.txt');
    Reset(Infile);
    While Not EOF(InFile) Do
    Begin

    For I:=1 to 8 Do
    Begin
    Read(InFile,SSN1a[I]);
    Read(Infile,Separator);
    Read(InFile,Score1a[I]);
    Readln(InFile);

    End;
    End
    End;

    [/code]

    Iby
  • [b][red]This message was edited by bond007 at 2002-12-7 11:17:44[/red][/b][hr]
    Thanks for your help. I will work on it more and see what I can get done.

    Bondo
  • : Try to omit paramters for procedure InputSub and test it again:
    :
    : [code]
    : Procedure InputSub;
    : Var InFile: Text; InFileName: String; I: Integer;
    : Separator: Char;
    :
    : {This procedure will access a text file for all input}
    :
    : Begin
    : Assign(InFile,'a:scores.txt');
    : Reset(Infile);
    : While Not EOF(InFile) Do
    : Begin
    :
    : For I:=1 to 8 Do
    : Begin
    : Read(InFile,SSN1a[I]);
    : Read(Infile,Separator);
    : Read(InFile,Score1a[I]);
    : Readln(InFile);
    :
    : End;
    : End
    : End;
    :
    : [/code]
    :
    : Iby
    :

    I did as you instructed and it works except I cannot get the actual grade to print. The original posting of my program had an unfinished Function but still at least the first grade of 'B' should have shown up. Do you guys know what could be wrong there?

    Bondo
  • : I did as you instructed and it works except I cannot get the actual grade to print. The original posting of my program had an unfinished Function but still at least the first grade of 'B' should have shown up. Do you guys know what could be wrong there?
    :
    : Bondo
    :


    Same thing, just get rid of parameters and address your variables
    directly.


    Iby
  • : : I did as you instructed and it works except I cannot get the actual grade to print. The original posting of my program had an unfinished Function but still at least the first grade of 'B' should have shown up. Do you guys know what could be wrong there?
    : :
    : : Bondo
    : :
    :
    :
    : Same thing, just get rid of parameters and address your variables
    : directly.
    :
    :
    : Iby
    :
    Here is the finished program (All of it works). Thank you folks for the great help!!
    I do have some questions about my program.

    1. At the bottom of the program where the call to the Function is made, why or how does it work by having the 'J' variable in the call? I tried it as a last resort and then it worked. I just don't see how.

    2. According to the instructions from the assignment:
    "You must properly pass parameters to all procedures and functions. The compiler will allow you to bypass this, but your solution will be wrong..."
    Have I done this correctly in my program?

    The code:
    [code]Program Scores {Input,Output};
    Uses CRT;
    {Declare Global Variables}
    Type
    IDArrayType = Array[1..8] of String[11];
    ScoresArrayType = Array[1..8] of Integer;
    GradeArrayType = Array[1..8] of Char;

    Var
    SSN: IDArrayType;
    Score: ScoresArrayType;
    FinalGrade: GradeArrayType;
    J: Integer;
    A,B,C,D,F: Char;


    Procedure InputSub; Var InFile: Text; InFileName: String; I: Integer;
    Separator: Char;

    {This procedure will access a text file for all input and place the input into SSN and Score Arrays}

    Begin
    Assign(InFile,'A:Scores.txt');
    Reset(Infile);
    While Not EOF(InFile) Do
    Begin
    For I:=1 to 8 Do
    Begin
    Read(InFile,SSN[I]);
    Read(Infile,Separator);
    Read(InFile,Score[I]);
    Readln(InFile);

    End;
    End
    End;

    Function Grade(Var I: Integer): Char;
    {This Function will compare the Score array elements in order to assign the correct grade}

    Begin
    If Score[I] >=90 Then
    Begin
    A:='A';
    Grade:=A
    End
    Else If (Score[I] >=80) And (Score[I]<=89) Then
    Begin
    B:='B';
    Grade:=B;
    End
    Else If (Score[I] >=70) And (Score[I]<=79) Then
    Begin
    C:='C';
    Grade:=C
    End
    Else If (Score[I] >=60) And (Score[I]<=69) Then
    Begin
    D:='D';
    Grade:=D
    End
    Else If Score[I]<=59 Then
    Begin
    F:='F';
    Grade:=F
    End

    End;

    Procedure PrintSub; Var I: Integer;
    {This procedure will display the elements of the SSN, Score, and FinalGrade Arrays}
    Begin
    Writeln('ID ','Score ','Grade');
    Writeln;
    For I:=1 to 8 Do
    Begin
    Writeln(SSN[I],' ',Score[I],' ',FinalGrade[I]);
    End
    End;



    {Main Program}
    Begin
    CLRSCR;
    {Call to procedure: InputSub}
    InputSub;

    {This loop calls the Grade function 8 times and stores results in the FinalGrade array}
    For J:=1 to 8 Do
    Begin
    FinalGrade[J]:=Grade(J);
    End;

    {Call to procedure: PrintSub}
    PrintSub;
    Readln;
    End.[/code]
  • : Here is the finished program (All of it works). Thank you folks for the great help!!
    : I do have some questions about my program.
    :
    : 1. At the bottom of the program where the call to the Function is made, why or how does it work by having the 'J' variable in the call? I tried it as a last resort and then it worked. I just don't see how.

    In your PrintSub procedure you could replace FinalGrade[I] with Grade(I), and then you wouldnt need that loop in the main section.

    : 2. According to the instructions from the assignment:
    : "You must properly pass parameters to all procedures and functions. The compiler will allow you to bypass this, but your solution will be wrong..."

    : Have I done this correctly in my program?

    In my opinion, no. Of course there is a chance your teacher has some obscure definition of parameter passing in which case you're alright (I highly doubt that though, sorry).

  • : : 2. According to the instructions from the assignment:
    : : "You must properly pass parameters to all procedures and functions. The compiler will allow you to bypass this, but your solution will be wrong..."
    :
    : : Have I done this correctly in my program?
    :
    : In my opinion, no. Of course there is a chance your teacher has some obscure definition of parameter passing in which case you're alright (I highly doubt that though, sorry).
    :


    You can remove loops from procedures and functions and place them
    into main program. This way you would have to use parameter passing.

    Iby

  • :
    : : : 2. According to the instructions from the assignment:
    : : : "You must properly pass parameters to all procedures and functions. The compiler will allow you to bypass this, but your solution will be wrong..."
    : :
    : : : Have I done this correctly in my program?
    : :
    : : In my opinion, no. Of course there is a chance your teacher has some obscure definition of parameter passing in which case you're alright (I highly doubt that though, sorry).
    : :
    :
    :
    : You can remove loops from procedures and functions and place them
    : into main program. This way you would have to use parameter passing.

    His original program is actually pretty close to what he needs. In an older message I pointed out he should use the "var" keywork in the prodecure declarations so he is passing actual variables and not just their values, and that there were some redundant variables. After making those 2 changes it worked great here.
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