Saturday 4 July 2015

DIGI BLACK BOARD

DIGI BLACK BOARD



Digi Black Board is application written in java . As the name suggest Digi Black Board is Digital Black Board for the Smart Class Room . Its just not a PPT editor only , it back itself with some other cool functionality which makes  it best suitable for the job.

some other functionaliy are as follow :-

1. Screen Recorder
2. PPT to PDF
3. Rough Pad
4. Drawing Pad
5. Smart Attendance Management System
6. One touch File Sharing option
7. Voice Activated 
8. And many more...

Full source Code and Full version of application will be available soon...

Snapshot of the application









Saturday 21 March 2015

Bezier Curve Algorithm in C | OpenGL for n control points

 

Bezier Curve Algorithm in C | OpenGL


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<GL/freeglut.h>
#include<GL/gl.h>
#define STEP 0.0001
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
int numpoints=0,q=0;
typedef struct{
    float x,y;
}POINT;
POINT *ctrlPoint,*auxPoint;

int i,j,f;
float t;
long double factorial(int x){
    long double fact=1;
    int p=1;
    if(x==0 || x==1)
        return 1;
    else{
        while(p<=x){
            fact*=p;
            p++;}
        }
    return fact;
}
long double binomial( int f, int n){
    double com=factorial(n)/(factorial(f)*factorial(n-f));
    return com;
}

POINT bernstein(POINT *ctrlPoint,float t,int n){
POINT N;N.x=N.y=0;

for(i=0;i<=n;i++){
    N.x+=ctrlPoint[i].x*binomial(i,n)*pow(t,i)*pow((1-t),(n-i));
    N.y+=ctrlPoint[i].y*binomial(i,n)*pow(t,i)*pow((1-t),(n-i));
    }
    return N;

}
void init(){
    glClearColor(0.0,0.0,0.0,0.0);
    glPointSize(10.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,(float)SCREEN_WIDTH,0.0,(float)SCREEN_HEIGHT);

}



void drawPoint(int x,int y){
    glColor3f(0.0,0.0,1.0);   
    glBegin(GL_POINTS);
        glVertex2i(x,y);
    glEnd();
    glFlush();
}

void drawLine(POINT p1,POINT p2){
    glColor3f(1.0,0.0,0.0);   
    glLineWidth(1.0);   
    glBegin(GL_LINES);
        glVertex2f(p1.x,p1.y);
        glVertex2f(p2.x,p2.y);
    glEnd();
    glFlush();
}
void drawCurve(POINT p1,POINT p2,float color){
    glColor3f(1,0.0,0.0);   
    glLineWidth(1);   
    glBegin(GL_LINES);
        glVertex2f(p1.x,p1.y);
        glVertex2f(p2.x,p2.y);
    glEnd();
    glFlush();
}

void MouseEvent(int button,int state,int x,int y){
    POINT M,P=ctrlPoint[0];
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
        ctrlPoint[numpoints].x=(float)x;
        ctrlPoint[numpoints].y=(float)SCREEN_HEIGHT-y;
        numpoints++;
        drawPoint(x,SCREEN_HEIGHT-y);
        }
    else
        if(button==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN){
            for(t = 0;t <= 1.0; t += STEP) {
                    M=bernstein(ctrlPoint,t,numpoints-1);   
                    drawCurve(P,M,t);
                    P=M;
                }
            }
        else
            if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN){
                for(i=0;i<numpoints-1;i++)
                    drawLine(ctrlPoint[i],ctrlPoint[i+1]);
            }
}
void keyboardEvent(unsigned char key,int x, int y){
    if(key==27)
        exit(0);
    else
        if(key=='C'){
            glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                         glFlush();
            numpoints=0;
            }
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}

int main(int argc, char *argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(800,600);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Bezier Curve Application in OpenGL | Developed BY Shaharyar Shaukat");
    ctrlPoint=(POINT *)malloc(q*sizeof(POINT));
    if(!ctrlPoint){
        exit(EXIT_FAILURE);
        }
    else{
    q++;
    auxPoint=(POINT *)realloc(ctrlPoint,q*sizeof(POINT));
    if(!auxPoint){
        exit(EXIT_FAILURE);
        }
    else{   
        ctrlPoint=auxPoint;
            glutMouseFunc(MouseEvent);
        }
    }
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboardEvent);
    init();
    glutMainLoop();
    free(ctrlPoint);
    free(auxPoint);
    return 0;
}

Saturday 21 February 2015

how to implement threads in c

how to implement threads in c




Pthreads are a simple and effective way of creating a multi-threaded application. 
When a thread is created using pthread_create, both the original thread and the new thread share the same code base and the same memory – it’s just like making two function calls at the same time.


All C programs using pthreads need to include the pthread.h header file.







#include<stdio.h>
#include<pthread.h>

pthread_t thread[2];


static void *function1(void *_){

 int i;
 for(i=0;i<10;i++){
  printf("\nThread1 says=%d",i);
  sleep(1);

}
}

static void *function2(void *_){

 int i;
 for(i=0;i<5;i++){

  printf("\nThread2 says=%d",i);
  sleep(5);
}
}

int main(){

    pthread_create(&thread[0], NULL , function1, NULL);
    pthread_create(&thread[1], NULL , function2, NULL);

    pthread_exit(NULL);
return 1;
}

Thursday 12 February 2015

NQueen Problem in C



 NQueen Problem in C



 




#include<stdio.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
#define MAXBOARDSIZE 8
int board[MAXBOARDSIZE];
int n;
int steps;
static int solno=1;
void PrintBoard()
{
    int i, k,z;

    printf("\nSolution %d:\n", solno++);
    printf("No of Steps=%d:\n", steps);
    for (i = 0; i < n; i++)
    {
        for (k = 0; k < board[i]; k++)
            printf("\t-");
        printf("\tQ%d",i+1);
        for(z=k;z<n-1;z++)
            printf("\t-");
        printf("\n");
    }
}

int IsPlaceSafe(int row, int col)
{
    int i;

    for(i=0; i<row; i++)
    {
        if( (board[i] == col) || (abs(board[i]-col) == abs(i-row)) )
            return FALSE;
    }
    return TRUE;
}

void NQueens(int row)
{
    int col;
    for(col=0; col<n; col++)
    {
        steps++;
        if(IsPlaceSafe(row, col))
        {
            board[row] = col;
            if(row == n-1)
                PrintBoard();
            else
                NQueens(row+1);
        }
    }
}

int main()

    int c=1;
    while(c==1){
    printf("Enter board size: ");
    scanf("%d", &n);
    if(n>MAXBOARDSIZE || n<4){
     printf("\nNo Solution Exit\n");
    }else{
    NQueens(0);
    }
    printf("\nPress 1 to Continue and 0 to exit");
    scanf("%d",&c);
    solno=1;
    steps=0;
    }
    return 0;
}

Wednesday 4 February 2015

DFA implementation in java

DFA implementation in java







--------APP CLASS--------
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.*;


public class App {
JFrame f1,f2,f3,f4,f5,f_dailog;
JButton JB_dfa,JB_nfa,JB_new,JB_select,JB_cDFA[];
String path="";
ArrayList<String> records;
int TT[][];
int start_state=0,final_state=0,no_state=0,no_var=0;
String inputString="";
DFA obj_DFA;
public App(){
f1=new JFrame();
f1.setSize(600, 420);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setResizable(false);
f1.setLocationRelativeTo(null);
f1.setLayout(null);
f1.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f1.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_dfa=new JButton("DFA");
JB_dfa.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_dfa.setBounds(50, 200, 80, 50);
JB_dfa.setBackground(new Color(109, 183, 246));
JB_dfa.setForeground(new Color(255,255,255));
JB_dfa.setFont(new Font("Garamond",  Font.BOLD , 20));
f1.add(JB_dfa);
JB_dfa.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f2.setVisible(true);
f1.setVisible(false);
}
});
JB_nfa=new JButton("NFA");
JB_nfa.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_nfa.setBounds(200, 200, 80, 50);
JB_nfa.setBackground(new Color(109, 183, 246));
JB_nfa.setForeground(new Color(255,255,255));
JB_nfa.setFont(new Font("Garamond",  Font.BOLD , 20));
f1.add(JB_nfa);
JB_nfa.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
f1.validate();
// FRAME 2
f2=new JFrame();
f2.setSize(600, 420);
f2.setVisible(false);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.setResizable(false);
f2.setLocationRelativeTo(null);
f2.setLayout(null);
f2.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f2.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_new=new JButton("CREATE NEW DFA");
JB_new.setToolTipText(" Provide Transition Table ");
JB_new.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_new.setBounds(30, 120, 300, 50);
JB_new.setBackground(new Color(109, 183, 246));
JB_new.setForeground(new Color(255,255,255));
JB_new.setFont(new Font("Garamond",  Font.BOLD , 25));
JB_new.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f_dailog.setVisible(true);
}
});
f2.add(JB_new);
JB_select=new JButton("SELECT  DFA");
JB_select.setToolTipText(" Select Sample DFA From given List  ");
JB_select.setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_select.setBounds(30, 220, 300, 50);
JB_select.setBackground(new Color(109, 183, 246));
JB_select.setForeground(new Color(255,255,255));
JB_select.setFont(new Font("Garamond",  Font.BOLD , 25));
JB_select.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
f2.setVisible(false);
f3.setVisible(true);
}
});
f2.add(JB_select);
// USER INPUT 
f_dailog=new JFrame("");
f_dailog.setSize(300, 200);
f_dailog.setVisible(false);
f_dailog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f_dailog.setResizable(false);
f_dailog.setLocationRelativeTo(null);
f_dailog.setLayout(null);
JLabel lbl_noState=new JLabel("NO. of States");
lbl_noState.setBounds(10, 10, 80, 40);
JTextField txt_noState=new JTextField();
txt_noState.setBounds(90, 10, 40, 40);
JLabel lbl_noVar=new JLabel("NO. of Var");
lbl_noVar.setBounds(160, 10, 80, 40);
JTextField txt_noVar=new JTextField();
txt_noVar.setBounds(220, 10, 40, 40);
f_dailog.add(lbl_noState);
f_dailog.add(lbl_noVar);
f_dailog.add(txt_noState);
f_dailog.add(txt_noVar);
//FRAME 3 Pre selected
f3=new JFrame();
f3.setSize(600, 420);
f3.setVisible(false);
f3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f3.setResizable(false);
f3.setLocationRelativeTo(null);
f3.setLayout(null);
f3.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
try{
BufferedImage bf = ImageIO.read(new File("icon//BG1.png"));  
f3.setContentPane(new Bg(bf));
}catch(Exception e){
}
JB_cDFA=new JButton[5];
JB_cDFA[0]=new JButton("DFA that accepts all string which occur with even no. of 0s and 1s");
JB_cDFA[0].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[0].setBounds(5, 100, 350, 30);
JB_cDFA[0].setBackground(new Color(109, 183, 246));
JB_cDFA[0].setForeground(new Color(255,255,255));
JB_cDFA[0].setFont(new Font("Garamond",  Font.BOLD , 9));
JB_cDFA[0].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
path="files//0.txt";
readFile(path);
f3.setVisible(false);
f5.setVisible(true);
System.out.println("");
for(int i=0;i<records.size();i++){
System.out.println(records.get(i));
}
System.out.println("------------");
System.out.println("no-states"+no_state);
System.out.println("start_states"+start_state);
System.out.println("final-states"+final_state);
System.out.println("no_var="+no_var);
System.out.println("--------------");
for(int i=0;i<no_state;i++){
System.out.print("\t"+TT[i][0]);
System.out.print("\t"+TT[i][1]);
System.out.print("\n");
}
obj_DFA=new DFA(TT,start_state,final_state,no_state,no_var);
Display obj_Display=new Display(TT);
obj_Display.setBounds(10, 100, 980, 480);
f5.add(obj_Display);
Thread t_Display=new Thread(obj_Display);
t_Display.start();

}
});
f3.add(JB_cDFA[0]);
JB_cDFA[1]=new JButton("DFA that accepts all string which ends with 0 and 1");
JB_cDFA[1].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[1].setBounds(5, 150, 350, 30);
JB_cDFA[1].setBackground(new Color(109, 183, 246));
JB_cDFA[1].setForeground(new Color(255,255,255));
JB_cDFA[1].setFont(new Font("Garamond",  Font.BOLD , 9));
JB_cDFA[1].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
path="files//1.txt";
readFile(path);
f3.setVisible(false);
f5.setVisible(true);
System.out.println("");
for(int i=0;i<records.size();i++){
System.out.println(records.get(i));
}
System.out.println("------------");
System.out.println("no-states"+no_state);
System.out.println("start_states"+start_state);
System.out.println("final-states"+final_state);
System.out.println("no_var="+no_var);
System.out.println("--------------");
for(int i=0;i<no_state;i++){
System.out.print("\t"+TT[i][0]);
System.out.print("\t"+TT[i][1]);
System.out.print("\n");
}
obj_DFA=new DFA(TT,start_state,final_state,no_state,no_var);
Display obj_Display=new Display(TT);
obj_Display.setBounds(10, 100, 980, 480);
f5.add(obj_Display);
Thread t_Display=new Thread(obj_Display);
t_Display.start();

}
});
f3.add(JB_cDFA[1]);
JB_cDFA[2]=new JButton("DFA that accepts all string which have 2nd last symbol is 1");
JB_cDFA[2].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[2].setBounds(5, 200, 350, 30);
JB_cDFA[2].setBackground(new Color(109, 183, 246));
JB_cDFA[2].setForeground(new Color(255,255,255));
JB_cDFA[2].setFont(new Font("Garamond",  Font.BOLD , 9));
f3.add(JB_cDFA[2]);
JB_cDFA[3]=new JButton("DFA that accepts all string which have 011 as substring");
JB_cDFA[3].setCursor(new Cursor(Cursor.HAND_CURSOR));
JB_cDFA[3].setBounds(5, 250, 350, 30);
JB_cDFA[3].setBackground(new Color(109, 183, 246));
JB_cDFA[3].setForeground(new Color(255,255,255));
JB_cDFA[3].setFont(new Font("Garamond",  Font.BOLD , 9));
f3.add(JB_cDFA[3]);
// DISPLAY FRAME
f5=new JFrame();
f5.setSize(1000, 600);
f5.setVisible(false);
f5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f5.setResizable(false);
f5.setLocationRelativeTo(null);
f5.setLayout(null);
f5.setTitle("FINITE AUTOMATA | DEVELOPED BY SHAHARYAR SHAUKAT");
final JTextField input=new JTextField();
JButton start=new JButton("START");
start.setCursor(new Cursor(Cursor.HAND_CURSOR));
start.setBounds(30, 10, 120, 50);
start.setBackground(new Color(109, 183, 246));
start.setForeground(new Color(255,255,255));
start.setFont(new Font("Garamond",  Font.BOLD , 20));
start.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
inputString=input.getText();
System.out.println(inputString);
obj_DFA.input=inputString;
Thread t_DFA=new Thread(obj_DFA);
t_DFA.start();
}
});
f5.add(start);
JButton pause=new JButton("PAUSE");
pause.setCursor(new Cursor(Cursor.HAND_CURSOR));
pause.setBounds(850, 10, 120, 50);
pause.setBackground(new Color(109, 183, 246));
pause.setForeground(new Color(255,255,255));
pause.setFont(new Font("Garamond",  Font.BOLD , 20));
pause.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
f5.add(pause);
input.setBounds(200, 10, 600, 50);
input.setFont(new Font("SansSerif", Font.BOLD, 25));
f5.add(input);
}
public static void main(String args[]){
new App();
}
public void readFile(String filename)
{
 records = new ArrayList<String>();
 try
 {
   BufferedReader reader = new BufferedReader(new FileReader(filename));
   String line;
   while ((line = reader.readLine()) != null)
   {
     records.add(line);
   }
   reader.close();
   storeValue();
   
 }
 catch (Exception e)
 {
   System.err.format("Exception occurred trying to read '%s'.", filename);
   e.printStackTrace();
  
 }
}
//
public void storeValue(){
no_state=records.size()-2;
start_state=records.get(0).charAt(1)-48;
final_state=records.get(1).charAt(1)-48;
no_var=records.get(2).trim().split("\\s+").length;
TT=new int[no_state][no_var];
for(int i=0;i<no_state;i++){
String[] words=records.get(i+2).split("\\s+");
for(int j=0;j<2;j++){
TT[i][0]=words[0].charAt(1)-48;
TT[i][1]=words[1].charAt(1)-48;
}
}
}
// End Class

}


----------------DFA CLASS--------------


public class DFA implements Runnable {
int TT[][];
static String input="";
static int start_state=0,final_state=0,no_state=0,no_var=0;
public static int present_state=0;
static int result=3,ptr=0;
public DFA(){
}
public DFA(int TT[][],int start_state ,int final_state ,int no_state,int no_var){
this.TT=TT;
this.start_state=start_state;
this.final_state=final_state;
this.no_state=no_state;
this.no_var=no_var;
present_state=start_state;
}
@Override
public void run() {
// TODO Auto-generated method stub
   result=0;
Algo();
}
public void Algo(){
for(int i=0;i<input.length();i++){
ptr=i;
char ch=input.charAt(i);
int ch_value=(int)ch-48;
int temp_state=present_state;
present_state=TT[temp_state][ch_value];
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(present_state==final_state){
System.out.println("ACCEPTED");
result=1;
}else{
System.out.println("NOT ACCEPTED");
result=2;
}
}

}


------------BG CLASS----------

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JComponent;
public class Bg extends JComponent {
Image i;
public Bg(Image i) {
this.i = i;
}
public void paintComponent(Graphics g) {
g.drawImage(i, 0, 0, null);  // Drawing image using drawImage method
 
}
}

Thursday 22 January 2015

Shortest Job First with Preemption in C

Shortest Job First with Preemption in C


Before proceeding forward , lets talk about data structures which i have used in this program.

1. I have created a structure  Process.
2. I have implemented a Job Pool of process via array of structure type Process which also used for input.
3. Ready Queue implemented through linked list. enqueue and dequeue function for ready queue.
4. Load_ReadyQueue function which load Process from job pool to ready queue.
5. MinInQueue is function to find the process in ReadyQueue having minimum reaming cpu burst time. ** Note that this function store the the address of that minimum process in the min , which is pointer  to the Process.
6. CPU is where process is actually allocated , Data structure used for CPU is struct     Process.

Procedure  is straight forward   

Run the loop for total sum of CPUBURST of all the process
Load the ready queue for every value of loop
IF (CPU is empty) Then allocate process from the Ready Queue having minimum cpu burst
IF( current running process in CPU have greater remaining cpu burst than process residing  in ready queue) Then swap the processes and allocate new process to CPU.
IF two process having same remaining cpu burst time then First come first serve is used for deciding the process.
IF( current process in CPU have completed its total cpu bound) then deallocate the CPU , and also free the pointer that pointing to address of that particular process.
** Note  dynamic memory have to freed
Continue these steps util all process have completed their cpu bound.

For simulation of process scheduling , check my process scheduling simulator in Java.






Source Code:-

#include <stdio.h>
#include <stdlib.h>

struct Process{
    int job_id;
    int arrival_time;
    int cpu_burst;
    int reaming_time;
    int waiting_time;
    int turnaround_time;
    int response_time;
    int lastresponse_time;
};
struct Process job_queue[10];
struct Process CPU;
struct Process temp_process;
struct Node{
    struct Process process;
    struct Node *link;
}*head,*tail,*temp,*ptr,*min,*minLink;
// Global functions

void enqueue(struct Process p);
void MinInQueue();
void load_ReadyQueue(int var);

// Global variable
int n,t=0;
double sum=0,sum2=0;
double avg_time,avg_turnTime;

int main()
{
    int i,j; // n->no. of process
    int loop_time;
    printf("\tSHORTEST REMAING TIME FIRST SCHEDULING\n");
    printf("\t ENTER NO. OF PROCESS\t=\t");
    scanf("%d",&n);
    printf("\n\t JOB_ID|ARRIVAL TIME|CPU BURST \n");
    printf("\t ------------------------------\n");
    for(i=0;i<n;i++){
        printf("\t     %d\t\t",i+1);
        scanf("\t%d\t\t\t \t",&job_queue[i].arrival_time);
        scanf("\t  %d",&job_queue[i].cpu_burst);
        job_queue[i].job_id=i+1;
        job_queue[i].reaming_time=job_queue[i].cpu_burst;
        t=t+job_queue[i].cpu_burst;
    }
    for(j=0;j<t;j++){
        load_ReadyQueue(j);
        if(CPU.reaming_time==0){
            MinInQueue();
            if(min==NULL){
                printf("|IDLE|");
            }else{
                CPU=min->process;
                CPU.lastresponse_time=j;
                if(CPU.cpu_burst==CPU.reaming_time){
                CPU.response_time=j;
                }
                if(min==head && min==tail){
                    head=NULL;
                    tail=NULL;
                    free(min);
                }else if(min==head && min!=tail){
                    head=min->link;
                    free(min);
                }else if(min==tail && min!=head){
                    minLink=head;
                    ptr=head;
                    while(ptr->link!=NULL){
                        minLink=ptr;
                        ptr=ptr->link;
                    }
                    tail=minLink;
                    minLink->link=NULL;
                    free(min);
                    minLink=NULL;
                }else if(min!=head && min!=tail){
                    minLink=head;
                    ptr=head;
                    while(ptr->link!=min){
                        minLink=ptr;
                        ptr=ptr->link;
                    }
                    minLink->link=min->link;
                    free(min);
                    minLink=NULL;
                }
                CPU.reaming_time--;
                printf("|JOB_ID%d|",CPU.job_id);
                //min=NULL;
            }
        }else if(CPU.reaming_time>0){
            MinInQueue();
            if(min!=NULL && min->process.reaming_time < CPU.reaming_time){
                    if(min==head && min==tail){
                        temp_process=CPU;
                        head=NULL;
                        tail=NULL;
                        CPU=min->process;
                        CPU.lastresponse_time=j;
                         if(CPU.cpu_burst==CPU.reaming_time){
                          CPU.response_time=j;
                           }
                        CPU.reaming_time--;
                        printf("|JOB_ID%d|",CPU.job_id);
                        free(min);
                        enqueue(temp_process);
                    }else if(min==head && min!=tail){
                        temp_process=CPU;
                        head=min->link;
                        CPU=min->process; 
                        CPU.lastresponse_time=j;
                           if(CPU.response_time==0){
                            CPU.response_time=j;
                             }
                        CPU.reaming_time--;
                        printf("|JOB_ID%d|",CPU.job_id);
                        free(min);
                        enqueue(temp_process);
                    }else if(min!=head && min==tail){
                        minLink=head;
                        ptr=head;
                        while(ptr->link!=NULL){
                            minLink=ptr;
                            ptr=ptr->link;
                        }
                        temp_process=CPU;
                        CPU=min->process;
                        CPU.lastresponse_time=j;
                        if(CPU.cpu_burst==CPU.reaming_time){
                         CPU.response_time=j;
                         }
                        tail=minLink;
                        minLink->link=NULL;
                        CPU.reaming_time--;
                        printf("|JOB_ID%d|",CPU.job_id);
                        free(min);
                        enqueue(temp_process);
                    }else{
                        minLink=head;
                        ptr=head;
                        while(ptr->link!=min){
                            minLink=ptr;
                            ptr=ptr->link;
                        }
                        temp_process=CPU;
                        CPU=min->process;
                        CPU.lastresponse_time=j;
                        if(CPU.cpu_burst==CPU.reaming_time){
                          CPU.response_time=j;
                          }
                        minLink->link=min->link;
                        CPU.reaming_time--;
                        printf("|JOB_ID%d|",CPU.job_id);
                        free(min);
                        enqueue(temp_process);
                    }

            }else{
                CPU.reaming_time--;
                printf("|JOB_ID%d|",CPU.job_id);
            }

        }
        if(CPU.reaming_time==0){
            job_queue[CPU.job_id-1].turnaround_time=j-job_queue[CPU.job_id-1].arrival_time+1;
            job_queue[CPU.job_id-1].waiting_time=job_queue[CPU.job_id-1].turnaround_time-job_queue[CPU.job_id-1].cpu_burst;
            job_queue[CPU.job_id-1].response_time=CPU.response_time;
            job_queue[CPU.job_id-1].lastresponse_time=CPU.lastresponse_time;
        }

    }
    printf("\n----------------------------------------------------------------------------------------------\n");
    printf("\t JOB_ID | Waitting Time | TurnAround Time | First Response Time | Last Response Time\n");
    for(i=0;i<n;i++){
        printf("\t  %d",job_queue[i].job_id);
        printf("\t \t%d",job_queue[i].waiting_time);
        printf("\t  %d",job_queue[i].turnaround_time);
        printf("\t\t     %d",job_queue[i].response_time);
        printf("\t\t\t        %d",job_queue[i].lastresponse_time);
        printf("\n");
        sum=sum+job_queue[i].waiting_time;
        sum2=sum2+job_queue[i].turnaround_time;
    }
    avg_time=sum/n;
    avg_turnTime=sum2/n;
    printf("\n\n AVG. WAITING TIME=%lf",avg_time);
    printf("\n\n AVG. TURN AROUND TIME=%lf",avg_turnTime);
    printf("\n");

    return 0;
}

// Function definition
void enqueue(struct Process p){
    // if no element is present
    if(tail==NULL){
        temp=(struct Node *)malloc(1*sizeof(struct Node));
        temp->link=NULL;
        temp->process=p;
        head=temp;
        tail=head;
    }else{
        temp = (struct Node *)malloc(1*sizeof(struct Node));
        tail->link=temp;
        temp->process=p;
        temp->link=NULL;
        tail=temp;
    }
}

void MinInQueue()
{
    ptr=head;

    if(ptr==NULL){
        min=NULL;
        return;
    }
      min = head;
    for (ptr=ptr->link;ptr!=NULL;ptr=ptr->link)
    {
        if(ptr->process.reaming_time < min->process.reaming_time){
            min=ptr;
        }else if(ptr->process.reaming_time==min->process.reaming_time){
            if(ptr->process.job_id < min->process.job_id){
                min=ptr;
            }
        }else {

        }
    }

}

load_ReadyQueue(int var){
    int i; // i-> local loop variable
    for(i=0;i<n;i++){
        if(job_queue[i].arrival_time==var){
            enqueue(job_queue[i]);
        }
    }


}