Go through the array. Have two variables.
double smallest = MAX_DOUBLE_VALUE; //Get this from the limits.h include file. I'm not sure if that's the exact way to do it, but that's it.
double largest = MIN_DOUBLE_VALUE; //Same as for MAX.
The reason you do the min and the max for the opposite variables is that you are guaranteed to change them then, no matter the number range.
int lcv = 0;
int position_smallest;
int position_largest;
int array_length = sizeof(array) / sizeof(array[0];
for( ; lcv < array_length; lcv++)
{
if(array[lcv] < smallest)
{
smallest = array[lcv];
position_smallest = lcv;
}
if(array[lcv] > largest)
{
largest = array[lcv];
position_largest = lcv;
}
}
There you go, now your two variables position_smallest and position_largest have the positions of the numbers you are looking for.
Hope it helps! If you have any more questions about the code, I'd be more than happy to help.
2007-02-07 23:15:10
·
answer #1
·
answered by Anonymous
·
1⤊
1⤋
With c++ you have the luxury of STL. Just use the sort algorithm and use the iterators calls begin() and end() to get the smallest and largest values.
2007-02-07 23:18:22
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋
#include
using namespace std;
void slposition (double b[], int size)
{
int i;
double small=b[0];
double big=b[0];
int smallindex=0;
int bigindex=0;
for(i=1;i
if (small > min(small,b[i])) smallindex=i;
if (big < max(big,b[i])) bigindex=i;
}
cout << "\n position of the smallest : " << smallindex;
cout << "\n position of the largest : " << bigindex;
return;
}
main () {
double a[10];
int n,i;
while(true){
system("cls");
cout << "\nEnter number of the elements :";
cin >> n;
for(i=0;i
cout << "\nEnter element " << i << " : ";
cin >> a[i];
}
slposition (a,n);
cout << endl << endl;
system("pause");
}
}
2007-02-08 00:19:58
·
answer #3
·
answered by iyiogrenci 6
·
0⤊
0⤋
http://answers.yahoo.com/question/index;_ylt=Au7_eqMB9n_hXroAPg2lPBty7hR.?qid=20070208041249AA4Mep8
2007-02-07 23:24:28
·
answer #4
·
answered by Anonymous
·
0⤊
0⤋