Hi, im new to this forum, and searched for everything to do with 2D array, but found nothing to no real avail.

My problem is that i have to write a 2D array in pascal that stores 15 student ID's and 5 marks for each student. I've managed to write out about 60% of it, but it fails to work as i think i've instructed it.

Heres the code:

program array2d;
uses crt;
var
   Marks         : array[1..5,1..15] of integer;
   Student_Id     : integer;
   Student_Mark  : integer;

Procedure Input;
begin
   for Student_Id := 1 to 15 do
      begin
        for Student_Mark := 1 to 5 do
           begin
              write('Enter Student Mark ');
              readln(Marks[Student_Id,Student_Mark]);
           end;
      end;
end;

Procedure Output;
begin
   for Student_Id := 1 to 15 do
      begin
         for Student_Mark := 1 to 5 do
            begin
               write(Marks[Student_Id,Student_Mark],'  ');
            end;
        writeln;
      end;
end;

begin
   clrscr;
   Input;
   Writeln;
   Output;
   writeln;
   readln;
end.

Problem being is that it asks for 75 marks for each student but not in any particular order, it doesn't actually ask for the student ID's. I know this is missing from the code, but I'm stuck as to what to enter. It all has to be in "Incredibly Basic" tags as I'm a first year student. This confuses me so much, Please Help!!! it would be greatfuly appreciated!

p.s. sorry for asking a silly question :P

Recommended Answers

All 6 Replies

Dear Detoxx

I don't think that your question is a silly question. this problem is a natural problem that all programmers may have but the expert programmers can solve problem by reviewing program code carefully.


you have a logical error in your program that I try to explain it for you
In your code you define an array that have 5 item in first dimension and 15 items in second dimension " Marks : array[1..5,1..15] of integer;"
but input procedure and output procedure are working on 15 items of first dimension and 5 items of second dimension
12. for Student_Id := 1 to 15 do
13. begin
14. for Student_Mark := 1 to 5 do
15. begin
16. write('Enter Student Mark ');
17. readln(Marks[Student_Id,Student_Mark]);
18. end;
19. end;

at run time Student_Id will evaluated by 1,2,3,4,5,6,...15 but first dimension of "Marks" only contains 5 Items this will raise a "Subscribe Exception Error" that cause non logical behaviour of your program.

I hope this is help full for you.
Fayyaz

thank you, it has helped quite alot.

my only problem is, now i have to input code so it asks for student ID's. would i have to enter something like

write('Enter Student Mark ');
readln(Marks[Student_Id,Student_Mark]);

but before that loop put

write('Enter Student ID')
readln(Marks[Student_mark,Student_Id]);

like swap the student_mark and student_id around in the square brackets for it to store under a ID instead of marks?

Dear Detoxx

I'm Sorry because I don't know want do you want do do

As I understand you want to read a student ID and then read 5 related Marks. for this purpose you can change your input procedure as flow

[LIST=1]
[*]Procedure Input;
[*]Var
[*]    Counter :Integer;
[*]begin
[*]   for Counter := 1 to 15 do
[*]      begin
[*]        write('Enter Student ID: ');
[*]        readln(Student_Id);
[*]        for Student_Mark := 1 to 5 do
[*]           begin
[*]              write('Enter Student Mark ');
[*]              readln(Marks[Student_Id,Student_Mark]);
[*]           end;
[*]      end;
[*]end;
[/LIST]

well what you had entered in the last post has helped me so much :D

thank you very much!! i shall pass this year now thanks to you :P HND here i come!! <3

well what you had entered in the last post has helped me so much :D

thank you very much!! i shall pass this year now thanks to you :P HND here i come!! <3

try the following

var
counter: integer;
student_id: array[1..70] of integer;
student_mark:integer;

Begin
student[counter]:=[1..70];
//for counter:= 1 to 70 do
//showmessage[student_mark][student_id];
//display this on memo1
memo1.lines.add(student_id[counter]);

End;
End.


just try it for one button

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.