(* Title: 	StdArr
	Author: Yamama Khadduri
	Revised: 09-OCT-2002
	Description: Adds new students to the array and keeps track of their attendance to class.
	
	Interface:
	INTERFACE StdArr;
		PROCEDURE NewStd (StdID:INTEGER);
		PROCEDURE Attendance (StdID:INTEGER):INTEGER;
	END StdRec.
*)

MODULE StdArr;

IMPORT Out;

	(* TYPE *)
	TYPE
		StudentRec = RECORD;	(*Creates a record of type StudentRec*)
			StdID: INTEGER;	(*Creates INTEGER and INTEGER fields in the record*)
			Presence: INTEGER;
			Status:=0 : INTEGER; (*initializing the status when creating*)
		END;
	
	CONST SIZE = 100;
	VAR Student: ARRAY SIZE OF StudentRec;	(*Creating an array of size 100 named Students and of type Records*)
	
(******************* NewStd ********************)
	PROCEDURE NewStd*(StdID:INTEGER);
		VAR i:INTEGER;
	BEGIN
		i:=0;	(*initializing the counter*)
		(*searching for a place in the array to store the new student*)
		WHILE (Student[i].Status = 1) & (Student[i].StdID = StdID) DO 
			i:=i+1;
		END;
		(*Storing the data in the place found*)
		Student[i].StdID:=StdID;
		Student[i].Presence:= 1;
		Student[i].Status:=1;
	END NewStd;
	
(****************** Attendance *******************)
(*Increments the presence of the student*)
	PROCEDURE Attendance*(StdID:INTEGER):INTEGER;
		VAR i: INTEGER;
	BEGIN
		i:=SearchData(StdID);
		Student[i].Presence:=Student[i].Presence +1;
		RETURN Student[i].Presence;
	END Attendance;
	
(***************** SearchData ********************)
(*Checks if the student is in the data, and returns its location identifier*)
	PROCEDURE SearchData(StdID:INTEGER):INTEGER;
		VAR i: INTEGER;
	BEGIN
		i:=0;
		WHILE (Student[i].StdID # StdID) DO
			i:=i+1;
		END;
		RETURN i;
	END SearchData;
	
(******************** Test **********************)
(* Pretend this is a separate Test module that Imports StdArr *)
	PROCEDURE Test*();
	BEGIN
		NewStd(0099999);
		NewStd(0088888);
		NewStd(0077777);
		Out.Int(Attendance(0099999),2);
		Out.Int(Attendance(0099999),2);
		Out.Int(Attendance(0088888),2);
	END Test;
(****************** Module Body ****************)	
BEGIN
	Test();
END StdArr.
