# Position in an array

Page 1 of 1

## 4 Replies - 1246 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=90cf52ce082f1109a6fdab1a0ca0e42d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 40mpg

• New D.I.C Head

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

Was This Post Helpful? 0

### #3 40mpg

• New D.I.C Head

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.
Was This Post Helpful? 0

### #4 SixOfEleven

• using Caffeine;

Reputation: 947
• Posts: 6,342
• 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!

Add two more variables to your code:

```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.

You can add your own code for the maxIndex.

Hope this helps!

*edit*
made a typo

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

Was This Post Helpful? 0

### #5 40mpg

• New D.I.C Head

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!
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }