lundi 22 mars 2021

Want to print next biggest number

So Here is the question: Consider a class named Job that has deadline as a data member and relevant getter/setter method(s). Assume you have to schedule two most earliest jobs on the basis of their deadlines. That is, if there are three jobs in the system with deadlines (deadline1, deadline2, and deadline3, respectively) then the system should report the top two earliest jobs (with smallest deadline value). You might need to find the deadline with smallest and second most smallest value.

Here is my code:

#include<iostream>
using namespace std;
class job
{
private:
    int Deadline;
public:
    static int i;
    void setDeadline(int a);
    int getDeadline();
};

void job::setDeadline(int a)
{
    Deadline = a;
    cout << "Job " << i << " Has Deadline " << Deadline << endl;
}

int job::getDeadline()
{
    return Deadline;
}
int job::i = 1;

int main()
{
    job job1, job2, job3, job4, job5, job6, job7, job8, job9, job10, count;

    job1.setDeadline(5);
    count.i++;
    job2.setDeadline(3);
    count.i++;
    job3.setDeadline(6);
    count.i++;
    job4.setDeadline(12);
    count.i++;
    job5.setDeadline(31);
    count.i++;
    job6.setDeadline(20);
    count.i++;
    job7.setDeadline(19);
    count.i++;
    job8.setDeadline(2);
    count.i++;
    job9.setDeadline(8);
    count.i++;
    job10.setDeadline(7);

    int array[10] = { job1.getDeadline(), job2.getDeadline(),job3.getDeadline(),job4.getDeadline(),job5.getDeadline(),job6.getDeadline(),job7.getDeadline(),job8.getDeadline(),job9.getDeadline(),job10.getDeadline() };
    int temp = array[0], store = 0, first = 0, second = 0;


    for (int i = 0; i <= 9; i++)
    {
        if (temp > array[i])
        {
            temp = array[i];
        }
    }
    for (int i = 0; i <= 9; i++)
    {
        if (temp == array[i])
        {
            first = i + 1;
            break;
        }
    }
    temp = array[0];
    for (int i = 0; i <= 9; i++)
    {
        if (temp > array[i])
        {
            temp = array[i];
        }
    }
    for (int i = 0; i <= 9; i++)
    {
        if (temp == array[i])
        {
            second = i + 1;
        }
    }
    cout << "\nJob " << first << " and Job " << second << " are earliest";
    return 0;
}

the problem I am facing is that both times it print the first smallest value. I want to print first 2 smallest value. How can I do that?

Aucun commentaire:

Enregistrer un commentaire