Need some help in a record problem 
Author Message
 Need some help in a record problem

Hello,

I have a project whereby I have to get the data (in record, we just
learned them very recently) of some students, get the average
for each of them, find the max and the minimum of the averages, etc.
I have worked on the program, but I had to stop because I got stuck at
the point where I was to get the average. For each of the three
students I keep geting the same number, and for the life of me, I can't
figure out what I am doing wrong.
Could somebody go quickly over the code (must be pretty easy for the
most of you, as it is a plain beginner's code), and give me a small
hint on where did I go wrong, as I simply don't get it anymore...

Thanks a lot,
D.

program grades(input, output);

const studentnum = 3;
type
        studindex = 1..studnum;
        idrange = 1111..9999;
        student = record
                        weight1 : real;
                        weight2 : real;
                        weight3 : real;
                        score1 : integer;
                        score2 : integer;
                        score3 : integer;
                        id : idrange;
                        avg : real;
                        grade : char;
                   end;
        studentarray = array[studindex] of student;
var
        onestudent : studentarray;
        i : integer;

procedure readdata(var stud: student);
        begin
                with stud do
                 begin
                     read(weight1, weight2, weight3);
                     read(score1, score2, score3);
                     read(id);
                 end;
        end;

procedure findaverage(var average: real);
 var stud : student;
  begin
    with stud do
     begin
         average := (weight1*score1)+(weight2*score2)+(weight3*score3);
         stud.avg := average;
     writeln(average);
     end;
    end;

begin
    for i := 1 to studentnum do
      begin
        writeln('Please input data for student number ', i);
        readdata(onestudent[i]);
        findaverage(onestudent[i].avg);
        writeln(onestudent[i].avg: 2:1);
     end;

end.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 16 Apr 2003 02:15:21 GMT  
 Need some help in a record problem

Quote:
> Hello,

> I have a project whereby I have to get the data (in record, we just
> learned them very recently) of some students, get the average
> for each of them, find the max and the minimum of the averages, etc.
> I have worked on the program, but I had to stop because I got stuck at
> the point where I was to get the average. For each of the three
> students I keep geting the same number, and for the life of me, I can't
> figure out what I am doing wrong.
> Could somebody go quickly over the code (must be pretty easy for the
> most of you, as it is a plain beginner's code), and give me a small
> hint on where did I go wrong, as I simply don't get it anymore...

> Thanks a lot,
> D.

...

Quote:
> procedure findaverage(var average: real);
>  var stud : student;
>   begin
>     with stud do
>      begin
>          average := (weight1*score1)+(weight2*score2)+(weight3*score3);
>          stud.avg := average;
>      writeln(average);
>      end;
>     end;

the are some errors in the code:

i think what you wanted is somthing like this:

procedure findaverage(var STUD:STUDENT);
  begin
    with stud do
     begin
         avg := (weight1*score1)+(weight2*score2)+(weight3*score3);
         writeln(avg);
     end;
    end;

your var stud in the procedure was not initialized. you might have got ANY result.

hth
Jochen



Wed, 16 Apr 2003 05:17:22 GMT  
 Need some help in a record problem
The problem is in the findaverage procedure. You're not initializing the
local variable 'stud'. Try this:

  procedure findaverage(VAR stud:student);
  begin
       with stud do
         avg:=(weight1*score1)+(weight2*score2)+(weight3*score3);
  end;

and the call should be

  findaverage(onestudent[i]);
  writeln('average is ',onestudent[i].avg:3:1);

Kingpin.

Quote:

> Hello,

> I have a project whereby I have to get the data (in record, we just
> learned them very recently) of some students, get the average
> for each of them, find the max and the minimum of the averages, etc.
> I have worked on the program, but I had to stop because I got stuck at
> the point where I was to get the average. For each of the three
> students I keep geting the same number, and for the life of me, I can't
> figure out what I am doing wrong.
> Could somebody go quickly over the code (must be pretty easy for the
> most of you, as it is a plain beginner's code), and give me a small
> hint on where did I go wrong, as I simply don't get it anymore...

> Thanks a lot,
> D.

> program grades(input, output);

> const studentnum = 3;
> type
>         studindex = 1..studnum;
>         idrange = 1111..9999;
>         student = record
>                         weight1 : real;
>                         weight2 : real;
>                         weight3 : real;
>                         score1 : integer;
>                         score2 : integer;
>                         score3 : integer;
>                         id : idrange;
>                         avg : real;
>                         grade : char;
>                    end;
>         studentarray = array[studindex] of student;
> var
>         onestudent : studentarray;
>         i : integer;

> procedure readdata(var stud: student);
>         begin
>                 with stud do
>                  begin
>                      read(weight1, weight2, weight3);
>                      read(score1, score2, score3);
>                      read(id);
>                  end;
>         end;

> procedure findaverage(var average: real);
>  var stud : student;
>   begin
>     with stud do
>      begin
>          average := (weight1*score1)+(weight2*score2)+(weight3*score3);
>          stud.avg := average;
>      writeln(average);
>      end;
>     end;

> begin
>     for i := 1 to studentnum do
>       begin
>         writeln('Please input data for student number ', i);
>         readdata(onestudent[i]);
>         findaverage(onestudent[i].avg);
>         writeln(onestudent[i].avg: 2:1);
>      end;

> end.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Wed, 16 Apr 2003 05:19:35 GMT  
 Need some help in a record problem
Thanks Jochen and Kingpin, you are both right, it worked when I
initialised the correct variable. It now all works fine, except for one
part, which, well, I made it to work, using a workaround, but I know it
is not the proper solution.
Basically after getting the average, I should also set the scores for
each of them. When the average is declared as a real, it has to be
transformed into an integer to work properly with the grades.
What I did was modifying the average procedure and changed the average
to be integer. The program works, it shows me the correct grades as
well, but I know the prof. wants to see the averages as real.
Here is what I have done:

procedure findaverage(var stud: student);
begin
    with stud do
         avg := round((weight1*score1)+(weight2*score2)+
(weight3*score3));
     end;

I have also written a function to convert the real to integer, which is:

function convtoint(avg:real):integer;
(* converts a real grade to integer *)
        begin
                convtoint := round(avg);
        end;

But then I am totally lost at putting the pieces together, in the main
body. That's why I made the 'workaround' to use integers from start
rather then real.
Can anybody help me, again, with this part? And please, bear with me, I
really try to learn it hard, but it takes time, I guess...

Thanks again,
D.



Quote:
> The problem is in the findaverage procedure. You're not initializing
the
> local variable 'stud'. Try this:

>   procedure findaverage(VAR stud:student);
>   begin
>        with stud do
>          avg:=(weight1*score1)+(weight2*score2)+(weight3*score3);
>   end;

> and the call should be

>   findaverage(onestudent[i]);
>   writeln('average is ',onestudent[i].avg:3:1);

> Kingpin.


> > Hello,

> > I have a project whereby I have to get the data (in record, we just
> > learned them very recently) of some students, get the average
> > for each of them, find the max and the minimum of the averages, etc.
> > I have worked on the program, but I had to stop because I got stuck
at
> > the point where I was to get the average. For each of the three
> > students I keep geting the same number, and for the life of me, I
can't
> > figure out what I am doing wrong.
> > Could somebody go quickly over the code (must be pretty easy for the
> > most of you, as it is a plain beginner's code), and give me a small
> > hint on where did I go wrong, as I simply don't get it anymore...

> > Thanks a lot,
> > D.

> > program grades(input, output);

> > const studentnum = 3;
> > type
> >         studindex = 1..studnum;
> >         idrange = 1111..9999;
> >         student = record
> >                         weight1 : real;
> >                         weight2 : real;
> >                         weight3 : real;
> >                         score1 : integer;
> >                         score2 : integer;
> >                         score3 : integer;
> >                         id : idrange;
> >                         avg : real;
> >                         grade : char;
> >                    end;
> >         studentarray = array[studindex] of student;
> > var
> >         onestudent : studentarray;
> >         i : integer;

> > procedure readdata(var stud: student);
> >         begin
> >                 with stud do
> >                  begin
> >                      read(weight1, weight2, weight3);
> >                      read(score1, score2, score3);
> >                      read(id);
> >                  end;
> >         end;

> > procedure findaverage(var average: real);
> >  var stud : student;
> >   begin
> >     with stud do
> >      begin
> >          average := (weight1*score1)+(weight2*score2)+
(weight3*score3);
> >          stud.avg := average;
> >      writeln(average);
> >      end;
> >     end;

> > begin
> >     for i := 1 to studentnum do
> >       begin
> >         writeln('Please input data for student number ', i);
> >         readdata(onestudent[i]);
> >         findaverage(onestudent[i].avg);
> >         writeln(onestudent[i].avg: 2:1);
> >      end;

> > end.

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 19 Apr 2003 03:53:07 GMT  
 Need some help in a record problem
Thanks Jochen and Kingpin, you are both right, it worked when I
initialised the correct variable. It now all works fine, except for one
part, which, well, I made it to work, using a workaround, but I know it
is not the proper solution.
Basically after getting the average, I should also set the scores for
each of them. When the average is declared as a real, it has to be
transformed into an integer to work properly with the grades.
What I did was modifying the average procedure and changed the average
to be integer. The program works, it shows me the correct grades as
well, but I know the prof. wants to see the averages as real.
Here is what I have done:

procedure findaverage(var stud: student);
begin
    with stud do
         avg := round((weight1*score1)+(weight2*score2)+
(weight3*score3));
     end;

I have also written a function to convert the real to integer, which is:

function convtoint(avg:real):integer;
(* converts a real grade to integer *)
        begin
                convtoint := round(avg);
        end;

But then I am totally lost at putting the pieces together, in the main
body. That's why I made the 'workaround' to use integers from start
rather then real.
Can anybody help me, again, with this part? And please, bear with me, I
really try to learn it hard, but it takes time, I guess...

Thanks again,
D.



Quote:
> The problem is in the findaverage procedure. You're not initializing
the
> local variable 'stud'. Try this:

>   procedure findaverage(VAR stud:student);
>   begin
>        with stud do
>          avg:=(weight1*score1)+(weight2*score2)+(weight3*score3);
>   end;

> and the call should be

>   findaverage(onestudent[i]);
>   writeln('average is ',onestudent[i].avg:3:1);

> Kingpin.


> > Hello,

> > I have a project whereby I have to get the data (in record, we just
> > learned them very recently) of some students, get the average
> > for each of them, find the max and the minimum of the averages, etc.
> > I have worked on the program, but I had to stop because I got stuck
at
> > the point where I was to get the average. For each of the three
> > students I keep geting the same number, and for the life of me, I
can't
> > figure out what I am doing wrong.
> > Could somebody go quickly over the code (must be pretty easy for the
> > most of you, as it is a plain beginner's code), and give me a small
> > hint on where did I go wrong, as I simply don't get it anymore...

> > Thanks a lot,
> > D.

> > program grades(input, output);

> > const studentnum = 3;
> > type
> >         studindex = 1..studnum;
> >         idrange = 1111..9999;
> >         student = record
> >                         weight1 : real;
> >                         weight2 : real;
> >                         weight3 : real;
> >                         score1 : integer;
> >                         score2 : integer;
> >                         score3 : integer;
> >                         id : idrange;
> >                         avg : real;
> >                         grade : char;
> >                    end;
> >         studentarray = array[studindex] of student;
> > var
> >         onestudent : studentarray;
> >         i : integer;

> > procedure readdata(var stud: student);
> >         begin
> >                 with stud do
> >                  begin
> >                      read(weight1, weight2, weight3);
> >                      read(score1, score2, score3);
> >                      read(id);
> >                  end;
> >         end;

> > procedure findaverage(var average: real);
> >  var stud : student;
> >   begin
> >     with stud do
> >      begin
> >          average := (weight1*score1)+(weight2*score2)+
(weight3*score3);
> >          stud.avg := average;
> >      writeln(average);
> >      end;
> >     end;

> > begin
> >     for i := 1 to studentnum do
> >       begin
> >         writeln('Please input data for student number ', i);
> >         readdata(onestudent[i]);
> >         findaverage(onestudent[i].avg);
> >         writeln(onestudent[i].avg: 2:1);
> >      end;

> > end.

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 19 Apr 2003 03:54:41 GMT  
 Need some help in a record problem

Quote:
> When the average is declared as a real, it has to be
> transformed into an integer to work properly with the grades.

Please elaborate on 'work properly'.

Kingpin.



Sat, 19 Apr 2003 04:23:09 GMT  
 Need some help in a record problem

Quote:

> Basically after getting the average, I should also set the scores for
> each of them. When the average is declared as a real, it has to be
> transformed into an integer to work properly with the grades.
> What I did was modifying the average procedure and changed the average
> to be integer. The program works, it shows me the correct grades as
> well, but I know the prof. wants to see the averages as real.

[snip]

Quote:
> function convtoint(avg:real):integer;
> (* converts a real grade to integer *)
>    begin
>            convtoint := round(avg);
>    end;

This function is pointless. Whenever you use convtoint(avg), all you are
doing is using round(avg); so why not just use round(avg) directly?

Anyway, I assume you have some code that takes the (integer) average and
turns it into a grade. If you want this to work with a real average, all you
have to do is replace your references to avg in that section of code by
round(avg).

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will

______________________________|_______________________________________



Sat, 19 Apr 2003 09:18:55 GMT  
 Need some help in a record problem
What I mean is, the avg is real, and the procedure where I assign the
grades according to the average asks continuously for ordinal numbers,
it simply doesn't work with a real number. It is looking for integer
numbers.

P.S. Now I see that my prev. posting appeared two times. So appologies,
I sent it only once, but the deja.com server must have mixed it up and
sent it two times.

Thanks again,
D.



Quote:
> > When the average is declared as a real, it has to be
> > transformed into an integer to work properly with the grades.

> Please elaborate on 'work properly'.

> Kingpin.

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 19 Apr 2003 16:25:15 GMT  
 Need some help in a record problem
Wow, why didn't I think of that!
It's so simple, I wonder why do I always tend to overcomplicate
things...

Thanks for your help, ouff, I wish I knew enough programming to realise
at least so simple things like these...



Quote:

> > Basically after getting the average, I should also set the scores
for
> > each of them. When the average is declared as a real, it has to be
> > transformed into an integer to work properly with the grades.
> > What I did was modifying the average procedure and changed the
average
> > to be integer. The program works, it shows me the correct grades as
> > well, but I know the prof. wants to see the averages as real.

> [snip]

> > function convtoint(avg:real):integer;
> > (* converts a real grade to integer *)
> >       begin
> >               convtoint := round(avg);
> >       end;

> This function is pointless. Whenever you use convtoint(avg), all you
are
> doing is using round(avg); so why not just use round(avg) directly?

> Anyway, I assume you have some code that takes the (integer) average
and
> turns it into a grade. If you want this to work with a real average,
all you
> have to do is replace your references to avg in that section of code
by
> round(avg).

> --
> ______________________________________________________________________
>      The Scarlet Manuka,      |        Nitpickers' Party motto:
>   Pratchett Quoter At Large,  |  "He who guards his lips guards his
>  First Prophet of Bonni, is:  |  soul, but he who speaks rashly will

> ______________________________|_______________________________________

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 19 Apr 2003 16:32:51 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. help , need to randomly select a record ......

2. Need help with BDE and commiting records

3. Need Help, reading records

4. Help needed - deleting a record

5. Need help saving a record with arrays.

6. need helping sorting arrays of records

7. Need help searching for text in a DBMemo through multiple records

8. Help needed - trouble inserting new record

9. Need help on "Record/Key deleted"

10. Copy/New Record creation help needed...

11. Grid and Record Locked Error - Need Help.

12. Problem posting record after editing, Please help.

 

 
Powered by phpBB® Forum Software