【问题标题】:How to search array from last index?如何从最后一个索引搜索数组?
【发布时间】:2015-03-04 16:21:51
【问题描述】:

我的程序假设复制内存管理功能 Next-fit。基本上我的程序应该有一个工作和一个分区。它将检查作业是否小于或等于分区大小。如果小于或等于,则存储作业。一个值(索引)应该保存作业最后存储在哪个分区中,并从该位置开始搜索。

问题:我不知道如何从存储最后一个作业的位置连续调用搜索。

void  Partition::nextFit(Job *job, Partition *partitionArray, int partitionArraySize, int numberOfJobs)
{
    Partition temp;
    int Next;
    int index;
    //loops the amount of jobs
    for (int i = 0; i < numberOfJobs; i++)
    {
        //job less than or = partition
        if (job[i].jobSize <= partitionArray[i].size)
        {
            //partition =  partition - job
            partitionArray[i].size = partitionArray[i].size -job[i].jobSize;
            cout << "Unused space (Hole): " << partitionArray[i].size << "Kb" << endl;
            cout << endl;

        }
        //index becomes the location of where the last job was stored
        index = i;

        //relaunch loop to search from point index
}

【问题讨论】:

  • 您是否尝试过在for 循环中使用index 而不是i
  • 如果我使用 index 而不是 i 循环不会是无限的 (int index =0; index
  • @maulinrodriguez //relaunch loop to search from point index 我不明白你到底想要什么?您已经到了这一点,那么 relaunch 是什么意思?
  • 我的意思是例如 Job1 : 200kb Job2 : 300kb Job4 : 100kb Job5 : 200kb 并且分区是 Part1:300 part2:200 part3: 400 part4:100 在这种情况下,job 1 将第 1 部分作业 2 将转到第 3 部分作业 3 将转到第 4 部分作业 4 将必须转到第 2 部分。到目前为止,它已访问数组 sub 0,2 和 3。如果我按原样离开循环它将在第四个循环后中断。当程序到达作业 4 时,它必须返回数组 sub 0,然后再返回到 1 并将作业 4 存储在 1 中。@πάνταῥεῖ
  • @maulinrodriguez 根据您的描述,您是否在开始编写代码之前就如何完成这项工作制定了计划?

标签: c++ arrays search


【解决方案1】:
void  Partition::nextFit(Job *job, Partition *partitionArray, int partitionArraySize, int numberOfJobs)
{
    int j = 0;
    int count=0;
    int locationOfMemory;

//finds the largest partition
    int maxsize = partitionArray[0].size;

    for (int l = 0; l < partitionArraySize; l++)
    {
        if (maxsize <= partitionArray[l].size)
        {
            maxsize = partitionArray[l].size;
        }
    }


//loops the amount of jobs

    for (int i = 0; i < numberOfJobs; i++)
    {
        if (job[i].jobSize < maxsize)
        {
            //job less than or = partition
            if (job[i].jobSize <= partitionArray[j].size)
            {
                locationOfMemory = j+1;
                //partition =  partition - job
                cout << "Job: " << i + 1 << " was allocated in Partition: " << locationOfMemory<< endl;
                partitionArray[j].size = partitionArray[j].size - job[i].jobSize;
                cout << "Unused space (Hole): " << partitionArray[j].size << "Kb" << endl;
                cout << endl;
                if (j != partitionArraySize - 1)
                {
                    j++;
                }
                else
                {
                    j = 0;
                }

            }
            else
            {    //if the job is larger than the partition stay at the current job and increment the partition index to the next one
                i = i - 1;
                j = j + 1;
                }
        }
         else
        {
            cout << "Job" << "is too big and cannot be allocated" << endl;
        }
    }
//partition busy or not
    for (int m = 0; m < partitionArraySize; m++)
    {

         if (partitionArray[m].size == 0)
        {
            count = m + 1;
            cout << "Partition # " << count << " Status: Busy" << endl;
        }
        else
        {
            count = m + 1;
            cout << "Partition # " << count << " Status: Free" << endl;
        }
    }


}

【讨论】:

    猜你喜欢
    • 2013-02-14
    • 2016-02-16
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2010-11-06
    • 2012-11-25
    相关资源
    最近更新 更多