Function with arrays: max() -help.

Discussion in 'Mac Programming' started by fBaran, May 20, 2005.

  1. fBaran macrumors regular

    Joined:
    Oct 22, 2003
    #1
    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!


    Code:
    #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)

    Code:
    maximum=array[start]
    should be
    Code:
    maximum=array[0]
    . Why? Beats me!
     
  2. superbovine macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #2
    Your problem is you are using doubles when you should be using ints

    your function should prototype like this

    Code:
    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.
     

Share This Page