array function errors

This is a discussion on array function errors within the C++ Programming forums, part of the General Programming Boards category; I'm writing a function that returns the maximum element of an array. I made an array that had 10 elements. ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    array function errors

    I'm writing a function that returns the maximum element of an array. I made an array that had 10 elements. For some reason I keep getting these errors and im not sure why. Any help would be greatly appreciated, thank you.

    These are my errors:
    Code:
         max.cpp(25) : error C2065: 'size' : undeclared identifier
         (26)error C2065: 'a' : undeclared identifier
          (26) : error C2109: subscript requires array or pointer type
    And the following is my code.

    Code:
       
    #include <iostream>
    using namespace std;
    int max (void);//prototype
    int main()
    {
     
     const int size=10;
    int a[size]={1,2,3,4,5,6,7,8,9,10};
    
     max();//call
     cout<<"The max number is"<<max<<endl;
    
      return 0;
     
    }
    
     int max(void)//function that returns maximum element of array
     {
    
       int MAX=10;
        int x=MAX;
    
      for (int i=0; i<size;i++)
       if (a[i]==x)
         
    	 return x;
    
     }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    because both a and size are local variables to main(). max() can not see nor does it know anything about those two variables. Simple make the following change:
    in main() :
    Code:
    max();//call
     cout<<"The max number is"<<max<<endl;
    To:
    Code:
     cout<<"The max number is"<<max(a, size)<<endl;
    and change the header for max() to:
    Code:
    int max (int a[], int size)

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Ok Thantos I made those changes and now I get a warning:

    Code:
         warning C4715: 'max' : not all control paths return a value
    I have no idea what that means. Here's my edited code.

    Code:
         
    
    #include <iostream>
    using namespace std;
    int max (int a[], int size );//prototype
    int main()
    {
     
     const int size=10;
    int a[size]={1,2,3,4,5,6,7,8,9,10};
    
     
     cout<<"The max number is"<<max(a, size);//call
    
      return 0;
     
    }
    
     int max(int a[], int size)//function that returns maximum element of array
     {
    
       int MAX=10;
        int x=MAX;
    
      for (int i=0; i<size;i++)
       if (a[i]==x)
         
    	 return x;
    
     }

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok what that error means is this:
    It is possible that given an unknown array there will never be a value that matches the value of x. Your current function is setup only to return a value if and only if it finds a match. You need to have a return with a specified value at the end of the function to handle the cases where no match is found.

    Now that said you should change your max() function to act truely as a max. The following is a fairly easy method for doing it:
    Code:
    int max (int a[], int size)
    {
      int max = a[0];
      for (int i=1; i < size; i++)
    	if ( max < a[i] )
    	  max = a[i];
      return max;
    }
    Last edited by Thantos; 07-08-2004 at 01:21 PM.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It just means that from the point of view of your compiler, it is possible that the values could be such that your if statement in max() never yields true, and that your function will not return a value. As it says, it's just a warning, but if you want to fix the problem use this:

    Code:
    if (a[i]==x) return x
    else return 0
    And then just add something to report an error if the result is 0

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    For his loop you do not want to do that sean. If you did that it would return on the first iteration of the loop regardless of its value

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Code:
    int max = a[0];
      for (int i=1; i < size; i++)
    	if ( max < a[i] )
    	  max = a[i];
      return max;
    Ok thantos, I made the change using the code you provided above and it worked perfectly. I have a small question though. I experimented some and put int max=a[9] since 10 is the biggest element in the array and it worked as well. Is setting it to a[0] more foolproof than a[9] or does it really make any difference at all? Thank you both for your help.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well a[9] worked for you because it was the largest. The reason you use a[0] though is because of this:

    1) Regardless of the size a[0] will be valid
    2) You have to step through the array one element at a time (unless you know its sorted but thats a different story) as such the first element you encounter will be the max, min, average, mean, etc until you look at another element. So it makes since to initalize your max with that value.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Ok that makes sense. Thank you Thantos.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Though it seems you are probably doing this as a learning exercise, which is a good thing, just to let you know that there is something already available that will do the same thing you are attempting here. Keep on learning by doing like you are... but file this away in memory for the future.

    Code:
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        const int size = 10;
        int a = {1,2,3,4,5,6,7,8,9,10};
    
        cout << "The max number is " << *max_element(a,a+size) << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Interesting. I will keep that in mind for the future. Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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