Position in an array

how do i display a position in an array

Page 1 of 1

4 Replies - 823 Views - Last Post: 13 April 2009 - 02:21 PM Rate Topic: -----

#1 40mpg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 942
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Position in an array

Posted 13 April 2009 - 02:07 PM

View Post40mpg, 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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