vendredi 21 avril 2017

Why don't these two arrays combine properly? C++

This program is supposed to take the sorted input of 2 different arrays X and Y and combine them into array Z. Array Z is then supposed to be sorted. The program does compile, but I get a run-time error telling me that the stack around 'y' has been corrupted. Any ideas?

    #include <iostream>
    #include <cmath>
    #include <conio.h>
    using namespace std;

    void order_da_numberz(double[], const int);
void combine_da_arrayz(double[], double[], double[], int, int);

int main() {

    double x[99];
    double y[99];
    double z[198];
    int arraySizeX;
    int arraySizeY;
    int arraySizeZ;

        cout << "How many numbers you wish to enter into array X (100 max): " << endl;
        cin >> arraySizeX;

        while (arraySizeX <= 100) {

            cout << "Enter the numbers into the array X: " << endl;

            for (int a = 0; a < arraySizeX; a++)
                cin >> x[a];

            order_da_numberz(x, arraySizeX);
            arraySizeX = 101;
        }

        cout << "How many numbers you wish to enter into array Y (100 max): " << endl;
        cin >> arraySizeY;

        while (arraySizeY <= 100) {

            cout << "Enter the numbers into the array Y: " << endl;

            for (int a = 0; a < arraySizeY; a++)
                cin >> y[a];

            order_da_numberz(y, arraySizeY);
            arraySizeY = 101;
        }


        arraySizeZ = arraySizeX + arraySizeY;
        combine_da_arrayz(x, y, z, arraySizeY, arraySizeZ);

        for (int q = 0; q < arraySizeZ; q++)
            cout << x[q] << "\t";


    }

    void order_da_numberz(double x[], int arraySize) {

        int swap;

        for (int c = 1; c <= arraySize; c++) {
            for (int d = 0; d < (arraySize - 1); d++) {

                if (x[d + 1] < x[d]) {
                    swap = x[d];
                    x[d] = x[d + 1];
                    x[d + 1] = swap;
                }
            }
        }

    }




    void combine_da_arrayz(double x[], double y[], double z[], int arraySizeY, int arraySizeZ) {

        for (int i = 0; i<10; i++)
        {
            z[i] = x[i];
            z[i + arraySizeY] = y[i];
        }

        order_da_numberz(z, arraySizeZ);




    }

Aucun commentaire:

Enregistrer un commentaire