Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 根据可用空间使用另一个数组中的项填充数组_C++_Arrays_Pointers_Multidimensional Array - Fatal编程技术网

C++ 根据可用空间使用另一个数组中的项填充数组

C++ 根据可用空间使用另一个数组中的项填充数组,c++,arrays,pointers,multidimensional-array,C++,Arrays,Pointers,Multidimensional Array,如何将每辆卡车的“装载空间”(2D阵列的第二部分)限制为1000?loadTrucks函数中的do while循环无法按预期工作。我尝试过对数组求和,并以各种组合使用do-while、a-while和if/else语句对其进行限制,但没有效果。非常感谢您的帮助或建议。谢谢你抽出时间 #include <iostream> #include <iomanip> #include <ctime> #include <cstdlib> using nam

如何将每辆卡车的“装载空间”(2D阵列的第二部分)限制为1000?loadTrucks函数中的do while循环无法按预期工作。我尝试过对数组求和,并以各种组合使用do-while、a-while和if/else语句对其进行限制,但没有效果。非常感谢您的帮助或建议。谢谢你抽出时间

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

int* initializePackages(int &numPackages);
void printArray(int* arr, int size);
void shufflePackages(int* packages, int size);
int** createTrucks(int &numTrucks);
void printTrucks(int** arr, int size);
void loadTrucks(int** trucks, int* packages, int numTrucks, int numPackages);

int main()
{
    int numPackages; //receives the passed in values from initializePackages.
    int numTrucks;  //receives the passed in values from createTrucks.

    srand(time(0)); //Seed the random function with the time clock.

    int* iPackages = initializePackages(numPackages);
    cout << "\nSorted Packages: \n";
    printArray(iPackages, numPackages);

    shufflePackages(iPackages, numPackages);
    cout << "\n\nShuffled Packages: \n";
    printArray(iPackages, numPackages);

    int** iTrucks = createTrucks(numTrucks);
    cout << "\n\nInitial Trucks: \n";
    printTrucks(iTrucks, numTrucks);

    cout << "\nLoaded Trucks: \n";
    loadTrucks(iTrucks, iPackages, numTrucks, numPackages);
    printTrucks(iTrucks, numTrucks);

}

int* initializePackages(int &numPackages)
{

    int* packages = 0; //returning the pointer from initializePackages.
    numPackages = 0;  //to pass the number of packages to main.

    cout << "Enter the number of packages to be loaded (between 60 and 500):  " << endl;
    while(numPackages < 60 || numPackages > 500){
        cin >> numPackages; //takes in the number of packages to be assigned.
    }

    packages = new int[numPackages]; //dynamically allocates an array of packages of based on the value of numPackages.

    for(int i=0; i < numPackages; i++){   //initializes the array with sequential values specified by numPackages.
        *(packages + i) = i +1;
    }

    return packages;
}

void printArray(int* arr, int size)
{
    for(int count = 0; count < size; count ++){
        cout << arr[count] << ((count + 1) % 20 ? " " : "\n"); //Accesses the array and prints out 20 elements per line.
    }
}

void shufflePackages(int* packages, int size)
{
    int j;      //additional variable to utilize as a swap variable.
    int temp;  //place holder variable.

    for(int i = 0; i < size-1; i++){  //for loop to iterate through the shuffle sequence.
        j = rand() % size;           //selects random spot in the array to begin shuffling
        temp = packages[i];         //assigns a place holder variable to the initial value
        packages[i] = packages[j]; //empty packages[i] element is now assigned the value from the packages[j] element
        packages[j]=temp;         //packages[j] element is now assigned the initial value originally assigned to the place holder variable
    }
}

int** createTrucks(int &numTrucks)
{
        numTrucks = 0;


        cout << "\nEnter the number of trucks to be loaded (between 3 and 25):  "  << endl;
        while(numTrucks < 3 || numTrucks > 25){    //specifies the parameters for the number of trucks
                cin >> numTrucks;
        }

        int** trucks = new int* [numTrucks];    //dynamically allocates an array based on number of trucks.

        for(int i = 0; i < numTrucks; i++){   //accesses the truck array and assigns a number to each truck.
            trucks[i] = new int[20];         //dynamically allocates an array of 20 elements

            for(int j = 0; j < 20; j++){   //initializes the truck's available load space with zeros.
                trucks[i][j] = 0;
            }
        }

    return trucks;
}

void printTrucks(int** arr, int size)
{
    for(int i = 0; i < size; i++){   //loop to print out the trucks and number. "Truck #: "
        cout << "Truck " << i + 1 << ": ";

        for(int j = 0; j < 20; j++){ //loop prints out the available load space of the truck's trailer.
            cout << " " << arr[i][j];
        }
        cout << "\n"; //Starts a new line of truck and load space to be printed.
    }
}

void loadTrucks(int** trucks, int* packages, int numTrucks, int numPackages)
{
    int sum = 0;
    int temp;


    for(int i = 0; i < numTrucks; i++){
        for(int j = 0; j < 20; j++){
            for(int k = 0; k < numPackages; k++){
                do{
                    temp = packages[k];
                    packages[k] = trucks[i][j];
                    trucks[i][j] = temp;
                    sum += trucks[i][j];
                }while(sum < 1000);
            }
        }
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
int*初始化包装(int和numPackages);
void printary(int*arr,int size);
无效shufflePackages(整数*包,整数大小);
int**创建卡车(int和NUMTRUKS);
无效打印卡车(内部**arr,内部尺寸);
无效载重汽车(整箱**卡车、整箱*包裹、整箱卡车、整箱包装);
int main()
{
int numPackages;//从initializePackages接收传入的值。
int numTrucks;//从CreateTracks接收传入的值。
srand(time(0));//使用时钟为随机函数设定种子。
int*iPackages=初始化包装(numPackages);
cout
for(int i=0;i

如果你想实现的是你所说的,那么你需要这样做。检查卡车中是否有空间,并将当前包裹添加到卡车中。如果卡车已满(1000+数量或20个包裹)您只需到达下一个卡车。
Break
存在当前循环并到达下一个卡车。

在我的loadTrucks函数或Main中?或者像一个四重嵌套for循环一样?当
loadTrucks
中时,您的
打算做什么?应该将每行限制为每个“包”的整数之和(卡车[j]的单个元素[i][j])整个装载量在1000以内。@Bastard24,检查我的答案。修复了您实际需要的内容。如果有帮助,请检查,如果没有问题,请标记我的答案。您可能需要一个double for和一个
int countTotal=0
,并在每一步检查卡车中是否有更多空间,或者是否应该转到另一个。继续检查,似乎退出了我接近(或确切地)你想要达到的目标,但也许我没有完全理解你。
for(int i = 0; i < numTrucks; i++){
        for(int j = 0; j < 20; j++){
            for(int k = 0; k < numPackages; k++){
                if(trucks[i][j] + packages[k] < 1000)
                {
                    trucks[i][j] += packages[k];//adds the package quantity to the truck
                    sum += trucks[i][j];
                }
                else break; //gets to the next truck, because the quantity is already filled
            }
        }
    }