#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
#include <string.h>
/*Function declarations */

int main()

{
    /* Condition for program loop*/
    int isRunning =1;
    int option = 1;

    /* Number of students and number of courses that the user wishes
    to enter*/
    int numStudents;
    int numCourses;



    printf("\nEnter number of students: ");
    scanf("%d",&numStudents);
    int *studentIDs = (int *)malloc(numStudents *sizeof(int));

    initializeStudents(studentIDs, numStudents);

    printf("\nEnter number of courses offered: ");
    scanf("%d", &numCourses);
    char **courses = (char *)malloc(numCourses *sizeof(char));


    initializeCourses(courses,numCourses);

    /*initialize table*/
    int table[numCourses][numStudents];
    initilaize2DArray((int *) table, numStudents,numCourses);

    while(isRunning){
        printMenu();

        printf("\n\n Select an option: ");
        scanf("%d",&option);

        if(option == 1){
            registerStudent(courses,studentIDs,numStudents,numCourses,(int *) table);

        }
        else if(option == 2){
            withdrawStudent(courses,studentIDs,numStudents,numCourses,(int *) table);
        }
        else if(option == 3){
            printf("----------------------------Registration table-----------------------");
            printf("\n");
            print2DArray((int *) table, numStudents,numCourses);
        }
        else if(option == 4){
            /*end of program*/
            isRunning = 0;
        }
        else
            printf("\ninvalid input");

    }


    return 0;
}

 /*print main menu method*/
 void printMenu(){
    printf("\n\n----------------------Main Menu----------------------");
    printf("\n1. Register a student in a course");
    printf("\n2. Withdraw a student in a course");
    printf("\n3. Display registration table:");
    printf("\n4. Exit");

    }


 /*initialize studentIDs array method*/
 void initializeStudents(int *studentIDs, int numStudents){
    int i;
    for(i =0;i<numStudents;i++){
        printf("    Enter student ID for student %d: ",i+1);
        scanf("%d", &studentIDs[i]);

        /*check if number is 5 digits by dividing it by 10 000 and checking if it is greater than 10 or lesser than 0*/
        if(studentIDs[i]/10000>=10 || studentIDs[i]/10000<=0){
            printf("ID Must be 5 digits\n");
            i--;
        }
    }
 }

  /*initialize courses method*/
  void initializeCourses(char **courses, int numCourses){
 int j;
    for(j=0;j<numCourses;j++){
        courses[j] = (char*) malloc(sizeof(char));
        printf("    Enter name of course %d: ", j+1);
        scanf("%s",courses[j]);

            /*check if length of string is 7*/
            if(strlen(courses[j]) !=7){
                printf("Not 7 digits\n");
                j--;
            }
    }
 }

 /*register student method*/
 /*In c you can't pass a variable by reference, the array variable that you assign inside the function contains initially the same address as the passed pointer,*/
 /*but it's a copy of it so modifying it will not alter the passed pointer.*/
 /*You need to pass the address of the pointer in order to be able to alter it, like this*/


 void registerStudent(char **courses,int *studentIDs, int numStudents,int numCourses, int (*table)[numStudents]){
    int tempID,i,j,indexStudent,indexCourse;
    char *tempCourse[7];

    /*prompt user for student id and course code.*/
    printf("\nEnter student ID: ");
    scanf("%d",&tempID);
    printf("Enter course code: ");
    scanf("%s",tempCourse);

    /*loop to find student index*/
    for(i=0;i<numStudents;i++){
        if(studentIDs[i]==tempID){
            indexStudent = i;
            break;
        }
    }

    /*Loop to find course index*/
    for(j=0;j<numCourses;j++){
        if(strcmp(courses[j],tempCourse) == 0){
            indexCourse = j;
            break;
        }
    }

    /*Assign value of 1 to the table at index [indexCourse][indexStudent] found*/
    table[indexCourse][indexStudent]=1;

 }

 void withdrawStudent(char **courses,int *studentIDs, int numStudents,int numCourses, int (*table)[numStudents]){
    int tempID,i,j,indexStudent,indexCourse;
    char *tempCourse[7];

    /*prompt user for student id and course code.*/
    printf("\nEnter student ID: ");
    scanf("%d",&tempID);
    printf("Enter course code: ");
    scanf("%s",tempCourse);

    /*loop to find student index*/
    for(i=0;i<numStudents;i++){
        if(studentIDs[i]==tempID){
            indexStudent = i;
            break;
        }
    }

    /*Loop to find course index*/
    for(j=0;j<numCourses;j++){
        if(strcmp(courses[j],tempCourse) == 0){
            indexCourse = j;
            break;
        }
    }

    /*Assign value of 1 to the table at index [indexCourse][indexStudent] found*/
    table[indexCourse][indexStudent]=0;

 }

