-1

I need to ask user to insert Number of Processes, Process ID and Waiting time then i have to sort, compare and print based on Process priority I am new on C, I can't figure out how to do it Any help is appreciated Thanks in advance*/

struct Process {
  int Id;
  int Prio;
  int Time;
};

struct Process process[100];


void init() {
  printf("Enter number of processes:\n ");
  scanf("%d",&n);
  while(n<3) {
    printf("Number has to be grater than 2\n");
    scanf("%d",&n);
  }

  for (int x=0; x<n; x++) {
    printf("Process %d ID:\n ", x+1);
    scanf("%d",&process[x].Id);
    printf("Process %d priority:\n ", x+1);
    scanf("%d",&process[x].Prio);
    printf("Process %d time:\n ", x+1);
    scanf("%d",&process[x].Time);
    }
}

void priority() {
  for (int x=0; x<n; x++) {
    printf("%d",process[x].Id);
    printf("        %d",process[x].Prio);
    printf("           %d\n\n",process[x].Time);
  }
}

void line(int dashes) {
  for(int x=1;x<dashes;x++) {
    printf("-");
  }
}

void display() {
  printf("\n");
  printf("                        PROCESS SCHEDULING\n");
  line(90);
  printf("\n");
  printf("ID");
  printf("    PRIORITY");
  printf("    WAITING TIME");
  line(90);
  printf("\n\n");
}
int main() {
  init();
  display();
  priority();
  return 0;
}
5
  • If I understand your question, you need to sort processes based on priority, simply apply any sort algorithm and compare by process.priority. What's your trouble exactly? Sintax?
    – IssamTP
    Nov 14, 2013 at 13:33
  • is this an assignment? which tutorial are you following? have you tried to compile this code (where is the variable "n")? which compiler? which operating system is this on?
    – x29a
    Nov 14, 2013 at 13:35
  • this is a assignment IssamTP. The think is I don't know the algorithm/sintax to apply.
    – escuk
    Nov 14, 2013 at 13:43
  • I have defined the variable "n" on top of my code. The code is working, but only printing the data as entered.
    – escuk
    Nov 14, 2013 at 13:44
  • Well, then you'll find my answer down below. Insert the call between init and display. lemme know. Please accept it if it works.
    – IssamTP
    Nov 14, 2013 at 13:57

2 Answers 2

0

For sorting you can apply any sorting algorithm (bubble sort, insertion sort, heap sort .. ) but when comparing adjacent elements, compare their priorities. Something like

if(process[index1].Prio < process[index2].Prio)
0

This is an implementation of BubbleSort, an elementary algorithm for sorting. This is not the best around you can find, but it's one of the easiest to understand. Reference here.

void SortProcesses()
{
  int right_bound = n - 1; /* Assuming you've declared n as number of processes inputed. */
  Process swap;

  while( right_bound > 0 )
  {
    for( int i = 0; i < right_bound; i++ )
    {
       if( process[i].Prio > process[i + 1].Prio )
       {
         swap.Id = process[i+1].Id;
         swap.Prio = process[i+1].Prio;
         swap.Time = process[i+1].Time;
         process[i+1].Id = process[i].Id;
         // ... idem for Prio and Time;
         process[i].Id = swap.Id;
         // ... idem for Prio and Time
         right_bound--;
       }
    }
  }
}
2
  • thanks very much you all. I've used another for loop instead of While loop.
    – escuk
    Nov 14, 2013 at 19:06
  • There are more way to do same thing.
    – IssamTP
    Nov 15, 2013 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.