#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    [CPU scheduling process] sorting processes


    Hi everyone, i could not find a way to solve my problem so i registered here in hope that somebody can help me. My problem is about the FCFS,in ordering the processes basing to their arrival time. Im new to arrays.

    in example:
    PROCESS NAME---BURST TIME----ARRIVAL TIME
    p0------------------3-----------------9
    p1------------------5-----------------4
    p2------------------3-----------------7
    p3------------------2-----------------0
    p4------------------5-----------------2


    so if im to sort them in ascending order it should look like this
    PROCESS NAME---BURST TIME----ARRIVAL TIME
    p3------------------2-----------------0
    p4------------------5-----------------2
    p1------------------5-----------------4
    p2------------------3-----------------7
    p0------------------3-----------------9



    and here is my code so far

    #include <stdio.h>
    #include<conio.h>


    main()
    {
    int bt[10],at[10],tc[10],a,b,c,temp,np,totalwt=0;
    float tat,wt,avgtat,avgwt;
    char name[10][10];

    for(a=0;a<10;a++)
    {
    bt[a]=0;
    at[a]=0;
    }

    //input no.of processes
    printf("NO. OF PROCESS: ");
    scanf("%d", &np);
    system("cls");

    //input burst times
    printf("\nBURST TIMES: ");
    for(a=0;a<np;a++)
    scanf("%d", &bt[a]);
    system("cls");

    //inputarrival times
    printf("\nARRIVAL TIMES: ");
    for(a=0;a<np;a++)
    scanf("%d",&at[a]);
    system("cls");

    //computation
    for(a=0;a<10;a++)
    {
    for(b=b+1;b<10;b++)
    {
    if(at[a]>at[b])
    {
    at[a]=temp;
    at[a]=at[b];
    at[b]=temp;
    }
    }
    }
    printf("\n\nPROCESS BURST TIME ARRIVALTIME\n");
    for(a=0;a<np;a++)
    printf("\np%d\t\t%d\t\t%d", a, bt[a],at[a]);
    for(b=0;b<10;b++)
    getch();
    }


    it willbe very much appreciated to whoever who can help me
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Let's remove variables unused in the post. Repair the pattern for "swap". Sort all arrays in the same way so they stay aligned. See other /* comments */ . Fixing the process name ordering remains an exercise.
    Code:
    #include<stdio.h>
    
    int main() {			/* int main() */
      int bt[10],at[10],a,b,temp,np;
    
      for(a=0;a<10;a++) {
        bt[a]=0;
        at[a]=0;
      }
    
      fputs("NO. OF PROCESS: ",stdout);
      scanf("%d", &np);
    
      if ((np < 0) || (10 < np))	/* insert error checks */
        return fputs("\nsorry, 0 to 10 are valid\n",stderr),1;
    
      //input burst times
      fputs("\nBURST TIMES: ",stdout);
      for(a=0;a<np;a++)
        scanf("%d",bt+a);
    
      //inputarrival times
      fputs("\nARRIVAL TIMES: ",stdout);
      for(a=0;a<np;a++)
        scanf("%d",at+a);
    
      for(a=0;a+1<np;a++)		/* fix the indexing */
        for(b=a+1;b<np;b++)		/* fix the indexing more */
          if(at[a]>at[b]) {
    	temp = at[a]; at[a]=at[b]; at[b]=temp; /* swap both arrays (and name as well) */
    	temp = bt[a]; bt[a]=bt[b]; bt[b]=temp;
          }
      puts("\n\nPROCESS\tBURST TIME\tARRIVAL TIME"); /* improve formatting */
      for(a=0;a<np;a++)
        printf("\np%d\t\t%d\t\t%d", a, bt[a],at[a]);
      putchar('\n');
      getchar();
      return 0;			/* main returns a value to operating system */
    }
    Last edited by b49P23TIvg; January 25th, 2013 at 05:09 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    thank you! this has been a great help =)

IMN logo majestic logo threadwatch logo seochat tools logo