Creating A Schedule Or Time Table For Educational Institution

Introduction

Creation of Scheduling/timetable for bigger educational institutions is a time-consuming process and has more complexities in solving the scheduling process. In this article, I will explain how to create a scheduling with C#.

Pre-Requisite Data for processing the flow

The following fields are required from the end-user for processing the flow

  1. Number of Classes
  2. Working Days Per Week
  3. Hours Per Day
  4. Hours Per Week
  5. Subjects Per Class
  6. Weekly Subjects Per Class
  7. Number of Teachers

Step 1:

Create a 3D array as,

int[,,] tabval = new int[totalClassRooms, totalDaysPerWeek, hoursPerDay];

Step 2

In the pre-requisite Data section, the Days in a week may have 6 Days (i.e. 6 X 7 Hours per day = 42 Hours). But in some cases, the hours may be limited to 4 hours on last / 6th Day (i.e 39 Hours). In the below step, the remaining values in the Array Variable are set as '-1'.

int rowCountVal = 1;
for (int i = 0; i < totalClassRooms; i++) {
    countHours = -1;
    for (int j = 0; j < totalDaysPerWeek; j++) {
        for (int k = 0; k < hoursPerDay; k++) {
            countHours++;
            if (countHours < hoursPerWeek) {
                tabval[i, j, k] = 0;
            } else {
                tabval[i, j, k] = -1;
            }
        }
        rowCountVal++;
    }
}

Step 3

In this step, the below code will store the subject values in the array variable. Here, in the first loop, the values for the third dimension of the array variable will be stored randomly for the first run of second dimension of the array variable. And the process will continue until all subjects have been added for all classes. While storing the values, the below step will check the previous and next hour values to avoid repetition of classes.

int noofSubjects = Convert.ToInt32(txtNoOfSubject.Text.ToString().Trim());
int noofOptionalSubjects = Convert.ToInt32(txtNoOfOptSubject.Text.ToString().Trim());
int[] coumpulsarySubjects = new int[noofSubjects];
int[] OptionalSubjects = new int[noofOptionalSubjects];
int z = 0;
for (int i = 0; i < coumpulsarySubjects.Length; i++) {
    coumpulsarySubjects[i] = i + 1;
    z = i;
}
for (int i = 0; i < OptionalSubjects.Length; i++) {
    z++;
    OptionalSubjects[i] = z + 1;
}
int subLoop = 0;
int teacherSelection = 0;
int firstRun = 0;
int firstrunFlagReset = 0;
int SecondRuntopFlag = 0;
for (int i = 0; i < totalClassRooms; i++) {
    if (firstrunFlagReset == 2) {
        firstRun = 2;
        firstrunFlagReset = -1;
    }
    for (int j = 0; j < totalDaysPerWeek; j++) {
        if (firstrunFlagReset == 1) {
            firstRun = 1;
            firstrunFlagReset = -1;
        }
        for (int k = 0; k < hoursPerDay; k++) {
            if (firstRun == 0) {
                firstrunFlagReset = 1;
                Random rnd = new Random();
                i = rnd.Next(0, totalClassRooms - 1);
            }
            if (firstRun == 1) {
                firstrunFlagReset = 2;
                if (SecondRuntopFlag < totalDaysPerWeek) {
                    Random rnd = new Random();
                    j = rnd.Next(0, totalDaysPerWeek - 1);
                    SecondRuntopFlag++;
                }
                if (SecondRuntopFlag == totalDaysPerWeek) {
                    j = totalDaysPerWeek - 1;
                    i = 0;
                }
            }
            int tempSub = coumpulsarySubjects[subLoop];
            SqlDataAdapter da1 = new SqlDataAdapter("select * from TeacherSpec where subject" + tempSub + " like 'Yes'", con);
            DataSet ds1 = new DataSet();
            da1.Fill(ds1);
            if (teacherSelection < ds1.Tables[0].Rows.Count) {
                teacherSelection++;
            } else if (teacherSelection == ds1.Tables[0].Rows.Count) {
                teacherSelection = 1;
            }
            int teacherVal = Convert.ToInt32(ds1.Tables[0].Rows[teacherSelection - 1][0].ToString());
            //-----------------------------------Checking Prev Hour and Next Hour Subject Starts-----------------------------
            int PrevHourSub = -1;
            int NxtHourSub = -1;
            int proceedFlag = -1;
            if (k > 0) {
                PrevHourSub = tabval[i, j, k - 1];
            }
            if (k < hoursPerDay) {
                if (k + 1 == hoursPerDay) {
                    NxtHourSub = -1;
                } else {
                    NxtHourSub = tabval[i, j, k + 1];
                }
            }
            if (PrevHourSub == ((teacherVal * 10) + tempSub) || NxtHourSub == ((teacherVal * 10) + tempSub)) {
                proceedFlag = 0;
            } else {
                proceedFlag = 1;
            }
            //-----------------------------------Checking Prev Hour and Next Hour Subject ends-----------------------------
            if (proceedFlag == 1) {
                proceedFlag = 0;
                if (tabval[i, j, k] == 0) {
                    SqlDataAdapter datemp1 = new SqlDataAdapter("select * from TeacherVSSubject where class=" + (i + 1) + " and subject=" + tempSub + "", con);
                    DataSet dstemp1 = new DataSet();
                    datemp1.Fill(dstemp1);
                    if (dstemp1.Tables[0].Rows.Count == 1 && Convert.ToInt32(dstemp1.Tables[0].Rows[0]["Teacher"].ToString().Trim()) != teacherVal) {
                        teacherVal = Convert.ToInt32(dstemp1.Tables[0].Rows[0]["Teacher"].ToString().Trim());
                    } else if (dstemp1.Tables[0].Rows.Count > 1) {
                        //Error
                    }
                    tabval[i, j, k] = (teacherVal * 10) + tempSub;
                    SqlDataAdapter datemp = new SqlDataAdapter("select * from TimeTable where (hour" + (k + 1) + "-(hour" + (k + 1) + "%10))/10 = " + teacherVal + " and dayval=" + (j + 1) + "", con);
                    DataSet dstemp = new DataSet();
                    datemp.Fill(dstemp);
                    if (dstemp.Tables[0].Rows.Count == 0) {
                        SqlCommand com3 = new SqlCommand("update TimeTable set hour" + (k + 1) + "=" + tabval[i, j, k] + " where dayval=" + (j + 1) + " and classval=" + (i + 1) + "", con);
                        com3.ExecuteNonQuery();
                        SqlCommand com6 = new SqlCommand("insert into ClassQueue values(" + (i + 1) + "," + tabval[i, j, k] + ")", con);
                        com6.ExecuteNonQuery();
                        SqlDataAdapter datemp2 = new SqlDataAdapter("select * from TeacherVSSubject where class=" + (i + 1) + " and teacher=" + teacherVal + " and subject=" + tempSub + "", con);
                        DataSet dstemp2 = new DataSet();
                        datemp2.Fill(dstemp2);
                        if (dstemp2.Tables[0].Rows.Count == 0) {
                            SqlCommand com4 = new SqlCommand("insert into TeacherVSSubject values(" + (i + 1) + "," + teacherVal + "," + tempSub + ")", con);
                            com4.ExecuteNonQuery();
                        }
                    } else {
                        tabval[i, j, k] = 0;
                    }
                }
            } else {
                //Subject already added in previous / next hour
            }
            subLoop++;
            if (subLoop >= coumpulsarySubjects.Length) {
                subLoop = 0;
            }
            if (firstRun == 1 && SecondRuntopFlag == totalDaysPerWeek) {
                i = -1;
            }
        }
    }
}

Step 4

Once the allocation of subjects has been completed, the below step will allocate the weekly subjects for all classes.

for (int i = 0; i < totalClassRooms; i++) {
    subLoop = 0;
    tempSub = OptionalSubjects[subLoop];
    for (int j = 0; j < totalDaysPerWeek; j++) {
        for (int k = 0; k < hoursPerDay; k++) {
            if (tabval[i, j, k] == 0) {
                SqlDataAdapter da1 = new SqlDataAdapter("select * from TeacherSpec where subject" + tempSub + " like 'Yes'", con);
                DataSet ds1 = new DataSet();
                da1.Fill(ds1);
                if (teacherSelection < ds1.Tables[0].Rows.Count) {
                    teacherSelection++;
                } else if (teacherSelection == ds1.Tables[0].Rows.Count || teacherSelection > ds1.Tables[0].Rows.Count) {
                    teacherSelection = 1;
                }
                int teacherVal = Convert.ToInt32(ds1.Tables[0].Rows[teacherSelection - 1][0].ToString());
                SqlDataAdapter datemp1 = new SqlDataAdapter("select * from TeacherVSSubject where class=" + (i + 1) + " and subject=" + tempSub + "", con);
                DataSet dstemp1 = new DataSet();
                datemp1.Fill(dstemp1);
                if (dstemp1.Tables[0].Rows.Count == 1 && Convert.ToInt32(dstemp1.Tables[0].Rows[0]["Teacher"].ToString().Trim()) != teacherVal) {
                    teacherVal = Convert.ToInt32(dstemp1.Tables[0].Rows[0]["Teacher"].ToString().Trim());
                } else if (dstemp1.Tables[0].Rows.Count > 1) {
                    //Error
                }
                tabval[i, j, k] = (teacherVal * 10) + tempSub;
                SqlDataAdapter datemp = new SqlDataAdapter("select * from TimeTable where (hour" + (k + 1) + "-(hour" + (k + 1) + "%10))/10 = " + teacherVal + " and dayval=" + (j + 1) + "", con);
                //select(hour2 - (hour2 % 10)) / 10 from TimeTable where (hour2 - (hour2 % 10)) / 10 > 0
                DataSet dstemp = new DataSet();
                datemp.Fill(dstemp);
                if (dstemp.Tables[0].Rows.Count == 0) {
                    SqlCommand com3 = new SqlCommand("update TimeTable set hour" + (k + 1) + "=" + tabval[i, j, k] + " where dayval=" + (j + 1) + " and classval=" + (i + 1) + "", con);
                    com3.ExecuteNonQuery();
                    SqlCommand com6 = new SqlCommand("insert into ClassQueue values(" + (i + 1) + "," + tabval[i, j, k] + ")", con);
                    com6.ExecuteNonQuery();
                    SqlDataAdapter datemp2 = new SqlDataAdapter("select * from TeacherVSSubject where class=" + (i + 1) + " and teacher=" + teacherVal + " and subject=" + tempSub + "", con);
                    DataSet dstemp2 = new DataSet();
                    datemp2.Fill(dstemp2);
                    if (dstemp2.Tables[0].Rows.Count == 0) {
                        SqlCommand com4 = new SqlCommand("insert into TeacherVSSubject values(" + (i + 1) + "," + teacherVal + "," + tempSub + ")", con);
                        com4.ExecuteNonQuery();
                    }
                    subLoop++;
                    if (OptionalSubjects.Length > subLoop) {
                        tempSub = OptionalSubjects[subLoop];
                    }
                } else {
                    tabval[i, j, k] = 0;
                }
            }
        }
    }
}

Finally generated result will be stored in the table 'TimeTable'.


Similar Articles