Question

Basically, I'm trying to set the size of my array according to an input (numArraySize will be asked for from the user). I created an array pointer in main and want to pass this pointer to the functions.

#include <iostream>
using namespace std;

// Algorithm for Insertion Sort
int numArraySize; 

void inputNums(int numArray[])
{
    cout << "Enter a bunch of numbers: " ;
    for(int x=0; x<numArraySize; x++)
    {
        cin >> numArray[x];
    }
}
void outputNums(int numArray[])
{
    for(int x=0; x<numArraySize; x++)
    {
        cout << numArray[x];
        if(x != numArraySize-1)
        {
            cout << " - ";
        }
    }
}
void insertionSort(int numArray[])
{
    int num;
    int i;
    for(int j=1; j<numArraySize; j++)
    {
        num = numArray[j];
        i = j-1;
        while(i>=0 && numArray[i] > num)
        {
            numArray[i+1] = numArray[i];
            i--;
        }
        numArray[i+1] = num;
    }
}

int main()
{
    int *numbers = new int[numArraySize];
    int choice;

    cout << "1) Insertion Sort" << endl;
    cout << "Enter your choice: " << endl;

    cin >> choice;              // Input choice for which algorithm to use

    if(choice == 1)
    {
        cout << "Enter size of the array: ";
        cin >> numArraySize;
        inputNums(numbers);         // Insert numbers
        insertionSort(numbers);     // Use algorithm to sort then output
        outputNums(numbers);
    }

    cout << endl;
    return 0;
}

EDIT:

Ok i fixed the error. I changed: "int numArraySize=1;" and the position of "int *numbers = new int[numArraySize];"

The problem was that i had to initialize the numArraySize anyways. That causes error apparently. And the second problem was i have to input numArraySize before i initialize "int *numbers" in the main function.

    #include <iostream>
    using namespace std;

    // Algorithm for Insertion Sort
    int numArraySize=1; 

    void inputNums(int numArray[])
    {
        cout << "Enter a bunch of numbers: " ;
        for(int x=0; x<numArraySize; x++)
        {
            cin >> numArray[x];
        }
    }
    void outputNums(int numArray[])
    {
        for(int x=0; x<numArraySize; x++)
        {
            cout << numArray[x];
            if(x != numArraySize-1)
            {
                cout << " - ";
            }
        }
    }
    void insertionSort(int numArray[])
    {
        int num;
        int i;
        for(int j=1; j<numArraySize; j++)
        {
            num = numArray[j];
            i = j-1;
            while(i>=0 && numArray[i] > num)
            {
                numArray[i+1] = numArray[i];
                i--;
            }
            numArray[i+1] = num;
        }
    }

    int main()
    {
        int choice;

        cout << "1) Insertion Sort" << endl;
        cout << "Enter your choice: " << endl;

        cin >> choice;              // Input choice for which algorithm to use

        if(choice == 1)
        {
            cout << "Enter size of the array: ";
            cin >> numArraySize;
            int *numbers = new int[numArraySize];
            inputNums(numbers);         // Insert numbers
            insertionSort(numbers);     // Use algorithm to sort then output
            outputNums(numbers);
        }

    cout << endl;
    system("pause");
    return 0;
}

Thanks for your helps :)

Was it helpful?

Solution

To pass an array to a function, you can simply use int *array:

void outputNums(int *numArray)
{
    for(int x=0; x<numArraySize; x++)
    {
        cout << numArray[x];
        if(x != numArraySize-1)
        {
            cout << " - ";
        }
    }
}

Please note that it would be better to use a standard container like a vector.

OTHER TIPS

When you are allocating the memory for the array in line

int *numbers = new int[numArraySize]

, you still haven't received the desired size for the array in variable numArraySize.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top