syuan (syuan) wrote in computerscience,
syuan
syuan
computerscience

  • Mood:

Help!!!

Hi, I'm new to the community. I study Graphics Design (this is my first year) but we started with Computer Science in general. I have no idea about programming and I don't get pascal at all... Can anyone help me with writing a program?

I'm supposed to create a program using Delphi7 (=> console type). There's a .txt file with a list of 4 students, the number of their grades and the list of the grades. The program should count the average of the grades for each student and then sort the names by the average, lowest to highest.


Kowalski Andrzej
4
3 3.5 4 3.5

Nowak Siergiej
5
5 3 4.5 4 2.5

Elfryda Ciupaga
4
4 4 3 4.5

Gertruda Cebula
3
5 4 3.5


I tried to do it myself but I'm a poor programmer. Here's what I came up, but most of it is probably wrong.



program Project2;

{$APPTYPE CONSOLE}

uses
SysUtils;

const max = 20;

type

student = record end;
ave = real;
studentlist = array [1..max] of student;

var
f: text;
no, i : integer;
sum : real;
name : string;
u : studentlist;
N : 4;

begin
assign(f, 'data.txt');
reset(f);

for 1..N do
u[i];

readln (f; u) name;
readln(f, no);
sum : 0;

for i:= 1 to no do

begin
read(f, grade);
sum:= sum + grade;
end;

u.ave := sum/no;
{ TODO -oUser -cConsole Main : Insert code here }
end.


If somebody can help me I'd be really grateful. I promise to study more on the pascal in the future.
  • Post a new comment

    Error

    default userpic
  • 8 comments
Most of us don't do Pascal much anymore (I do like the language, but there isn't much market for it), and won't be able to just look at someone else's program and find everything that's wrong with no hints.

Does it compile? If not, what is the error text?
After that, what happens when you run it?
I wrote the procedures according to the book, but every time Delphi finds errors. Most of the time it's the wrong sign. When I write a semicolon, it expects something entirely different. How do I know which one is right? Besides, I'm afraid I mixed up the most of the procedures and loops.

When I hit 'run' button, the line with 'N : 4;' is highlighted. The error text says that a '..' was expected, but a semicolon was found. We did something similar during classes, and everything was fine. Now, it seems it's not fine anymore.

I'm still working on the prog. Maybe I'll be able to figure it all out by myself.
"N : 4;" is not a variable declaration, but "N : 4..10;" would be. Pascal has a unique feature called subranges, where a variable can be defined with a restricted range of acceptable values, and the compiler thinks you're trying to do that. I think you're looking for this:

const
N = 4;
I think I'm starting to get it... 'N' describes the order numbers for students, so I could declare it ike this:

N : 1..4; //or should there be '0' instead of '1'?

or like this:

N : 1..max; // for 'max' as a constant --> {const max = 4;}


That would make sense, and Pascal does have a similar feature in sets (not that I remember how to use it), but that's not what I meant. You're just using N as a constant to record how many records there are. You want a const section with "N = 4;" in it, and then the later "for 1..N do".

Now that I look at that part, any of the basic control-of-flow constructs apply to the next thing in the file -- either a statement or a block. You have several lines to read for each record, so you'd want to create a block with:

for 1..N do
Begin
{Read the name.
Read the number of grades.
Read the individual grades.
Skip the blank line.}
End;
I don't know Pascal, but I'll try to make a few suggestions anyway.

1. Comment out lines until it compiles, and slowly add them back in.
2. When posting code, write it in rich text mode to preserve spacing. Proper spacing improves readability. Among programmers there is a definite sense of good design.  Using the pre html tag will work too.
3. I don't know Delphi, but the student struct is probably declared with the variables it has. Here is an example from Wikipedia (from the Pascal entry):

b = record
      a: integer;
      b: char;
      c: a
end;

So, your student should be something like

student = record
            name: string;
            ave: real
end;

Hope this helps.


Or you can just use my handy text-to-html converter. :)

http://www.lightning-rose.com/projects/txt2html.htm

Yeah, that too. :D Thanks.