-
April 19th, 2010, 05:25 PM
#1
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.
-
April 19th, 2010, 05:49 PM
#2
-
April 19th, 2010, 05:50 PM
#3
Re: Array elements
Which part are you stuck on?
-
April 19th, 2010, 05:59 PM
#4
Re: Array elements
You can do this in 4 lines using a combination of std::min_element, std::max_element, and std:istance.
-
April 19th, 2010, 09:00 PM
#5
Re: Array elements
Originally Posted by shani09
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
-
April 20th, 2010, 12:28 AM
#6
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;
}
-
April 20th, 2010, 12:53 AM
#7
-
April 20th, 2010, 02:43 AM
#8
Re: Array elements
Debug your code step-by-step and try to find out what and where differs from your expectations.
Victor Nijegorodov
-
April 20th, 2010, 07:20 AM
#9
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.
-
April 20th, 2010, 09:43 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|