maxIndex in an array

This is a discussion on maxIndex in an array within the C++ Programming forums, part of the General Programming Boards category; Please help to check and explain the error please. Code: #include <iostream> #include <cstdlib> using namespace std; int maxIndex(int a[], ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    maxIndex in an array

    Please help to check and explain the error please.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int maxIndex(int a[], int n);
    
    int main()
    {
        const int max_size = 30;
        int list[max_size];
        int n;
        
        
        cout<<"Enter size of array:";
        cin>>n;
    
    //input numbers into array
        
        for(int i = 0; i < n; i++)
        {
                cin>>list[i];
        }
        
        cout<<"The largest integer entered was "<< maxIndex(list[], n)<<endl;
    
    system("pause");
    return 0;
    }
    
    
    
    int maxIndex(int a[], int n)
    
    // Pre: n > 0, and the array elements a[0], a[1], ..., a[n-1]
    //      contain integer values.
    
    // Post: returns the index of the first occurrence of the
    //       largest element of a[0..n-1].
    
    {
      int k = 0;
      int i = 1;
      while (i < n)
      {
        if (a[i] > a[k])
           k = i;
        i++;
      }
      return k;
    }
    when i compile the code, the error stated on the red colour.

    thank you

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    cout<<"The largest integer entered was "<< maxIndex(list, n)<<endl;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    thank you very much...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21