Klinetel
Klinetel

Reputation: 302

Simple Array Finding Index Position of Highest and Lowest Value

I am trying to write a simple logical program to find the highest and lowest values, which has already been done. The issue is how to find the index position is when it finds the highest and lowest value. See the attached picture. enter image description here

#include <iostream>

using namespace std;


int main()
{

int number=0, min=0, max=0;
int *rangeOfNumbers = new int[];

cout<<"Enter 5 numbers: ";

for(int i=0; i<5;i++)
{
    cout<<"Enter number "<<i+1<<": ";
    cin>>rangeOfNumbers[i];
}

//max min array positions

min=rangeOfNumbers[0];
max=rangeOfNumbers[0];

//find max and mins
for(int j=0; j<5;j++)
{
if(max<rangeOfNumbers[j])
{
    max=rangeOfNumbers[j];
}
else if(min>rangeOfNumbers[j])
{
    min=rangeOfNumbers[j];
}
}

cout<<"\n\nMin number: "<<min;
cout<<"\nMax number: "<<max;

cin.get();
cin.get();

return 0;
}

Upvotes: 1

Views: 35374

Answers (5)

Maikel Luis
Maikel Luis

Reputation: 11

I would suggest a tiny optimization, if you initialize min and max at item 0 why ask again for the same element

for (int I = 0;

in place

for (int I = 1;

The source code:

#include <iostream>

using namespace std;

#define SIZE 5

int main()
{

    int min=0, max=0;
    int *rangeOfNumbers = new int[SIZE];

    cout<<"Enter 5 numbers: ";

    for(int i=0; i < SIZE; i++)
    {
        cout<<"Enter number " << i + 1 <<": ";
        cin>>rangeOfNumbers[i];
    }

    //max min array positions

    min = rangeOfNumbers[0];
    max = rangeOfNumbers[0];

    int minindex = 0;
    int maxindex = 0;

    //find max and mins
    for(int j = 1; j < SIZE; j++)
    {
        if( max < rangeOfNumbers[j])
        {
            max = rangeOfNumbers[j];
            maxindex = j;
        }
        if( min > rangeOfNumbers[j])
        {
            min = rangeOfNumbers[j];
            minindex = j;
        }
    }

    maxindex += 1;
    minindex += 1;

    cout<<"\n\nMin number: "<<min;
    cout<<"\nMax number: "<<max;

    cin.get();
    cin.get();

    return 0;
}

Upvotes: 0

maditya
maditya

Reputation: 8906

In addition to updating min and max, also keep an index variable and update it.

//max min array positions

min=rangeOfNumbers[0];
max=rangeOfNumbers[0];
int minindex = 0;
int maxindex = 0;

//find max and mins
for(int j=0; j<5;j++)
{
  if(max<rangeOfNumbers[j])
  {
    max=rangeOfNumbers[j];
    maxindex = j;
  }
  if(min>rangeOfNumbers[j])
  {
    min=rangeOfNumbers[j];
    minindex = j;
  }
}

maxindex += 1;
minindex += 1;

Upvotes: 4

user995502
user995502

Reputation:

This might be far from what you are looking for right now. However you should get used to stl containers (vectors, lists and all that stuff) if you are going to do some intensive algorithms like finding max and min.

The benefit is that many algorithms are already available and are optimized for performance.

I think your example is just an exercise. anyways her is how to if it wasn't

For example this problem is ideal for using a vector. Here is an example

#include <iostream>
#include<vector>
#include<algorithm>

using namespace std;


int main()
{

    int number=0, min=0, max=0;
    vector<int> rangeOfNumbers;

    cout<<"Enter 5 numbers: ";

    for(int i=0; i<5;i++)
    {
        cout<<"Enter number "<<i+1<<": ";
        cin>>number;
        rangeOfNumbers.push_back(number);
    }

    vector<int>::iterator maxelem = max_element(rangeOfNumbers.begin(), rangeOfNumbers.end());
    vector<int>::iterator minelem = min_element(rangeOfNumbers.begin(), rangeOfNumbers.end());

    cout << endl << "Max number: " << (*maxelem) << " at " << std::distance(rangeOfNumbers.begin(), maxelem) + 1;
    cout << endl << "Min number: " << (*minelem)<< " at " << std::distance(rangeOfNumbers.begin(), minelem) + 1;

    cin.get();

    return 0;
}

Upvotes: 1

taocp
taocp

Reputation: 23664

Not sure how you can even compile the code:

int *rangeOfNumbers = new int[];

You need to specify a size when you new an array of integers.

int *rangeOfNumbers = new int[5];

I compiled under gcc 4.5.3, got the following error:

 error: expected primary-expression before ‘]’ token

You also need to remember the indices for max and min when you scan the array.

For example: before the for loop, initialize:

int maxIndex = -1;

Inside for loop:

if (max < A[i])
{
   maxIndex = i;
   max = A[i];
}

Similar stuff should be done for min.

Upvotes: 1

MostafaR
MostafaR

Reputation: 3695

Change max and min to indexOfMax and indexOfMin, Because by storing index of maximum you can access both maximum index and maximum value.

So you should change the max if to something like this:

if(rangeOfNumbers[indexOfMax] < rangeOfNumbers[j])
{
    indexOfMax = j;
}

Continue this change for other lines yourself.

Upvotes: 1

Related Questions