将一个数组的元素分配给2d数组的元素

时间:2016-02-14 03:09:17

标签: c++ arrays pointers

对于我的项目,我必须将一个动态分配的数组的元素分配给动态分配的2D数组的另一部分。我已经研究过,但找不到与我的问题有关的任何内容。

当我尝试运行程序时,它只为一个"加载空间"上的每个元素分配一个值。每辆卡车我感谢任何帮助和建议。我不允许使用数组库。谢谢你的时间。

#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\nTrucks: \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;

    for(int i = 0; i < numTrucks; i++){
        for(int j = 0; j < 20; j++){
            for(int k = 0; k < numPackages; k++){
                trucks[i][j] = packages[k];
            }
        }
    }
}

0 个答案:

没有答案