الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[ تمت الإجابة ]تصحيح كود non-primitive sjf

بدأه empror9 في 25 أغسطس 2012 · 5 رد · 507 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمه الله وبركاته

اريد ان اكمل برنامج يقوم بـ non-primitive sjf .. الكود لدي شبه جاهز لكن ينقصه بعض الامور

#include<iostream>
using namespace std;

struct process{
    int processId;
    int burstTime;
    int arrivalTime;
};

int main(){
    int numberOfProcesses;

     //number of process
    cout <<"please enter the number of process: ";
    cin >> numberOfProcesses;
    const int maxprocesses = 10;
    struct process p[maxprocesses];

//int smaller[numberOfProcesses];

    // initialize variables
    int smallest_burst = p[0].burstTime;
    int smallest_burst_index = 0;
    int smallest_arrivalTime = p[0].arrivalTime;
    int smallest_arrivalTime_index = 0;



     // get burst time , arrival time
     for(int i=0; i<numberOfProcesses; i++){
        p.processId = i;
        cout << "p" << i << ":" << endl;
        cout <<"Burst time: ";
        cin >> p.burstTime;
        cout <<"Arrival Time: ";
        cin >> p.arrivalTime;
      }

       //display user input
      cout<<"Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
      for(int i=0;i<numberOfProcesses;i++){
          cout <<"P" << p.processId << "\t" << p.burstTime << "\t" << p.arrivalTime <<endl;
      }



     // after computing
     for(int i = 1; i < numberOfProcesses; ++i)
    {
       if(p.burstTime < smallest_burst)
       {
           smallest_burst = p.burstTime;
           smallest_burst_index = i;
       }

        if(p.arrivalTime < smallest_arrivalTime)
       {
           smallest_arrivalTime = p.arrivalTime;
           smallest_arrivalTime_index = i;
       }
    }

      //display
     cout << endl;
     cout<<"Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
     for(int i=0; i<numberOfProcesses; i++)
    {
     cout <<"P" << p[smallest_arrivalTime_index].processId << "\t" << p[smallest_arrivalTime_index].burstTime << "\t" << p[smallest_arrivalTime_index].arrivalTime << endl;
     cout <<"P" << p[smallest_burst_index].processId << "\t" << p[smallest_burst_index].burstTime << "\t" << p[smallest_burst_index].arrivalTime << endl;

    // here I need to print smaller burst time


    }
      return 0;
      }

بالمناسبه هذا الكود يقوم بعرض نفس البيانات

     cout <<"P" << p[smallest_arrivalTime_index].processId << "\t" << p[smallest_arrivalTime_index].burstTime << "\t" << p[smallest_arrivalTime_index].arrivalTime << endl;
     cout <<"P" << p[smallest_burst_index].processId << "\t" << p[smallest_burst_index].burstTime << "\t" << p[smallest_burst_index].arrivalTime << endl;

في البداية اريد ان اعرض اصغر arrival time من ثم ابدا اعرض بقية process حسب الاصغر فالاصفر من burst time

مثال:

process burst time arrival time

p0 5 5

p1 3 1

p2 4 0

p3 9 2

result should be, p2(because first arrived), p1,p0 and p3

في الانتظار

تم تعديل هذه المشاركة بواسطة empror9 في 25 أغسطس 2012 في 12:02

#2

لعلى قد افدتك بشئ ...

1.الكود بعد التعديل:

#include<iostream>
using namespace std;

struct process
{
    int processId;
    int burstTime;
    int arrivalTime;
};

int main()
{
    int numberOfProcesses;
    //number of process
    cout << "please enter the number of process: ";
    cin >> numberOfProcesses;
    const int maxprocesses = 10;
    struct process p[maxprocesses];

    // get burst time , arrival time
    for (int i = 0; i < numberOfProcesses; i++)
    {
        p.processId = i;
        cout << "p" << i << ":" << endl;
        cout << "Burst time: ";
        cin >> p.burstTime;
        cout << "Arrival Time: ";
        cin >> p.arrivalTime;
    }

    //display user input
    cout << "Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
    for (int i = 0; i < numberOfProcesses; i++)
        cout << "P" << p.processId << "\t" << p.burstTime << "\t" << p.arrivalTime << endl;


// الفرز باسلوب الفقاعة Bubble Sort
    struct process tmp;
    for (int i = 0; i < numberOfProcesses; i++)
    {
        for (int j = 0; j < numberOfProcesses - 1; j++)
        {
            if (p[j].arrivalTime > p[j + 1].arrivalTime)
            {
                tmp = p[j];
                p[j] = p[j + 1];
                p[j + 1] = tmp;
            }
        }
    }

    //display
    cout << endl;
    cout << "Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
    for (int i = 0; i < numberOfProcesses; i++)
        cout << "P" << p.processId << "\t" << p.burstTime << "\t" << p.arrivalTime << endl;

    return 0;
}

2.المدخلات والمخرجات:

please enter the number of process: 4
p0:
Burst time: 5
Arrival Time: 5
p1:
Burst time: 3
Arrival Time: 1
p2:
Burst time: 4
Arrival Time: 0
p3:
Burst time: 2
Arrival Time: 9
Process	Burst	Arrival
P0	5	5
P1	3	1
P2	4	0
P3	2	9

Process	Burst	Arrival
P2	4	0
P1	3	1
P0	5	5
P3	2	9
Press [Enter] to close the terminal ...

هذا والله ولى التوفيق

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#3

اشكرك لكن ليس هذا المطلوب

المطلوب اولا ان يتم اختيار اصغر اريفل تايم وهذا العمل تم تنفيذه

بعد ذلك يجب اختيار بقية البروسيس على حسب الاصغر فالاصغر بالنسبه لـ برست تايم ولا ننظر الى الاريفل تايم مطلقا

حيث ان البرنامج التي تفضلت بتعديله يحسب جميع البروسيس حسب الاريفل تايم فقط

بالبدايه نريد اخذ اصغر اريفل تايم ومن ثم نختار الاصغر فالاصغر بالنسبه للبرست تايم ولا نكترث بشان الاريفل تايم

مثال

process burst time arrival time

p0 5 5

p1 3 1

p2 4 0

p3 9 2

result should be, p2(because first arrived), p1,p0 and p3

#4

1. ارجاع اصغر Arrival Time

2.ثم فرز burst time تصاعديا مع استبعاد Arrival time الاولى ..

1

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#5

الكود بعد التعديل 2

#include<iostream>
using namespace std;

struct process
{
    int processId;
    int burstTime;
    int arrivalTime;
};

int main()
{
    int numberOfProcesses;
    //number of process
    cout << "please enter the number of process: ";
    cin >> numberOfProcesses;
    const int maxprocesses = 10;
    struct process p[maxprocesses];

    // get burst time , arrival time
    for (int i = 0; i < numberOfProcesses; i++)
    {
        p.processId = i;
        cout << "p" << i << ":" << endl;
        cout << "Burst time: ";
        cin >> p.burstTime;
        cout << "Arrival Time: ";
        cin >> p.arrivalTime;
    }

    //display user input
    cout << "Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
    for (int i = 0; i < numberOfProcesses; i++)
        cout << "P" << p.processId << "\t" << p.burstTime << "\t" << p.arrivalTime << endl;

    int max = p[0].arrivalTime; // start with max = first element
    int maxelementindex = 0;
// ايجاد اصغر وقت وصول
    for (int i = 1; i < numberOfProcesses; i++)
    {
        if (p.arrivalTime < max)
        {
            max = p.arrivalTime;
            maxelementindex = i;
        }
    }
// وضع اصغر وقت وصولفى اول المصفوفة  
    struct process tmp;
    tmp = p[0];
    p[0] = p[maxelementindex];
    p[maxelementindex] = tmp;
    //ثم الفرز من النصر الثانى الى الاخير تصاعدى حسب وقت برست
// الفرز باسلوب الفقاعة Bubble Sort
    for (int i = 1; i < numberOfProcesses; i++)
    {
        for (int j = 1; j < numberOfProcesses - 1; j++)
        {
            if (p[j].burstTime > p[j + 1].burstTime)
            {
                tmp = p[j];
                p[j] = p[j + 1];
                p[j + 1] = tmp;
            }
        }
    }

    //display
    cout << endl;
    cout << "Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
    for (int i = 0; i < numberOfProcesses; i++)
        cout << "P" << p.processId << "\t" << p.burstTime << "\t" << p.arrivalTime << endl;

    return 0;
}

المدخلات والمخرجات

please enter the number of process: 4
p0:
Burst time: 5
Arrival Time: 5
p1:
Burst time: 3
Arrival Time: 1
p2:
Burst time: 4
Arrival Time: 0
p3:
Burst time: 9
Arrival Time: 2
Process	Burst	Arrival
P0	5	5
P1	3	1
P2	4	0
P3	9	2

Process	Burst	Arrival
P2	4	0
P1	3	1
P0	5	5
P3	9	2
Press [Enter] to close the terminal ...
1

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#6

بالضبط :)

الله يعطيك العافيه حبيبي

مواضيع مشابهة