Christopher Littlewood
Christopher Littlewood

Reputation: 803

invalid conversion from int to int*

I am getting the error:

Invalid conversion from int to int*.

I have not created any int* (I think) and when I change the offending lines to be int*, There are no build errors but the program crashes upon launch.

Here is my code:

//Main:
int main(){

    //Varibales:
    Random randomInt;
    clock_t start;
    clock_t End;
    double duration;
    double clocksPerSec;
    int result;
    int arraySize;


    //Repeat 100 times:
    for(int i=1; i<=100; i++){

        //Set array size:
        arraySize = i*500;
        //Create the array:
        int testArray[arraySize];

        //For the array size:
        for(int j=0; j<arraySize; j++){


            //Add random number to array:
            testArray[j] = randomInt.randomInteger(1, 10000);

        }

        //Run the test:
        start = clock();
        result = algorithmTest(testArray[arraySize], arraySize);
        End = clock();

        //Calculate execution time:
        duration = End - start;
        clocksPerSec = duration/CLOCKS_PER_SEC;

        //Display the result:
        cout << "The median is: ";
        cout << result << endl;
        cout << "Exection time was: ";
        cout << clocksPerSec;
        cout << "s\n" << endl;

    }

    //Return 0:
    return 0;

}

It seams to be throwing the error when i call algorithmTest(); Here it is:

//First Test:
int algorithmTest(int testArray[], int Size){

    //Declare variables:
    int k = Size/2;
    int numSmaller;
    int numEqual;

    //For every element in the array:
    for(int i=0; i<Size; i++){

        //Set varibales to 0:
        numSmaller = 0;
        numEqual = 0;

        //For every element in the array:
        for(int j=0; j<Size; j++){

            //If j is less than i:
            if(testArray[j] < testArray[i]){

                //Increment numSmaller:
                numSmaller++;

            //Else if j is equal to i:
            }else if(testArray[j] == testArray[i]){

                //Increment numEqual:
                numEqual++;

            }
        }

        //Determine if the median was found:
        if(numSmaller < k && k <= (numSmaller + numEqual)){

            //Retrun the medain:
            return testArray[i];

        }
    }

    //Return 0:
    return 0;

}

Upvotes: 0

Views: 1444

Answers (2)

xhg
xhg

Reputation: 1885

result = algorithmTest(testArray[arraySize], arraySize);

should be

result = algorithmTest(testArray, arraySize);

Your function int algorithmTest(int testArray[], int Size) takes an int[] as first argument, while you pass a testArray[arraySize], where [i] operator means fetch the value at ith element of testArray, which is an int. Therefore you encounter that error.

In order to clarify something, the [...] in the line int testArray[arraySize]; is different from the [...] in the line result = algorithmTest(testArray[arraySize], arraySize);: first one is for indicating array's size, while the second one is for accessing the element.

Upvotes: 1

Neutrosider
Neutrosider

Reputation: 572

Look at the definition of AlgorithmTest. You require an int[] (also known as int*) as first parameter, but when you call it you give it an actual int

Upvotes: 0

Related Questions