0

In this code , I am creating an array of structure and trying to sort the structure array based on "arr_time" field. I am having problem on how to pass the structure array by reference in function sort_process() .

#include<iostream>
using namespace std;

struct process {
    public:
    int p_id,arr_time,burst_time,comp_time;
};

void sort_process( process x[],int len)
{
    int i,j;
    process temp;
    for(i=0;i<len;i++)
    {
        for(j=0;j<len-1;j++)
        {
            if(x[j].arr_time > x[j+1].arr_time)
            {
                temp = x[j];
                x[j] = x[j+1];
                x[j+1] = temp;
            }
            }
        }
}


int main()
{
    int n,i=0,j=0,k=0,t=0,flag;
    cout<<"\n Enter number of processes : ";
    cin>>n;
    process p[n];

    for(i=0;i<n;i++)
        initialize(p[i]);   
    sort_process(p,n);  
    return 0;
}
Andreas
  • 5,393
  • 9
  • 44
  • 53

2 Answers2

0

You can simply use std::sort in the header <algorithm>:

std::sort(p, p+n, [](const process & p1, const process & p2){return p1 < p2}); // Will sort p in ascending order

But if you really want to do it manually and pass the array to a function, you can create the function as:

void sort_process(process * x, std::size_t len) // copy the pointer
{
    std::sort(x, x+len, [](const process & p1, const process & p2){return p1 < p2;});
}

But passing process x[] as you did is equivalent.

Note: As we refer to an array by the pointer to its first element, what your are passing here is a pointer to the first element too (and not the array).


If you really want to pass it by reference, you can do it as follows:

void sort_process(process * const & x, std::size_t len)
{
    std::sort(x, x+len, [](const process & p1, const process & p2){return p1 < p2;});
}

I added a const qualifier to forbid the modification of the pointer as it is not a copy but a reference to the original one

But you don't really gain anything by passing a reference here in my opinion.

Fareanor
  • 5,900
  • 2
  • 11
  • 37
0

Seems like you are afraid of dynamic arrays and pointers. Try this:

#include<iostream>
using namespace std;

struct process
{
    int p_id, arr_time, burst_time, comp_time;

    void initialize()
    {
        p_id = arr_time = burst_time = comp_time = 0;
    }

    process()
    {
        p_id = arr_time = burst_time = comp_time = 0;
    }

    ~process() {}
};

void sort_process(process*& x, int len)
{
    int i, j;
    process temp;
    for (i = 0; i < len; i++)
    {
        for (j = 0; j < len - 1; j++)
        {
            if (x[j].arr_time > x[j + 1].arr_time)
            {
                temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
            }
        }
    }
}

int main()
{
    int n, i = 0, j = 0, k = 0, t = 0, flag;

    cout << "\n Enter number of processes : ";
    cin >> n;

    process* p = new process[n];

    for (i = 0; i < n; i++)
        p[i].initialize();

    sort_process(p, n);

    for (i = 0; i < n; i++)
        p[i].~process();
    delete[] p; p = 0;

    return 0;
}
Perotto
  • 194
  • 1
  • 14