# Position in an array

Page 1 of 1

## 4 Replies - 1781 Views - Last Post: 13 April 2009 - 02:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=99032&amp;s=c8dfb3f579311e66b5a3f148e5e303e1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 40mpg

Reputation: 0
• Posts: 8
• Joined: 13-April 09

# Position in an array

Posted 13 April 2009 - 01:24 PM

```#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;

int main( )
{
const int N_LEVELS = 5;

int n = 0;
int sum = 0;
double average = 0.0;
int levelScore[N_LEVELS] = { 450, 316, 148, 775, 200 };
int minVal = 0;
int maxVal = 0;

for (n = 0; n < N_LEVELS; n++)
{
sum = sum + levelScore[n];
}

average = double(sum) / double(N_LEVELS);

minVal = maxVal = levelScore[0];

for(n=0; n< N_LEVELS; n++)
{
if(levelScore[n] < minVal) minVal = levelScore[n];
if(levelScore[n] > maxVal) maxVal = levelScore[n];
}

cout << "The average is " << average << endl;
// i do not know how to make it display the position(level) that the low score occur at
cout << " the low score is " << minVal << " at level " <<	<< endl;
// the same problem as above
cout << " the high score is " <<maxVal << " at level " <<   << endl;

system("PAUSE");
return 0;
}

```

ok as you can see..i want to display the position of the minimum score and the position of the maximum score in the array...but i do not know how to do it..thank you guys!

Is This A Good Question/Topic? 0

## Replies To: Position in an array

### #2 krum110487

• D.I.C Regular

Reputation: 39
• Posts: 291
• Joined: 07-February 09

## Re: Position in an array

Posted 13 April 2009 - 01:33 PM

within this for loop:

```for (n = 0; n < N_LEVELS; n++)
{
sum = sum + levelScore[n];
}

```

you should check each if the current value is less than the min value, if so, change min value to that current value (levelScore[n]) and you will do vice versa for the max value.

EDIT: Hmmm I looked at your code again, and I see that you are doing that, but in another loop, it *SHOULD* work....

This post has been edited by krum110487: 13 April 2009 - 01:35 PM

### #3 40mpg

Reputation: 0
• Posts: 8
• Joined: 13-April 09

## Re: Position in an array

Posted 13 April 2009 - 01:46 PM

i still don't understand, everything works fine, i found the minimum and maximum easily...but as you can see, i need the position in the array the the max and min value occur at..for the array above..it should be position 3 for the minimim and position 4 for the maximum value..i just don't know how to retrieve that from the arrray.

### #4 SixOfEleven

• Planeswalker

Reputation: 1055
• Posts: 6,643
• Joined: 18-October 08

## Re: Position in an array

Posted 13 April 2009 - 02:07 PM

40mpg, on 13 Apr, 2009 - 12:24 PM, said:

```#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;

int main( )
{
const int N_LEVELS = 5;

int n = 0;
int sum = 0;
double average = 0.0;
int levelScore[N_LEVELS] = { 450, 316, 148, 775, 200 };
int minVal = 0;
int maxVal = 0;

for (n = 0; n < N_LEVELS; n++)
{
sum = sum + levelScore[n];
}

average = double(sum) / double(N_LEVELS);

minVal = maxVal = levelScore[0];

for(n=0; n< N_LEVELS; n++)
{
if(levelScore[n] < minVal) minVal = levelScore[n];
if(levelScore[n] > maxVal) maxVal = levelScore[n];
}

cout << "The average is " << average << endl;
// i do not know how to make it display the position(level) that the low score occur at
cout << " the low score is " << minVal << " at level " <<	<< endl;
// the same problem as above
cout << " the high score is " <<maxVal << " at level " <<   << endl;

system("PAUSE");
return 0;
}

```

ok as you can see..i want to display the position of the minimum score and the position of the maximum score in the array...but i do not know how to do it..thank you guys!

```int minIndex;
int maxIndex;

```

You need to change your for loop to do something like this:

```for (n=0; n< N_LEVELS; n++)
{
if (levelScore[n] < minVal)
{
minVal = levelScore[n];
minIndex = n;
}
...
}

```

minIndex will hold the position in the array that has the minimum value.

Hope this helps!

*edit*

This post has been edited by SixOfEleven: 13 April 2009 - 02:09 PM

### #5 40mpg

Reputation: 0
• Posts: 8
• Joined: 13-April 09

## Re: Position in an array

Posted 13 April 2009 - 02:21 PM

it fixed it! thank you very much! awesome forum!