I'm getting an error with this code. Both the findMax and findMin lines in main say "Invalid conversion from 'int' to 'int*'". What does it mean?

#include <iostream>
using namespace std;

int findMax(int[],int);
int findMin(int[],int);

int main ()
{
    int max, min, x;
    findMax(max,x);
    findMin(min,x);

    return 0;
}

int findMax(int a[], int n)
{
    cout << "How many numbers would you like to enter? ";
    cin >> n;
    int max;
    max=a[n];

    for(int i=0; i<n; i++)
    {
        if(max>a[i])
            max=a[i];
    }

    return max;
}

int finMin(int a[], int n)
{
    cout << "How many numbers would you like to enter? ";
    cin >> n;
    int min;
    min=a[n];

    for(int i=0; i<n; i++)
    {
        if(min<a[i])
            min=a[i];
    }

    return min;
}

Your two functions, findMax() and findMin(), take a pointer as the first parameter because when passing an array to a function the whole array is not passed, but just a pointer to the start of the array. So the first parameter in those two functions are pointers and you are trying to pass an int there. And the compiler is complaining that it can't convert the int that you are passing to the pointer type that is required.

You probably want to do this instead:

findMax(int[max],x);

Edited 4 Years Ago by MandrewP: Added content

I tried that but now it says "expected primary-expression before 'int'".

Ooops, findMax(int[max], x) is incorrect. Sorry. But looking over your code, it seems as if you are trying to calculate the maximum or minimum number from a series of numbers that you input, right? But I don't see where you even input any series of numbers. So is this right? If so, then your code is way off. Let me know and I can outline your code and show you where you are making your errors.

You first want to fill your array. Then send that array to both min and max functions. The array may be filled in the main fucntion, then sent to each of the functions to find the maximum and minimum values in the array.

You can fill an array like this.

int newArray[] = {1,2,3,4,5};
int sizeOfArray  = 5;

Which will create a new integer array named, newArray, with 5 integers that can be indexed from newArray[0] through newArray[4].

Once the array has been created, you send the array to each -calling- functions as such.

findMaximumValueinArray(newArray, sizeOfArray);         // whereas sizeOfArray is the actual size of the array. 

whereas the -declared- and -defined- function will look like this.

int findMaximumValueInArray(int newArray[], int sizeOfArray);

Once you have sent your array to each of the functions, you may then loop through the array using the sizeOfArray as its length so you do not attempt to preview past the bounds of the array. The first value of the array can be considered either your minimum or maximum value of the array and then compare the next value with your min/max value. If the next value is either lesser/greater than your current min/max value, replace your min/max value with the lesser/greater value that you just checked. Continue this until the end of the for loop. By this time you will have found your min/max value in the array.

This article has been dead for over six months. Start a new discussion instead.