PDA

View Full Version : Function with arrays: max() -help.




fBaran
May 21, 2005, 01:35 AM
Here's my error when g++'ing: "error: invalid types `double*[double]' for array subscript". If any one can help me with this, please! I've been trying for half an hour now, but don't get it. I got this error before, but can't remember for my life what it was about. Thanks in advance!


#include <iostream>
using namespace std;

double max(double array[], double start, double end);

main () {
double num[10];
cout <<endl;
populate (num, 0, 9);
max (num, 0, 9);
cout <<endl;
}

double populate (double array[], double start, double end)
{
double num[10];
for (int i=0; i<=end; i++)
{
cout <<"Enter digit " <<i+1 <<": " ;
cin >> num[i];
}
cout <<endl;
}

double max (double array[], double start, double end)
{
double num[10], maximum=array[start]; //ERROR HERE!?!
for (int i=0; i<=end; i++)
{
if (num[i] > maximum)
maximum = num[i];
cout <<maximum;
}
cout <<endl;
}

Edit: Error found! Yay 8)

maximum=array[start] should be maximum=array[0]. Why? Beats me!



superbovine
May 21, 2005, 02:26 AM
Your problem is you are using doubles when you should be using ints

your function should prototype like this


double max(double[], int, int);
double populate (double[], int, double);

double populate (double array[], int start, int end) {

}

double max (double array[], int start, int end) {

}


it is giving you the error because you can't access the index of an array with a double because array are only indexed with integers.

You also need to declare the variables in your function. for exmaple in max you are using variables that you never declared an int.

another problem is you are passing "num[]" from main into both your function, and call it 'array[]'. you need to use "array" because using num will be lost because "num[]" goes away after the function execute because it's scope is the life of the function call.

the function for max is just plain wrong. rethink that one. you need to do a sort routine.