CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Array elements

  1. #1
    Join Date
    Apr 2010
    Posts
    14

    Array elements

    Hello eveyone;

    Please does any one know how to return the location of the maximum and minimum elements in an array from a function to the main. I mean the locations not the values. Like if the maximum and minimum are the third and fifth elements respectively in the array(of course entered by the user) it returns the both locations to the main.

    Thank you.

  2. #2
    Join Date
    Apr 2010
    Posts
    14

    Re: Array elements

    anyone pls?

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Array elements

    Which part are you stuck on?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Array elements

    You can do this in 4 lines using a combination of std::min_element, std::max_element, and std:istance.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array elements

    Quote Originally Posted by shani09 View Post
    Hello eveyone;

    Please does any one know how to return the location of the maximum and minimum elements in an array from a function to the main.
    Well, you asked if anyone knows how to do this, so yes, I know how to do that.

    What's your next question?

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 2010
    Posts
    14

    Re: Array elements

    Okay here is what i wrote. it is giving me wrong results:

    #include <iostream>
    using namespace std;

    int SIZEE (int [],int size, int&, int&);

    int main()
    {
    const int SIZE = 100;
    int a[SIZE];
    int nums, min, max;
    cout<<"Enter the total number of integers"<<endl;
    cin>>nums;

    cout<<"Enter the numbers"<<endl;
    for (int count=0; count<nums; count++)
    {
    cin>>a[count];
    }

    SIZEE(a,nums,max,min);
    cout<<"The max position is"<<max<<""<<"and the min position is"<<min<<endl;
    return 0;
    }

    int SIZEE (int arr[],int size, int &min, int &max)
    {

    max=arr[0];

    min=arr[0];

    for(int count=0;count<size;count++)

    {
    cout<<arr[count]<<" ";

    { if(arr[count]>max)
    max=arr[count];
    max=count+1;
    }





    if(arr[count]<min)
    { min=count+1;
    min=arr[count];
    }


    }

    return max,min;

    }

  7. #7
    Join Date
    Apr 2010
    Posts
    14

    Re: Array elements

    please tell me my error

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Array elements

    Debug your code step-by-step and try to find out what and where differs from your expectations.
    Victor Nijegorodov

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Array elements

    If you want help, you need to use code tags and proper indentation. See the difference?

    Without tags and formatting.
    { if(arr[count]>max)
    max=arr[count];
    max=count+1;
    }
    With tags and formatting and white space.
    Code:
    {	
        if(arr[count ] > max)
             max = arr[count];
        max = count + 1;
    }
    Think about what you're doing with max there vs. what you want to be doing. You're trying to use it for two things and once and that won't work.

  10. #10
    Join Date
    Jun 2005
    Posts
    315

    Re: Array elements

    it is giving me wrong results
    What is it that you expect versus what you are getting?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured